-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
90 lines (73 loc) · 2.52 KB
/
db.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* eslint-disable space-before-function-paren */
// per guidance from https://node-postgres.com/guides/project-structure
const pg = require("pg");
pg.types.setTypeParser(20, (value) => +value); // https://github.com/vitaly-t/pg-promise/wiki/BigInt
pg.types.setTypeParser(1700, (value) => +value);
const pool = new pg.Pool();
const tsdbPool = new pg.Pool({
host: process.env.TSDBHOST,
user: process.env.TSDBUSER,
password: process.env.TSDBPASSWORD,
database: process.env.TSDBDATABASE,
port: process.env.TSDBPORT,
});
module.exports = {
async query(text, params) {
return await pool.query(text, params);
},
async tsdbQuery(text, params) {
return await tsdbPool.query(text, params);
},
async getClient() {
const client = await pool.connect();
const query = client.query;
const release = client.release;
// set a timeout of 5 seconds, after which we will log this client's last query
const timeout = setTimeout(() => {
console.error("A client has been checked out for more than 5 seconds!");
console.error(
`The last executed query on this client was: ${client.lastQuery}`
);
}, 5000);
// monkey patch the query method to keep track of the last query executed
client.query = (...args) => {
client.lastQuery = args;
return query.apply(client, args);
};
client.release = () => {
// clear our timeout
clearTimeout(timeout);
// set the methods back to their old un-monkey-patched version
client.query = query;
client.release = release;
return release.apply(client);
};
return client;
},
async getTSDBClient() {
const client = await tsdbPool.connect();
const query = client.query;
const release = client.release;
// set a timeout of 5 seconds, after which we will log this client's last query
const timeout = setTimeout(() => {
console.error("A client has been checked out for more than 5 seconds!");
console.error(
`The last executed query on this client was: ${client.lastQuery}`
);
}, 5000);
// monkey patch the query method to keep track of the last query executed
client.query = (...args) => {
client.lastQuery = args;
return query.apply(client, args);
};
client.release = () => {
// clear our timeout
clearTimeout(timeout);
// set the methods back to their old un-monkey-patched version
client.query = query;
client.release = release;
return release.apply(client);
};
return client;
},
};