import { PrismaPg } from '@prisma/adapter-pg'; import { PrismaClient } from '@prisma/client'; import { env } from '@/lib/env'; /** * Prisma client singleton. * * WP-01 wraps this with the tenant-scoping extension required by PLAN.md ยง3.1, * so every query is filtered by the session's account and scope, with * PostgreSQL row-level security behind it as defence in depth. Nothing outside * this module should construct a client. */ const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined; }; function createClient(): PrismaClient { const adapter = new PrismaPg({ connectionString: env.DATABASE_URL }); return new PrismaClient({ adapter, log: env.NODE_ENV === 'development' ? ['warn', 'error'] : ['error'], }); } export const prisma = globalForPrisma.prisma ?? createClient(); if (env.NODE_ENV !== 'production') { globalForPrisma.prisma = prisma; }