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 { PrismaClient } from '@prisma/client'
|
||||||
import dotenv from 'dotenv'
|
|
||||||
|
|
||||||
// Load .env file if it exists
|
// Load .env file if it exists
|
||||||
dotenv.config()
|
require('dotenv').config()
|
||||||
|
|
||||||
const globalForPrisma = globalThis as unknown as {
|
const globalForPrisma = globalThis as unknown as {
|
||||||
prisma: PrismaClient | undefined
|
prisma: PrismaClient | undefined
|
||||||
@@ -10,16 +9,24 @@ const globalForPrisma = globalThis as unknown as {
|
|||||||
|
|
||||||
// Detect database provider from environment (default to sqlite for local development)
|
// Detect database provider from environment (default to sqlite for local development)
|
||||||
const databaseProvider = process.env.DATABASE_PROVIDER || 'sqlite'
|
const databaseProvider = process.env.DATABASE_PROVIDER || 'sqlite'
|
||||||
|
const databaseUrl = process.env.DATABASE_URL
|
||||||
|
|
||||||
// Create PrismaClient with appropriate adapter
|
// Create PrismaClient with appropriate adapter
|
||||||
const createPrismaClient = () => {
|
const createPrismaClient = () => {
|
||||||
let client: PrismaClient
|
let client: PrismaClient
|
||||||
|
|
||||||
if (databaseProvider === 'postgresql') {
|
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
|
// Use PrismaPg adapter for PostgreSQL
|
||||||
const { PrismaPg } = require('@prisma/adapter-pg')
|
const { PrismaPg } = require('@prisma/adapter-pg')
|
||||||
const pg = require('pg')
|
const adapter = new PrismaPg({ connectionString: databaseUrl })
|
||||||
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
|
|
||||||
client = new PrismaClient({ adapter })
|
client = new PrismaClient({ adapter })
|
||||||
} else {
|
} else {
|
||||||
// No adapter needed for SQLite
|
// No adapter needed for SQLite
|
||||||
|
|||||||
Reference in New Issue
Block a user