26 lines
712 B
TypeScript
26 lines
712 B
TypeScript
import { PrismaClient } from "@prisma/client";
|
|
import { PrismaPg } from "@prisma/adapter-pg";
|
|
import pkg from "pg";
|
|
|
|
const { Pool } = pkg;
|
|
|
|
const globalForPrisma = globalThis as unknown as {
|
|
prisma: PrismaClient | undefined;
|
|
};
|
|
|
|
const getPrismaClient = () => {
|
|
const connectionString = process.env.DATABASE_URL;
|
|
if (!connectionString) {
|
|
throw new Error("DATABASE_URL is not configured in your environment variables.");
|
|
}
|
|
const pool = new Pool({ connectionString });
|
|
const adapter = new PrismaPg(pool);
|
|
return new PrismaClient({ adapter });
|
|
};
|
|
|
|
export const prisma = globalForPrisma.prisma ?? getPrismaClient();
|
|
|
|
if (process.env.NODE_ENV !== "production") {
|
|
globalForPrisma.prisma = prisma;
|
|
}
|