import { PrismaClient } from '@prisma/client'; import { PrismaPg } from '@prisma/adapter-pg'; import { Pool } from 'pg'; const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined; }; function createPrismaClient() { // In development without a database, we'll create a mock-friendly client // For production or when DATABASE_URL is set, use the real adapter const connectionString = process.env.DATABASE_URL; if (!connectionString) { console.warn('DATABASE_URL not set - database features will not work'); // Return a client that will throw clear errors when used return new PrismaClient({ // This will fail on actual DB operations but allows imports to work adapter: new PrismaPg(new Pool({ connectionString: 'postgresql://localhost:5432/dummy' })), }); } const pool = new Pool({ connectionString }); const adapter = new PrismaPg(pool); return new PrismaClient({ adapter, log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'], }); } export const db = globalForPrisma.prisma ?? createPrismaClient(); if (process.env.NODE_ENV !== 'production') { globalForPrisma.prisma = db; } export default db;