25 lines
598 B
TypeScript
25 lines
598 B
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
|
|
const globalForPrisma = globalThis as unknown as {
|
|
prisma: PrismaClient | undefined;
|
|
};
|
|
|
|
function createPrismaClient() {
|
|
const databaseUrl = process.env.DATABASE_URL;
|
|
|
|
if (!databaseUrl) {
|
|
console.error('[DB] DATABASE_URL is not set! Using fallback connection string.');
|
|
}
|
|
|
|
return new PrismaClient({
|
|
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
|
|
});
|
|
}
|
|
|
|
export const db =
|
|
globalForPrisma.prisma ?? createPrismaClient();
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
globalForPrisma.prisma = db;
|
|
}
|