feat: support both SQLite and PostgreSQL in prisma.ts
This commit is contained in:
+16
-5
@@ -1,6 +1,4 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
import { PrismaPg } from '@prisma/adapter-pg'
|
||||
import pg from 'pg'
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
// Load .env file if it exists
|
||||
@@ -10,11 +8,24 @@ const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined
|
||||
}
|
||||
|
||||
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
|
||||
// Detect database provider from environment (default to sqlite for local development)
|
||||
const databaseProvider = process.env.DATABASE_PROVIDER || 'sqlite'
|
||||
|
||||
// Create PrismaClient with adapter
|
||||
// Create PrismaClient with appropriate adapter
|
||||
const createPrismaClient = () => {
|
||||
const client = new PrismaClient({ adapter })
|
||||
let client: PrismaClient
|
||||
|
||||
if (databaseProvider === 'postgresql') {
|
||||
// Use PrismaPg adapter for PostgreSQL
|
||||
const { PrismaPg } = require('@prisma/adapter-pg')
|
||||
const pg = require('pg')
|
||||
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
|
||||
client = new PrismaClient({ adapter })
|
||||
} else {
|
||||
// No adapter needed for SQLite
|
||||
client = new PrismaClient()
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user