31 lines
952 B
TypeScript
31 lines
952 B
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) {
|
|
// Check if newly added models (e.g. category) exist on cached client
|
|
if ((globalForPrisma.prisma as any).category) {
|
|
return globalForPrisma.prisma;
|
|
}
|
|
}
|
|
const client = new PrismaClient({ adapter });
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
globalForPrisma.prisma = client;
|
|
}
|
|
return client;
|
|
}
|
|
|
|
export const db = getPrismaClient();
|