// db.js // Handles PostgreSQL database connection and queries const { Pool } = require('pg'); require('dotenv').config(); // Load environment variables from .env file // Create a new pool instance using environment variables // The pool manages multiple client connections const pool = new Pool({ user: process.env.DB_USER, host: process.env.DB_HOST, database: process.env.DB_DATABASE, password: process.env.DB_PASSWORD, port: process.env.DB_PORT, // Optional: Add SSL configuration if required by your database provider // ssl: { // rejectUnauthorized: false // Adjust as needed // } }); // Test the database connection on startup pool.connect((err, client, release) => { if (err) { console.error('Error acquiring client for initial DB connection test:', err.stack); // Exit the process if the database connection fails on startup process.exit(1); } else { console.log('Successfully connected to PostgreSQL database.'); // Release the client back to the pool release(); } }); // Export a query function to interact with the database module.exports = { query: (text, params) => pool.query(text, params), // You can add more specific database functions here if needed // Example: getClient: () => pool.connect() };