Files
petfeed/prisma.config.ts
T

37 lines
1.3 KiB
TypeScript

import { defineConfig } from "prisma/config";
import { readFileSync, existsSync } from "fs";
import { resolve } from "path";
// Prisma 7's env() helper doesn't reliably load .env on all platforms.
// Load it manually so DATABASE_URL is available when this config is evaluated.
function loadEnvFile(filename: string) {
const envPath = resolve(process.cwd(), filename);
if (!existsSync(envPath)) return;
const lines = readFileSync(envPath, "utf-8").split(/\r?\n/);
for (const line of lines) {
const match = line.match(/^([^#\s][^=]*)=(.*)$/);
if (!match) continue;
const key = match[1].trim();
const value = match[2].trim().replace(/^["']|["']$/g, "");
if (!(key in process.env)) process.env[key] = value;
}
}
loadEnvFile(".env");
loadEnvFile(".env.local"); // Next.js convention — takes priority
// Prisma CLI (db push, migrate) needs the DIRECT connection (port 5432).
// The pooled URL (PgBouncer, port 6543) hangs on schema operations.
const directUrl = process.env.DIRECT_URL ?? process.env.DATABASE_URL;
if (!directUrl) throw new Error("DIRECT_URL (or DATABASE_URL) is not set in .env or .env.local");
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: {
url: directUrl,
},
migrations: {
seed: "tsx prisma/seed.ts",
},
});