Skip to content

Commit

Permalink
use prisma singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanfajlur committed Oct 6, 2024
1 parent 240ae82 commit 2ddacc3
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions app/libs/prisma.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { PrismaClient } from '@prisma/client'

declare global {
var __prisma: PrismaClient
}
function singleton<Value>(name: string, value: () => Value): Value {
const globalStore = global as any

globalStore.__singletons ??= {}
globalStore.__singletons[name] ??= value()

if (!global.__prisma) {
global.__prisma = new PrismaClient()
return globalStore.__singletons[name]
}

global.__prisma.$connect()
const prisma = singleton('prisma', () => new PrismaClient())
prisma.$connect()

export const prisma = global.__prisma
export { prisma }

0 comments on commit 2ddacc3

Please sign in to comment.