2026-03-12 19:18:01 -05:00
|
|
|
import { PrismaClient } from '@prisma/client'
|
2026-03-12 17:09:22 -05:00
|
|
|
|
|
|
|
|
const globalForPrisma = globalThis as unknown as {
|
|
|
|
|
prisma: PrismaClient | undefined
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 20:30:36 -05:00
|
|
|
function getPrismaClient(): PrismaClient {
|
2026-03-12 20:27:11 -05:00
|
|
|
if (!globalForPrisma.prisma) {
|
|
|
|
|
globalForPrisma.prisma = new PrismaClient({
|
|
|
|
|
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return globalForPrisma.prisma
|
|
|
|
|
}
|
2026-03-12 17:09:22 -05:00
|
|
|
|
2026-03-12 20:30:36 -05:00
|
|
|
// Use a Proxy so `new PrismaClient()` is only called when a property
|
|
|
|
|
// is first accessed (inside a request handler), NOT at module import time.
|
|
|
|
|
// This prevents PrismaClientConstructorValidationError during Next.js
|
|
|
|
|
// static analysis / "Collecting page data" phase.
|
|
|
|
|
export const prisma = new Proxy({} as PrismaClient, {
|
|
|
|
|
get(_target, prop) {
|
|
|
|
|
return Reflect.get(getPrismaClient(), prop)
|
|
|
|
|
},
|
|
|
|
|
apply(_target, thisArg, args) {
|
|
|
|
|
return Reflect.apply(getPrismaClient() as unknown as Function, thisArg, args)
|
|
|
|
|
},
|
|
|
|
|
})
|