From 2ddacc3df726795d320b2138e38328b77bce6832 Mon Sep 17 00:00:00 2001 From: adnanfajlur Date: Sun, 6 Oct 2024 21:30:41 +0700 Subject: [PATCH] use prisma singleton --- app/libs/prisma.server.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/libs/prisma.server.ts b/app/libs/prisma.server.ts index 7669080..a794254 100644 --- a/app/libs/prisma.server.ts +++ b/app/libs/prisma.server.ts @@ -1,13 +1,15 @@ import { PrismaClient } from '@prisma/client' -declare global { - var __prisma: PrismaClient -} +function singleton(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 }