import { PrismaClient } from "@prisma/client"; // Safety net: Prisma returns BigInt columns (fileSize, highResSize, …) as // BigInt, which JSON.stringify refuses — one such column anywhere in an API // response turns the whole route into a 500. Serialize them as strings, the // same shape the routes that convert them by hand already produce. Routes // that spread whole rows into NextResponse.json are then safe by default. declare global { interface BigInt { toJSON(): string; } } if (!("toJSON" in BigInt.prototype)) { Object.defineProperty(BigInt.prototype, "toJSON", { value: function toJSON(this: bigint) { return this.toString(); }, writable: true, configurable: true, }); } const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined; }; export const db = globalForPrisma.prisma ?? new PrismaClient({ log: process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"], }); if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = db;