UNPKG

@smallprod/models

Version:
118 lines (117 loc) 4.54 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const defaultConfig = { migrationPath: 'database/migrations', }; class DbManager { constructor() { this.config = defaultConfig; this.dbs = []; this.setConfig = (config) => { this.config = Object.assign(Object.assign({}, this.config), config); }; this.getConfig = () => this.config; this.add = async (dbms, host, port, user, password, database, name = '', debug = false) => { switch (dbms) { case 'mariadb': { const GlobalMariaModel = (await Promise.resolve().then(() => __importStar(require('./global/maria.db')))).default; const ndb = new GlobalMariaModel(debug); await ndb.setPool({ host, user, port, database, password, }); this.dbs.push({ name, db: ndb }); break; } case 'postgre': { const GlobalPostgreModel = (await Promise.resolve().then(() => __importStar(require('./global/postgres.db')))) .default; const ndb = new GlobalPostgreModel(debug); await ndb.setPool({ host, port, user, password, database, }); this.dbs.push({ name, db: ndb }); break; } case 'oracle': { const GlobalOracleModel = (await Promise.resolve().then(() => __importStar(require('./global/oracle.db')))).default; const ndb = new GlobalOracleModel(debug); await ndb.setPool({ user, password, connectString: `${host}:${port}/${database}`, }); this.dbs.push({ name, db: ndb }); break; } case 'mssql': { const GlobalMicrosoftModel = (await Promise.resolve().then(() => __importStar(require('./global/microsoft.db')))) .default; const ndb = new GlobalMicrosoftModel(debug); await ndb.setPool({ port, user, password, database, server: host, }); this.dbs.push({ name, db: ndb }); break; } default: { console.error('Unknown sgbd'); } } }; this.get = (name = null) => { if (!this.dbs.length) return null; if (!name) return this.dbs[0].db; const db = this.dbs.find((d) => d.name === name); if (db) return db.db; return null; }; this.clear = async () => { await this.dbs.reduce(async (previous, current) => { await previous; await current.db.disconnect(); }, Promise.resolve()); this.dbs = []; }; } } exports.default = DbManager; DbManager.getInstance = () => { if (!DbManager._instance) { DbManager._instance = new DbManager(); } return DbManager._instance; };