UNPKG

lia-mysql

Version:

JavaScript library of data standards.

96 lines 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MysqlInstaller = void 0; const base_1 = require("./base"); const mysql2_1 = require("mysql2"); const lang_1 = require("../lang"); class MysqlInstaller extends base_1.BaseInstaller { /** * * @param configs * @param target mysql * @param debug debug model */ constructor(configs, target, debug = false) { target = target ? target : global; if (!target.__SQL_CACHE) { target.__SQL_CACHE = {}; } if (!target.SQLS) { target.SQLS = {}; } let mul = !(configs instanceof lang_1.Config); if (mul) { if (configs.uri || configs.host) { mul = false; } } super('MYSQL', target, debug, mul); this.configs = configs; } async load() { await super.load(); return this; } async install() { if (this.multiple) { for (const key in this.configs) { await this.createClient(this.configs[key], key); } } else { await this.createClient(this.configs, 'default'); } } querySql(connect, sql, params) { return new Promise((resolve, reject) => { connect.query(sql, params, (error, result) => { if (error) { reject(error); } else { resolve(result); } }); }); } /** * @private * @param name */ _matchName(name) { return name.toUpperCase(); } createClient(options, name) { name = this._matchName(name); const id = this.randomStr(); const config = options; // eslint-disable-next-line @typescript-eslint/no-this-alias const _this = this; return new Promise((resolve, reject) => { // 使用连接池,提升性能 let pool1 = (0, mysql2_1.createPool)(config); // const pool = pool1.promise(); pool1.on('acquire', (connection) => { _this.logSys(`client[ ${id} ]: acquire`); }); pool1.on('connection', (connection) => { _this.logSys(`client[ ${id} ]: connection`); }); pool1.on('enqueue', () => { _this.logSys(`client[ ${id} ]: enqueue`); }); pool1.on('release', (connection) => { _this.logSys(`client[ ${id} ]: release`); }); let client = new lang_1.Client(pool1); if (name === 'DEFAULT') { _this._target.SQL = client; } _this._target.SQLS[name] = client; resolve(client); }); } } exports.MysqlInstaller = MysqlInstaller; //# sourceMappingURL=mysql.js.map