import pg from 'pg' const { Pool } = pg let pool: pg.Pool | null = null export function getPool(): pg.Pool { if (!pool) { const config = useRuntimeConfig() pool = new Pool({ connectionString: config.databaseUrl, max: 20, idleTimeoutMillis: 30000, connectionTimeoutMillis: 2000 }) pool.on('error', (err) => { console.error('Unexpected error on idle client', err) }) } return pool } export async function query( text: string, params?: any[] ): Promise> { const client = await getPool().connect() try { return await client.query(text, params) } finally { client.release() } } export async function transaction( callback: (client: pg.PoolClient) => Promise ): Promise { const client = await getPool().connect() try { await client.query('BEGIN') const result = await callback(client) await client.query('COMMIT') return result } catch (error) { await client.query('ROLLBACK') throw error } finally { client.release() } }