"use strict"; /** * Dutchie AZ Database Connection * * Isolated database connection for Dutchie Arizona data. * Uses a separate database/schema to prevent cross-contamination with main app data. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getDutchieAZPool = getDutchieAZPool; exports.query = query; exports.getClient = getClient; exports.closePool = closePool; exports.healthCheck = healthCheck; const pg_1 = require("pg"); // Consolidated DB naming: // - Prefer CRAWLSY_DATABASE_URL (e.g., crawlsy_local, crawlsy_prod) // - Then DUTCHIE_AZ_DATABASE_URL (legacy) // - Finally DATABASE_URL (legacy main DB) const DUTCHIE_AZ_DATABASE_URL = process.env.CRAWLSY_DATABASE_URL || process.env.DUTCHIE_AZ_DATABASE_URL || process.env.DATABASE_URL || 'postgresql://dutchie:dutchie_local_pass@localhost:54320/crawlsy_local'; let pool = null; /** * Get the Dutchie AZ database pool (singleton) */ function getDutchieAZPool() { if (!pool) { pool = new pg_1.Pool({ connectionString: DUTCHIE_AZ_DATABASE_URL, max: 10, idleTimeoutMillis: 30000, connectionTimeoutMillis: 5000, }); pool.on('error', (err) => { console.error('[DutchieAZ DB] Unexpected error on idle client:', err); }); console.log('[DutchieAZ DB] Pool initialized'); } return pool; } /** * Execute a query on the Dutchie AZ database */ async function query(text, params) { const p = getDutchieAZPool(); const result = await p.query(text, params); return { rows: result.rows, rowCount: result.rowCount || 0 }; } /** * Get a client from the pool for transaction use */ async function getClient() { const p = getDutchieAZPool(); return p.connect(); } /** * Close the pool connection */ async function closePool() { if (pool) { await pool.end(); pool = null; console.log('[DutchieAZ DB] Pool closed'); } } /** * Check if the database is accessible */ async function healthCheck() { try { const result = await query('SELECT 1 as ok'); return result.rows.length > 0 && result.rows[0].ok === 1; } catch (error) { console.error('[DutchieAZ DB] Health check failed:', error); return false; } }