-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathdb.js
More file actions
43 lines (37 loc) · 713 Bytes
/
db.js
File metadata and controls
43 lines (37 loc) · 713 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
32
33
34
35
36
37
38
39
40
41
42
43
const pg = require("pg");
const { Pool } = pg;
const pool = new Pool({
connectionString: "postgresql://admin:3214@localhost:5432/cyf_ecommerce",
connectionTimeoutMillis: 5000,
ssl: false,
});
const connectDB = async () => {
if (!pool) {
return;
}
let client;
try {
client = await pool.connect();
} catch (err) {
console.log(err);
throw err;
}
console.log(`postgres connected to ${client.database} on port 5432`);
client.release();
};
const disconnectDb = () => {
if (!pool) {
return;
}
pool.end();
};
module.exports = {
connectDB,
disconnectDb,
query: (...args) => {
if (!pool) {
return;
}
return pool.query.apply(pool, args);
},
};