-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
31 lines (26 loc) · 775 Bytes
/
db.js
File metadata and controls
31 lines (26 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const { Pool } = require('pg');
require('dotenv').config();
const dbPort = Number(process.env.DB_PORT || 5432);
const useSSL = process.env.DB_SSL === 'true';
const pool = new Pool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: dbPort,
ssl: useSSL ? { rejectUnauthorized: true } : undefined,
});
const query = async (text, params) => {
try {
if (typeof text !== 'string' || !text.trim()) {
throw new Error('Query text must be a non-empty string.');
}
return await pool.query({ text, values: Array.isArray(params) ? params : [] });
} catch (err) {
console.error('Error executing query:', err.stack);
throw err;
}
};
module.exports = {
query,
};