34bca145e4
Deploy / deploy (push) Failing after 9m27s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FbMbJPadPzVwvt9rU9GBNo
28 lines
1.0 KiB
TypeScript
28 lines
1.0 KiB
TypeScript
import { Pool, types } from 'pg';
|
|
|
|
// COUNT()/SUM() come back as bigint (type 20) and numeric (1700): hand them to JS as numbers.
|
|
types.setTypeParser(20, (v) => parseInt(v, 10));
|
|
types.setTypeParser(1700, (v) => parseFloat(v));
|
|
|
|
const g = globalThis as unknown as { __pgPool?: Pool };
|
|
|
|
function pool(): Pool {
|
|
if (!g.__pgPool) {
|
|
if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
|
|
// scripts/load-pg.js publishes each build as schema "live"
|
|
g.__pgPool = new Pool({ connectionString: process.env.DATABASE_URL, max: 8, options: '-c search_path=live' });
|
|
}
|
|
return g.__pgPool;
|
|
}
|
|
|
|
/** Run SQL written with `?` placeholders (rewritten to $1, $2, ...). */
|
|
export async function query<T = any>(sql: string, params: unknown[] = []): Promise<T[]> {
|
|
let i = 0;
|
|
const text = sql.replace(/\?/g, () => `$${++i}`);
|
|
return (await pool().query(text, params as any[])).rows as T[];
|
|
}
|
|
|
|
export async function one<T = any>(sql: string, params: unknown[] = []): Promise<T | undefined> {
|
|
return (await query<T>(sql, params))[0];
|
|
}
|