{"size": 1625, "ext": "js", "max_stars_repo_path": "app/communications/dbClient.js", "max_stars_repo_name": "ogarciarevett/rappi-test-be", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/communications/dbClient.js", "max_issues_repo_name": "ogarciarevett/rappi-test-be", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/communications/dbClient.js", "max_forks_repo_name": "ogarciarevett/rappi-test-be", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.390625, "max_line_length": 74, "alphanum_fraction": 0.5741538462}
const knex = require('knex');
const pgConfig = require('config').get('postgresql');

let isolatedKnexConnections = [];
let singletonConnection = null;

const dbConfig = {
    client: 'pg',
    version: '7.2',
    connection: {
        user: pgConfig.user,
        password: pgConfig.password,
        host: pgConfig.host,
        port: pgConfig.port,
        database: pgConfig.database,
    },
    pool: {
        min: pgConfig.minPool,
        max: pgConfig.maxPool,
    },
};

class dbClient {
    newConnection() {
        return knex(dbConfig);
    }

    getConnection() {
        // TODO:set connection based on environment. Testing DB
        if (!singletonConnection) {
            singletonConnection = this.newConnection();
        }
        return singletonConnection;
    }

    createConnection(shouldIsolateKnexConnection) {
        // eslint-disable-next-line
        let knex;
        if (shouldIsolateKnexConnection) {
            knex = this.newConnection();
            isolatedKnexConnections.push(knex);
        } else {
            knex = this.getConnection();
        }
        return knex;
    }

    closeAllIsolated() {
        console.log(
            `Closing ${isolatedKnexConnections.length} Knex Connenctions`,
        );
        const promises = [];
        for (let i = 0; i < isolatedKnexConnections.length; i++) {
            promises.push(isolatedKnexConnections[i].destroy());
        }
        isolatedKnexConnections = [];
        return Promise.all(promises).catch(err => {
            console.log('ERRRR', err);
            return err;
        });
    }
}
module.exports = dbClient;
