import { PrismaClient } from '@prisma/client'; import path from 'node:path'; /** * The Prisma CLI resolves a relative SQLite path (`file:./dev.db`) against the schema directory * (`prisma/`), but inside a bundled Nitro output it would resolve against the copied client and * open an empty database. Resolve relative sqlite paths against `/prisma` so the runtime and * the CLI always agree. Absolute paths and non-sqlite URLs are passed through unchanged. */ function resolveDatabaseUrl(): string | undefined { const url = process.env.DATABASE_URL; if (!url || !url.startsWith('file:')) return url; const file = url.slice('file:'.length); if (path.isAbsolute(file)) return url; return `file:${path.resolve(process.cwd(), 'prisma', file)}`; } // A single PrismaClient per process. const globalForPrisma = globalThis as unknown as { prisma?: PrismaClient }; export const db = globalForPrisma.prisma ?? new PrismaClient({ datasources: { db: { url: resolveDatabaseUrl() } } }); if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = db;