// Copied/adapted from novelpad-desktop apps/website/app/db/db.server.ts @ 62c56b87 import { schema, type NpOutreachDatabase } from '@novelpad/outreach-core'; import { drizzle } from 'drizzle-orm/node-postgres'; import pkg from 'pg'; const { Pool } = pkg; // HMR-safe singleton. Without this, Vite re-evaluates this module on hot // reload and instantiates a fresh Pool, while the previous pool lingers with // open connections until idleTimeoutMillis fires — see the equivalent note // in novelpad-desktop's apps/website/app/db/db.server.ts. const g = globalThis as unknown as { __outreachPool?: InstanceType; __outreachDb?: NpOutreachDatabase; }; const pool = g.__outreachPool ?? (g.__outreachPool = new Pool({ connectionString: process.env.DATABASE_URL, application_name: 'outreach-review', max: Number(process.env.PG_POOL_MAX ?? 10), idleTimeoutMillis: 30000, connectionTimeoutMillis: Number( process.env.PG_CONNECTION_TIMEOUT_MS ?? 15000, ), })); const db = g.__outreachDb ?? (g.__outreachDb = drizzle(pool, { schema }) as unknown as NpOutreachDatabase); export { db, pool };