Currently I've got a Postgresql server running, I'm hoping to create a development database and a production database (i've already got this defined using package.json scripts)
I'm just wondering what the best way to use Prisma is in this scenario. I eventually plan on creating a dashboard for this bot, but for now I just want to get it working and using it properly. The docs example just say to use it in the index.ts file, but I doubt this is the best way to use it, especially if I need to use the database in commands. Could I get some recommendations/tips?
#Discord.JS - Best way to use Prisma?
10 messages · Page 1 of 1 (latest)
It’s not super clear to me what you’re asking. What do you mean by “best way to use”?
Sorry, i should've explained it better. I meant that the official documentation, their example is just using it and adding to the DB in the index.ts file, this wouldn't work since i'll have to work with it in different events, commands, etc. how could i go about doing this without having to connect to the server and disconnect each time it's being used? or if there's a better way that i'm just not thinking of, how ?
I would create a db.ts file that looks something like this:
import { PrismaClient } from '@prisma/client'
let prisma: PrismaClient
export const initPrisma = () => {
if (!prisma)
prisma = new PrismaClient()
}
export const getPrisma = () => prisma
and in your bot entrypoint/initialisation do
import { initPrisma } from "./db"
initPrisma()
then in your commands/events you can do
import { getPrisma } from "../db"
getPrisma().queryUser(4)
You could actually skip the initPrisma step and just instantiate it to a new PrismaClient up front. You may or may not want to call $connect on starting your application though (by default, it will spin up a connection to your DB on the first query, but you can tell it to do that immediately if you want to start the connections eagerly instead of lazily)
Instantiating it once in a file that you then import elsewhere is a solid choice (considering node will execute the file the first time you import it, and subsequent imports will refer to the same “object”). If you have a long-lived application object that your various commands, etc would have access to, you might want to put it there instead
I see, thank you guys for your help!
I also had a question about development/production, I noticed that in their example code they were using this code:
import { PrismaClient } from "@prisma/client";
const globalForPrisma = global as unknown as { prisma: PrismaClient };
export const prisma =
globalForPrisma.prisma ||
new PrismaClient({
});
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
export default prisma
to my understanding it uses globalForPrisma.prisma for development and a new PrismaClient() for production, although I'm not sure why. And do you mean "instantiating it once in a file that you then import elsewhere is a solid choice", do you mean just like how @jaunty edge did in in their code?