-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPostgres.js
More file actions
42 lines (37 loc) · 1.17 KB
/
Postgres.js
File metadata and controls
42 lines (37 loc) · 1.17 KB
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
const { Client } = require("pg");
module.exports = class Postgres {
constructor(config) {
this.config = { ...config };
this.client = new Client(this.config);
}
connect() {
return new Promise((resolve, reject) => {
this.client.connect((err) => {
if (!err) {
console.log("Node database connection opened")
resolve();
} else {
console.log(err);
reject("Error getting node database connection");
}
})
})
};
close() {
return new Promise((resolve, reject) => {
this.client.end((err) => {
console.log("Node database connection closed");
this.client = new Client(this.config); //reinstantiate client for next run
});
})
};
query(query) {
return new Promise((resolve, reject) => {
this.client.query(query, (err, res) => {
if(typeof res == "undefined" || err)
return reject(err);
resolve(res);
});
});
}
};