-
-
Notifications
You must be signed in to change notification settings - Fork 127
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
Labels
enhancement
New feature or request
Comments
Tomato6966
changed the title
Support prisma.io as a database
How to use prisma.io as a database
Sep 9, 2022
Here is how I do it: SCHEMA: model Giveaways {
message_id String @id
data Json
}
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
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.
The text was updated successfully, but these errors were encountered: