import { drizzle } from 'drizzle-orm/postgres-js'; import postgres from 'postgres'; import * as schema from './schema/index'; export type Database = ReturnType; export interface DatabaseOptions { url?: string; /** Connection pool size. Keep modest; the agent worker opens its own. */ max?: number; /** Log every statement. Never enable in production — values include PII. */ debug?: boolean; } export function createDatabase(options: DatabaseOptions = {}) { const url = options.url ?? process.env.DATABASE_URL; if (!url) { throw new Error( 'DATABASE_URL is not set. PIG needs its own dedicated Postgres database — ' + 'do not point it at one shared with another application.', ); } const sql = postgres(url, { max: options.max ?? 10, // Drizzle handles its own type parsing; leaving this on causes surprises // with numeric columns, which here carry money and GPU-hours. transform: undefined, onnotice: () => {}, debug: options.debug ? (_c, query) => console.debug('[db]', query) : undefined, }); return drizzle(sql, { schema }); } export { schema }; export * from './schema/index';