fix: add defensive checks to prisma.ts to prevent build failures
Test / unit-tests (push) Has been cancelled
Test / unit-tests (push) Has been cancelled
This commit is contained in:
+11
-4
@@ -1,8 +1,7 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
// Load .env file if it exists
|
||||
dotenv.config()
|
||||
require('dotenv').config()
|
||||
|
||||
const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined
|
||||
@@ -10,16 +9,24 @@ const globalForPrisma = globalThis as unknown as {
|
||||
|
||||
// Detect database provider from environment (default to sqlite for local development)
|
||||
const databaseProvider = process.env.DATABASE_PROVIDER || 'sqlite'
|
||||
const databaseUrl = process.env.DATABASE_URL
|
||||
|
||||
// Create PrismaClient with appropriate adapter
|
||||
const createPrismaClient = () => {
|
||||
let client: PrismaClient
|
||||
|
||||
if (databaseProvider === 'postgresql') {
|
||||
// Validate DATABASE_URL is present for PostgreSQL
|
||||
if (!databaseUrl) {
|
||||
throw new Error(
|
||||
'DATABASE_URL environment variable is required when DATABASE_PROVIDER is set to postgresql. ' +
|
||||
'Current DATABASE_PROVIDER: ' + databaseProvider
|
||||
)
|
||||
}
|
||||
|
||||
// Use PrismaPg adapter for PostgreSQL
|
||||
const { PrismaPg } = require('@prisma/adapter-pg')
|
||||
const pg = require('pg')
|
||||
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
|
||||
const adapter = new PrismaPg({ connectionString: databaseUrl })
|
||||
client = new PrismaClient({ adapter })
|
||||
} else {
|
||||
// No adapter needed for SQLite
|
||||
|
||||
Reference in New Issue
Block a user