Cache PrismaClient globally in ALL environments (was only cached in dev). Without this, production could create multiple PrismaClient instances, each with its own connection pool, exhausting the database connections. v1.71.2 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
17 lines
404 B
TypeScript
17 lines
404 B
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
|
|
const globalForPrisma = global as unknown as { prisma: PrismaClient };
|
|
|
|
export const prisma =
|
|
globalForPrisma.prisma ||
|
|
new PrismaClient({
|
|
datasources: {
|
|
db: {
|
|
url: process.env.DATABASE_URL,
|
|
},
|
|
},
|
|
});
|
|
|
|
// Cache the client globally in ALL environments to prevent connection pool exhaustion
|
|
globalForPrisma.prisma = prisma;
|