const fs = require('fs'); const path = require('path'); const { Pool } = require('pg'); const pool = new Pool({ connectionString: process.env.DATABASE_URL, }); pool.on('error', (err) => { console.error('Unexpected Postgres pool error:', err); }); // Apply schema.sql on startup (idempotent — uses CREATE TABLE IF NOT EXISTS). async function init() { const schema = fs.readFileSync(path.join(__dirname, '..', 'db', 'schema.sql'), 'utf8'); await pool.query(schema); } function query(text, params) { return pool.query(text, params); } // Run a function inside a transaction, passing it a dedicated client. async function transaction(fn) { const client = await pool.connect(); try { await client.query('BEGIN'); const result = await fn(client); await client.query('COMMIT'); return result; } catch (err) { await client.query('ROLLBACK'); throw err; } finally { client.release(); } } module.exports = { pool, query, transaction, init };