69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import path from "node:path"
|
|
|
|
import { PrismaClient } from "@prisma/client"
|
|
|
|
declare global {
|
|
var prisma: PrismaClient | undefined
|
|
}
|
|
|
|
// Resolve a robust DATABASE_URL for all runtimes (prod/dev)
|
|
const PROJECT_ROOT = process.cwd()
|
|
const PRISMA_DIR = path.join(PROJECT_ROOT, "prisma")
|
|
|
|
function resolveFileUrl(url: string) {
|
|
if (!url.startsWith("file:")) {
|
|
return url
|
|
}
|
|
|
|
const filePath = url.slice("file:".length)
|
|
|
|
if (filePath.startsWith("//")) {
|
|
return url
|
|
}
|
|
|
|
if (path.isAbsolute(filePath)) {
|
|
return `file:${path.normalize(filePath)}`
|
|
}
|
|
|
|
const normalized = path.normalize(filePath)
|
|
const prismaPrefix = `prisma${path.sep}`
|
|
const relativeToPrisma = normalized.startsWith(prismaPrefix)
|
|
? normalized.slice(prismaPrefix.length)
|
|
: normalized
|
|
|
|
const absolutePath = path.resolve(PRISMA_DIR, relativeToPrisma)
|
|
|
|
if (!absolutePath.startsWith(PROJECT_ROOT)) {
|
|
throw new Error(`DATABASE_URL path escapes project directory: ${filePath}`)
|
|
}
|
|
|
|
return `file:${absolutePath}`
|
|
}
|
|
|
|
function normalizeDatasourceUrl(envUrl?: string | null) {
|
|
const trimmed = envUrl?.trim()
|
|
if (trimmed) {
|
|
return resolveFileUrl(trimmed)
|
|
}
|
|
|
|
if (process.env.NODE_ENV === "production") {
|
|
return "file:/app/data/db.sqlite"
|
|
}
|
|
|
|
return resolveFileUrl("file:./db.dev.sqlite")
|
|
}
|
|
|
|
const resolvedDatabaseUrl = normalizeDatasourceUrl(process.env.DATABASE_URL)
|
|
|
|
export const prisma =
|
|
global.prisma ?? new PrismaClient({ datasources: { db: { url: resolvedDatabaseUrl } } })
|
|
|
|
if (process.env.NODE_ENV !== "production") {
|
|
global.prisma = prisma
|
|
}
|
|
|
|
if (process.env.NODE_ENV !== "production") {
|
|
// Helps detect mismatched DB path during dev server bootstrap
|
|
// eslint-disable-next-line no-console
|
|
console.log("[prisma] Using database:", resolvedDatabaseUrl)
|
|
}
|