import pg from 'pg'; import { database } from '$lib/config'; const { Pool } = pg; const pool = new Pool(database); export async function query(text, params) { const client = await pool.connect(); try { const result = await client.query(text, params); return result; } finally { client.release(); } } export async function queryWithAbort(text, params, signal) { if (signal?.aborted) { throw new Error('Query aborted'); } const client = await pool.connect(); const queryPromise = client.query(text, params); if (signal) { const abortPromise = new Promise((_, reject) => { signal.addEventListener('abort', () => { client.release(); reject(new Error('Query aborted')); }, { once: true }); }); try { const result = await Promise.race([queryPromise, abortPromise]); return result; } finally { client.release(); } } try { const result = await queryPromise; return result; } finally { client.release(); } } export default pool;