forked from DefiLlama/yield-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbConnection.js
executable file
·38 lines (33 loc) · 1.07 KB
/
dbConnection.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
const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '../../config.env') });
const pgp = require('pg-promise')({
/* initialization options */
capSQL: true, // capitalize all generated SQL
});
// set type options (pg-promise returns integers and numeric types as strings)
// id 20 = INTEGER
// id 1700 = NUMERIC
pgp.pg.types.setTypeParser(20, parseInt);
pgp.pg.types.setTypeParser(1700, parseFloat);
// on first connect, cache db connection for reuse so we don't
// need to connect on new requests
let conn = null;
const connect = async () => {
if (conn === null) {
console.log('using new db connection');
// set connection
conn = pgp({
connectionString: process.env.DATABASE_URL,
// max milliseconds a client can go unused before it is removed
// from the connection pool and destroyed.
// overriding default of 30sec to 60sec to decrease nb of potential reconnects of 1 lambda
// running multiple adapters
idleTimeoutMillis: 60000,
});
}
return conn;
};
module.exports = {
pgp,
connect,
};