UNPKG

@starbemtech/star-db-query-builder

Version:

A query builder to be used with mysql or postgres

42 lines 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAllDbClients = exports.getDbClient = exports.initDb = void 0; const pg_1 = require("pg"); const promise_1 = require("mysql2/promise"); const pgClient_1 = require("./pgClient"); const mysqlClient_1 = require("./mysqlClient"); const dbClients = {}; const initDb = async (config) => { if (!config.type) throw new Error('Type is required. Accept values: pg | mysql'); if (!config.options) throw new Error('Connection options is required'); if (config.type === 'pg') { const poolConfig = config.options; const pool = new pg_1.Pool(config.options); dbClients[config.name] = await (0, pgClient_1.createPgClient)(pool, config.retryOptions, poolConfig); console.log(`@starbemtech/star-db-query-builder: Postgres db client "${config.name}" created successfully`); } else if (config.type === 'mysql') { const pool = (0, promise_1.createPool)(config.options); dbClients[config.name] = (0, mysqlClient_1.createMysqlClient)(pool, config.retryOptions); console.info(`@starbemtech/star-db-query-builder: Postgres db client "${config.name}" created successfully`); } else { throw new Error('Unsupported database type'); } }; exports.initDb = initDb; const getDbClient = (name) => { const client = dbClients[name]; if (!client) { throw new Error(`Database client "${name}" is not initialized`); } return client; }; exports.getDbClient = getDbClient; const getAllDbClients = () => { return dbClients; }; exports.getAllDbClients = getAllDbClients; //# sourceMappingURL=initDb.js.map