import { PrismaClient } from '@prisma/client' const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined } function getPrismaClient(): PrismaClient { if (!globalForPrisma.prisma) { globalForPrisma.prisma = new PrismaClient({ log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'], }) } return globalForPrisma.prisma } // 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) }, })