35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
import { Pool } from 'pg';
|
|
import { PrismaPg } from '@prisma/adapter-pg';
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
const connectionString =
|
|
process.env.DATABASE_URL ||
|
|
'postgres://postgres:mBTWE2cDKGExtpktguD3HPe8y9Xr9kWFYdxV4WHQKISLLGoBAS4UdtfSCfXwvPpq@65.109.236.58:37298/postgres';
|
|
|
|
const pool = new Pool({ connectionString });
|
|
const adapter = new PrismaPg(pool);
|
|
|
|
const globalForPrisma = globalThis as unknown as {
|
|
prisma: PrismaClient | undefined;
|
|
};
|
|
|
|
function getPrismaClient(): PrismaClient {
|
|
if (process.env.NODE_ENV !== 'production' && globalForPrisma.prisma) {
|
|
// Ensure all models (category, cheatsheet, prompt) exist on cached client
|
|
if (
|
|
(globalForPrisma.prisma as any).category &&
|
|
(globalForPrisma.prisma as any).cheatsheet &&
|
|
(globalForPrisma.prisma as any).prompt
|
|
) {
|
|
return globalForPrisma.prisma;
|
|
}
|
|
}
|
|
const client = new PrismaClient({ adapter });
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
globalForPrisma.prisma = client;
|
|
}
|
|
return client;
|
|
}
|
|
|
|
export const db = getPrismaClient();
|