/** * Database Connection for Booking Service - PostgreSQL (Supabase) */ const { Pool } = require('pg'); const DATABASE_URL = process.env.DATABASE_URL || process.env.SUPABASE_DB_URL; let pool = null; async function connectDB() { if (pool) return pool; try { if (!DATABASE_URL) { console.error('❌ Booking Service: DATABASE_URL is not set'); return null; } const useSSL = process.env.PGSSLMODE !== 'disable' && !DATABASE_URL.includes('sslmode=disable'); pool = new Pool({ connectionString: DATABASE_URL, ssl: useSSL ? { rejectUnauthorized: false } : false }); const client = await pool.connect(); console.log('✅ Booking Service: PostgreSQL connected'); client.release(); return pool; } catch (err) { console.error('❌ Booking Service: PostgreSQL connection failed', err.message); pool = null; return null; } } function getPool() { if (!pool) { console.warn('⚠️ Booking Service: Accessing getPool() before connectDB()'); } return pool; } async function closeDB() { if (pool) { await pool.end(); pool = null; } } module.exports = { connectDB, getPool, closeDB };