Created
January 30, 2025 05:54
-
-
Save Xeven777/cdcaf6b8a3c4d4e8e835ea7efb5874af to your computer and use it in GitHub Desktop.
Safe way to initialise Prisma Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PrismaClient } from "@prisma/client"; | |
const prismaClientSingleton = () => { | |
return new PrismaClient(); | |
}; | |
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>; | |
const globalForPrisma = globalThis as unknown as { | |
prisma: PrismaClientSingleton | undefined; | |
}; | |
const prisma = globalForPrisma.prisma ?? prismaClientSingleton(); | |
export default prisma; | |
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment