Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use prisma.io as a database #479

Open
Tomato6966 opened this issue Aug 27, 2022 · 1 comment
Open

How to use prisma.io as a database #479

Tomato6966 opened this issue Aug 27, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@Tomato6966
Copy link

this supports:
postgresql, mongodb, mysql, sqlite and many more!

It's also easy to implement.
Just provide an example schema and ur methods will be like

db.giveaways.findFirst({
where: { id: "guildId" },
select: { data: true, id: true, ended: true }
})
or

db.giveaways.findMany({
where: { ended: false },
select: { data: true, id: true, ended: true }
})

etc. etc.

@Tomato6966 Tomato6966 added the enhancement New feature or request label Aug 27, 2022
@Tomato6966 Tomato6966 changed the title Support prisma.io as a database How to use prisma.io as a database Sep 9, 2022
@Tomato6966
Copy link
Author

Tomato6966 commented Sep 9, 2022

Here is how I do it:

SCHEMA:

model Giveaways {
  message_id String @id
  data       Json
}

As far as I tested, you do not need the autoincrementing id (this makes it possible to be able to delete after message_id)

DATABASE initation:

const { PrismaClient } = require("@prisma/client");
client.db = new PrismaClient();

MANAGER:

const stringifyCallback = (_, v) => (typeof v === 'bigint' ? `BigInt("${v}")` : v);
const parseCallback = (_, v) => typeof v === 'string' && /BigInt\("(-?\d+)"\)/.test(v) ? eval(v) : v;
const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {
    // This function is called when the manager needs to get all giveaways which are stored in the database.
    async getAllGiveaways() {
        return new Promise((resolve, reject) => {
            client.db.giveaways.findMany({
                where: { message_id: { } },
                select: { data: true, message_id: true }
            }).then((x) => {
                const res = x.filter(v => v?.data && v?.message_id);
                const giveaways = res.map((row) => JSON.parse(row.data, parseCallback));
                return resolve(giveaways);
            }).catch(err => {
                console.error(err);
                return reject(err);
            })
        });
    }

    // This function is called when a giveaway needs to be saved in the database.
    async saveGiveaway(messageId, giveawayData) {
        return new Promise((resolve, reject) => {
            client.db.giveaways.create({
                data: {
                    message_id: messageId,
                    data: JSON.stringify(giveawayData, stringifyCallback)
                }
            }).then(() => {
                resolve(true)
            }).catch(err => {
                console.error(err);
                return reject(err);
            })
        });
    }

    // This function is called when a giveaway needs to be edited in the database.
    async editGiveaway(messageId, giveawayData) {
        return new Promise((resolve, reject) => {
            client.db.giveaways.update({
                where: { message_id: messageId },
                data: {
                    data: JSON.stringify(giveawayData, stringifyCallback)
                }
            }).then(() => {
                resolve(true)
            }).catch(err => {
                console.error(err);
                return reject(err);
            })
        });
    }

    // This function is called when a giveaway needs to be deleted from the database.
    async deleteGiveaway(messageId) {
        return new Promise((resolve, reject) => {
            client.db.giveaways.delete({
                where: { message_id: messageId }
            }).then(() => {
                resolve(true)
            }).catch(err => {
                console.error(err);
                return reject(err);
            })
        });
    }
};

// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
    default: {
        botsCanWin: false,
        embedColor: '#FF0000',
        embedColorEnd: '#000000',
        reaction: '🎉'
    }
});
// We now have a giveawaysManager property to access the manager everywhere!
client.giveawaysManager = manager;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant