All files / database/connection index.js

100% Statements 17/17
100% Branches 4/4
100% Functions 3/3
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  94x 94x 94x 94x 94x   94x 94x     94x               94x 5781x 5781x 387x   5781x   5781x 5781x       94x   40x    
// following this example https://node-postgres.com/guides/project-structure
const log = require("../../util/log");
const pg = require('pg');
const pgCamelCase = require('pg-camelcase');
const {Pool} = pg;
const types = pg.types;
 
pgCamelCase.inject(pg);
types.setTypeParser(1700, 'text', parseFloat); // converts Postgres numeric types to js Numbers
 
 
const pool = new Pool({
    user: process.env.DB_USER,
    host: process.env.DB_HOST,
    database: process.env.DB_DATABASE,
    password: process.env.DB_PASS,
    port: 5432,
});
 
const query = (text, params, callback) => {
    log.debug(">>> %s", text);
    if (params && params.length > 0) {
        log.debug(">>> %s", params);
    }
    return pool.query(text, params, callback)
        .then(res => {
            log.debug("<<< %s", JSON.stringify(res.rows));
            return res;
        });
};
 
module.exports = {
    query,
    end: () => pool.end(),
};