-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
35 lines (31 loc) · 1009 Bytes
/
database.js
File metadata and controls
35 lines (31 loc) · 1009 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
const _ = require('lodash')
const { credentials } = require('./config')
const pg = require('pg');
const connectionString = credentials.postgres.connectionString;
var pool;
const camelizeKeys = (obj) => {
if (!_.isObject(obj)) {
return obj;
}
if (_.isArray(obj)) {
return obj.map(camelizeKeys);
}
if (obj instanceof Date) {
return obj;
}
// nested calls here. IF we are dealing with objects, we have to check the values and the keys
// we ofc have to change the keys to be camelized and if the values are objects or arrays,
// those have to be camelized as well
return _.mapValues( _.mapKeys(obj, (value, key) => _.camelCase(key)), (value, key) => {
return _.isObject(value) || _.isArray(value) ? camelizeKeys(value) : value;
});
};
module.exports = {
getPool: () => {
if (pool) return pool; // if it is already there, grab it here
pool = new pg.Pool({connectionString});
return pool;
},
camelize: (rows) => {
return rows.map(camelizeKeys)
}}