import { PrismaClient } from '@prisma/client'; import { fileURLToPath, pathToFileURL } from 'node:url'; import path from 'node:path'; declare global { // eslint-disable-next-line no-var var __vectorPrisma: PrismaClient | undefined; } function resolveSqliteUrl(raw: string | undefined): string | undefined { if (!raw || !raw.startsWith('file:')) return raw; const rest = raw.slice('file:'.length).replace(/^\/+/, ''); if (path.isAbsolute(rest) || /^[A-Za-z]:[\\/]/.test(rest)) { return 'file:' + rest.replace(/\\/g, '/'); } const here = path.dirname(fileURLToPath(import.meta.url)); const schemaDir = path.resolve(here, '..', 'prisma'); const absolute = path.resolve(schemaDir, rest); return 'file:' + absolute.replace(/\\/g, '/'); } const url = resolveSqliteUrl(process.env.DATABASE_URL); export const prisma: PrismaClient = globalThis.__vectorPrisma ?? new PrismaClient({ datasources: url ? { db: { url } } : undefined, log: process.env.NODE_ENV === 'production' ? ['error'] : ['warn', 'error'], }); if (process.env.NODE_ENV !== 'production') { globalThis.__vectorPrisma = prisma; }