import pg from 'pg'; import { config } from '../config.js'; const { Pool } = pg; export const pool = new Pool({ host: config.database.host, port: config.database.port, database: config.database.database, user: config.database.user, password: config.database.password, max: 20, idleTimeoutMillis: 30000, connectionTimeoutMillis: 10000, }); export async function query(text: string, params?: any[]): Promise> { return pool.query(text, params); } export async function getClient(): Promise { return pool.connect(); } export async function transaction(fn: (client: pg.PoolClient) => Promise): Promise { const client = await pool.connect(); try { await client.query('BEGIN'); const result = await fn(client); await client.query('COMMIT'); return result; } catch (e) { await client.query('ROLLBACK'); throw e; } finally { client.release(); } }