-
Notifications
You must be signed in to change notification settings - Fork 11
/
database.js
49 lines (42 loc) · 954 Bytes
/
database.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// dependencies
const { Client } = require('pg');
let client;
async function connect (callback) {
client = new Client({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASS
});
// Connect to the database and handle errors
try {
console.log('Connecting...');
await client.connect();
console.log('Connected!');
callback();
} catch (error) {
console.error(error);
}
}
// Disconnect from the database
function disconnect () {
client.end(err => {
console.log('Disconnected from database');
if (err) {
console.log('There was an error during disconnection', err.stack);
}
});
}
async function query (query_text, query_params) {
try {
const result = await client.query(query_text, query_params);
return result;
}catch (err) {
console.error(err);
}
}
module.exports = {
connect,
disconnect,
query
};