Androz2091 / discord-giveaways

🎉 Complete framework to facilitate the creation of giveaways using discord.js
https://discord-giveaways.js.org
MIT License
336 stars 127 forks source link

How to use prisma.io as a database #479

Open Tomato6966 opened 2 years ago

Tomato6966 commented 2 years ago

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 commented 1 year ago

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;