From 0f713deb0c3b6ec1f8c3c81eb27f0a91bc2079c4 Mon Sep 17 00:00:00 2001 From: Harish-Naruto Date: Sat, 14 Feb 2026 10:39:47 +0530 Subject: [PATCH 1/3] multiconnection fix --- src/db/client.ts | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/db/client.ts b/src/db/client.ts index 9d89206..d8c5921 100644 --- a/src/db/client.ts +++ b/src/db/client.ts @@ -1,11 +1,35 @@ import { PrismaPg } from "@prisma/adapter-pg"; import { PrismaClient } from "../generated/prisma/client"; +import { Pool } from "pg"; export * from "../generated/prisma/client"; -const adapter = new PrismaPg({ - connectionString: process.env.DATABASE_URL, -}); +const globalForPrisma = globalThis as unknown as { + prisma?: PrismaClient; + pgPool?: Pool; +}; + +const pool = + globalForPrisma.pgPool ?? + new Pool({ + connectionString: process.env.DATABASE_URL, + }); + +const adapter = new PrismaPg(pool); + +export const prisma = + globalForPrisma.prisma ?? + new PrismaClient({ + adapter, + log: + process.env.NODE_ENV === "development" + ? ["query", "error", "warn"] + : ["error"], + }); + +if (process.env.NODE_ENV !== "production") { + globalForPrisma.prisma = prisma; + globalForPrisma.pgPool = pool; +} -const prisma = new PrismaClient({ adapter }); export default prisma; \ No newline at end of file From ba0629bef7a1c555de99f9515c0c4c3034fa289d Mon Sep 17 00:00:00 2001 From: Harish-Naruto Date: Sat, 14 Feb 2026 10:45:29 +0530 Subject: [PATCH 2/3] add global PrismaClient to avoid multiple connection --- src/db/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/db/client.ts b/src/db/client.ts index d8c5921..b29c70e 100644 --- a/src/db/client.ts +++ b/src/db/client.ts @@ -21,12 +21,12 @@ export const prisma = new PrismaClient({ adapter, log: - process.env.NODE_ENV === "development" + process.env.NODE_ENV === "Development" ? ["query", "error", "warn"] : ["error"], }); -if (process.env.NODE_ENV !== "production") { +if (process.env.NODE_ENV !== "Production") { globalForPrisma.prisma = prisma; globalForPrisma.pgPool = pool; } From 33b4e46c77a9f3b5f3cc792504ef1517050720aa Mon Sep 17 00:00:00 2001 From: Harish-Naruto Date: Sat, 14 Feb 2026 10:50:24 +0530 Subject: [PATCH 3/3] change to lowercase letters --- src/db/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/db/client.ts b/src/db/client.ts index b29c70e..d8c5921 100644 --- a/src/db/client.ts +++ b/src/db/client.ts @@ -21,12 +21,12 @@ export const prisma = new PrismaClient({ adapter, log: - process.env.NODE_ENV === "Development" + process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"], }); -if (process.env.NODE_ENV !== "Production") { +if (process.env.NODE_ENV !== "production") { globalForPrisma.prisma = prisma; globalForPrisma.pgPool = pool; }