UNPKG

@ckstack/ck-lib-sequelize

Version:

sequelize helper module

763 lines 57.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const sequelize_typescript_1 = require("sequelize-typescript"); const sequelize_1 = require("sequelize"); const fs = __importStar(require("fs")); const _ = __importStar(require("underscore")); const path = __importStar(require("path")); const JSON5 = __importStar(require("json5")); const tunnel_ssh_1 = __importDefault(require("tunnel-ssh")); const dk_lib_1 = require("@dkpkg/dk-lib"); const CkDBType_1 = require("./CkDBType"); const MyValues_1 = require("./MyValues"); const _rdb = []; const operatorsAliases = { $eq: sequelize_1.Op.eq, $ne: sequelize_1.Op.ne, $gte: sequelize_1.Op.gte, $gt: sequelize_1.Op.gt, $lte: sequelize_1.Op.lte, $lt: sequelize_1.Op.lt, $not: sequelize_1.Op.not, $in: sequelize_1.Op.in, $notIn: sequelize_1.Op.notIn, $is: sequelize_1.Op.is, $like: sequelize_1.Op.like, $notLike: sequelize_1.Op.notLike, $iLike: sequelize_1.Op.iLike, $notILike: sequelize_1.Op.notILike, $regexp: sequelize_1.Op.regexp, $notRegexp: sequelize_1.Op.notRegexp, $iRegexp: sequelize_1.Op.iRegexp, $notIRegexp: sequelize_1.Op.notIRegexp, $between: sequelize_1.Op.between, $notBetween: sequelize_1.Op.notBetween, $overlap: sequelize_1.Op.overlap, $contains: sequelize_1.Op.contains, $contained: sequelize_1.Op.contained, $adjacent: sequelize_1.Op.adjacent, $strictLeft: sequelize_1.Op.strictLeft, $strictRight: sequelize_1.Op.strictRight, $noExtendRight: sequelize_1.Op.noExtendRight, $noExtendLeft: sequelize_1.Op.noExtendLeft, $and: sequelize_1.Op.and, $or: sequelize_1.Op.or, $any: sequelize_1.Op.any, $all: sequelize_1.Op.all, $values: sequelize_1.Op.values, $col: sequelize_1.Op.col, }; class CkSequelize { static __connectByTunnel(config) { return new Promise((resolve, reject) => { (0, tunnel_ssh_1.default)(config, (err) => { if (err) { dk_lib_1.CkLogger.errLog2(`can't connect by tunnel. err [${err.toString()}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); reject(err); return; } resolve(); }); }); } static connect(dbInfo, tunnelDbInfo) { var _a, e_1, _b, _c, _d, e_2, _e, _f, _g, e_3, _h, _j, _k, e_4, _l, _m; return __awaiter(this, void 0, void 0, function* () { let idx = 0; if (dbInfo && dbInfo.length > 0) { CkSequelize.DB_INFO = dbInfo.slice(0); try { for (var _o = true, _p = __asyncValues(CkSequelize.DB_INFO), _q; _q = yield _p.next(), _a = _q.done, !_a;) { _c = _q.value; _o = false; try { const element = _c; if (_rdb[idx]) { dk_lib_1.CkLogger.warnLog2(`already connected rdb. index [${idx}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); return; } const modelAbsPath = element.MODEL_ABS_PATH; const nModel = Number(modelAbsPath); const modelTarget = []; if (nModel === CkDBType_1.TABLE_TYPE.CORE_MODEL) { const modelPath = path.join(__dirname, '../../../ck-lib-models/dist/core'); dk_lib_1.CkLogger.debugLog2(`used ck-lib-models/core package. model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath)) { dk_lib_1.CkLogger.debugLog2(`found ck-lib-models/core model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath); } else { dk_lib_1.CkLogger.errLog2(`not found ck-lib-models/core model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } } else if (nModel === CkDBType_1.TABLE_TYPE.BOARD_MODEL) { const modelPath = path.join(__dirname, '../../../ck-lib-models/dist/board'); dk_lib_1.CkLogger.debugLog2(`used ck-lib-models/board package. model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath)) { dk_lib_1.CkLogger.debugLog2(`found ck-lib-models/board model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath); } else { dk_lib_1.CkLogger.errLog2(`not found ck-lib-models/board model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } } else if (nModel === CkDBType_1.TABLE_TYPE.CORE_AND_BOARD_MODEL) { const modelPath1 = path.join(__dirname, '../../../ck-lib-models/dist/core'); dk_lib_1.CkLogger.debugLog2(`used ck-lib-models/core package. model directory [${modelPath1}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath1)) { dk_lib_1.CkLogger.debugLog2(`found ck-lib-models/core model directory [${modelPath1}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath1); } else { dk_lib_1.CkLogger.errLog2(`not found ck-lib-models/core model directory [${modelPath1}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } const modelPath2 = path.join(__dirname, '../../../ck-lib-models/dist/board'); dk_lib_1.CkLogger.debugLog2(`used ck-lib-models/board package. model directory [${modelPath2}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath2)) { dk_lib_1.CkLogger.debugLog2(`found ck-lib-models/board model directory [${modelPath2}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath2); } else { dk_lib_1.CkLogger.errLog2(`not found ck-lib-models/board model directory [${modelPath2}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } } else if (nModel === CkDBType_1.TABLE_TYPE.PUSH_TABLE_MODEL) { const modelPath = path.join(__dirname, '../../../ck-lib-models/dist/core'); dk_lib_1.CkLogger.debugLog2(`used ck-push-cron package. default model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath)) { dk_lib_1.CkLogger.debugLog2(`found ck-push-cron default model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath); } else { dk_lib_1.CkLogger.errLog2(`not found ck-push-cron default model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } } else if (nModel === CkDBType_1.TABLE_TYPE.PUSH_TABLE_INFOMATION_SCHEMA_MODEL) { const modelPath = path.join(__dirname, '../../../ck-lib-models/dist/information_schema'); dk_lib_1.CkLogger.debugLog2(`used ck-push-cron package. information model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath)) { dk_lib_1.CkLogger.debugLog2(`found ck-push-cron information model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath); } else { dk_lib_1.CkLogger.errLog2(`not found ck-push-cron information model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } } else if (modelAbsPath) { modelTarget.push(modelAbsPath); } if (element.OPT_MODEL_ABS_PATH) { for (const row of element.OPT_MODEL_ABS_PATH) { modelTarget.push(row); } } if (modelTarget.length <= 0) { dk_lib_1.CkLogger.errLog2(`not found sequelize model directory [${element.MODEL_ABS_PATH}], modelTarget [${JSON5.stringify(modelTarget)}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); throw new dk_lib_1.CkError(MyValues_1.PROC_LHD.IP_LHD_HTTP, dk_lib_1.CkValue.ERROR_TYPE.rdb_connection, 500); } try { for (var _r = true, modelTarget_1 = (e_2 = void 0, __asyncValues(modelTarget)), modelTarget_1_1; modelTarget_1_1 = yield modelTarget_1.next(), _d = modelTarget_1_1.done, !_d;) { _f = modelTarget_1_1.value; _r = false; try { const row1 = _f; if (!fs.existsSync(row1)) { dk_lib_1.CkLogger.errLog2(`not found sequelize model directory [${element.MODEL_ABS_PATH}], row1 [${row1}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); throw new dk_lib_1.CkError(MyValues_1.PROC_LHD.IP_LHD_HTTP, dk_lib_1.CkValue.ERROR_TYPE.rdb_connection, 500); } } finally { _r = true; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (!_r && !_d && (_e = modelTarget_1.return)) yield _e.call(modelTarget_1); } finally { if (e_2) throw e_2.error; } } const opt = { dialect: element.DIALECT, benchmark: element.SEQUELIZE_BENCH, pool: { max: element.POOL_MAX_COUNT, min: element.POOL_MIN_COUNT || 2, acquire: element.POOL_ACQUIRE, idle: element.POOL_IDLE, evict: element.POOL_EVICT || 1000, }, operatorsAliases, dialectOptions: { decimalNumbers: true, maxPreparedStatements: 100, }, models: modelTarget, hooks: { beforeConnect(config) { dk_lib_1.CkLogger.debugLog2(`database connect hook. before database connect. host [${config.host}], port [${config.port}], database [${config.database}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); }, afterConnect(connection, config) { const threadId = connection ? connection.threadId : 'unknown'; dk_lib_1.CkLogger.debugLog2(`database connect hook. after database connect. host [${config.host}], port [${config.port}], database [${config.database}], threadId [${threadId}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); }, beforeDisconnect(connection) { const threadId = connection ? connection.threadId : 'unknown'; dk_lib_1.CkLogger.debugLog2(`database connect hook. before disconnect. threadId [${threadId}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); }, afterDisconnect(connection) { const threadId = connection ? connection.threadId : 'unknown'; dk_lib_1.CkLogger.debugLog2(`database connect hook. after disconnect. threadId [${threadId}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); }, }, }; if (element.HOST) { opt.host = element.HOST; } if (element.PORT) { opt.port = element.PORT; } if (element.SCHEMA) { opt.database = element.SCHEMA; } if (element.USER) { opt.username = element.USER; } if (element.PASSWORD) { opt.password = element.PASSWORD; } if (element.TIMEZONE) { opt.timezone = element.TIMEZONE; } if (element.DIALECT === 'mysql') { opt.dialectOptions = Object.assign({ charset: 'utf8_general_ci' }, opt.dialectOptions); } if (element.SEQUELIZE_LOGGING) { opt.logging = (msg) => dk_lib_1.CkLogger.debugLog(`${msg || ''}`, '[rdb-query:]'); } else { opt.logging = false; } if (element.REPLICA) { const replication = { read: [], write: {} }; if (_.size(element.REPLICA.READ) > 0) { for (const row of element.REPLICA.READ) { const readElement = {}; if (row.HOST) { readElement.host = row.HOST; } if (row.PORT) { readElement.port = row.PORT; } if (row.USER) { readElement.username = row.USER; } if (row.PASSWORD) { readElement.password = row.PASSWORD; } if (row.SCHEMA) { readElement.database = row.SCHEMA; } replication.read.push(readElement); } } if (_.size(element.REPLICA.WRITE) > 0) { if (element.REPLICA.WRITE.HOST) { replication.write.host = element.REPLICA.WRITE.HOST; } if (element.REPLICA.WRITE.PORT) { replication.write.port = element.REPLICA.WRITE.PORT; } if (element.REPLICA.WRITE.USER) { replication.write.username = element.REPLICA.WRITE.USER; } if (element.REPLICA.WRITE.PASSWORD) { replication.write.password = element.REPLICA.WRITE.PASSWORD; } if (element.REPLICA.WRITE.SCHEMA) { replication.write.database = element.REPLICA.WRITE.SCHEMA; } } if (_.size(replication.read) > 0 && _.size(replication.write) > 0) { opt.replication = replication; } } dk_lib_1.CkLogger.infoLog2(`try connect to database. opt [${JSON5.stringify(opt)}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); _rdb[idx] = new sequelize_typescript_1.Sequelize(opt); idx += 1; } finally { _o = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_o && !_a && (_b = _p.return)) yield _b.call(_p); } finally { if (e_1) throw e_1.error; } } } if (tunnelDbInfo && tunnelDbInfo.length > 0) { CkSequelize.TUNNEL_DB_INFO = tunnelDbInfo.slice(0); try { for (var _s = true, _t = __asyncValues(CkSequelize.TUNNEL_DB_INFO), _u; _u = yield _t.next(), _g = _u.done, !_g;) { _j = _u.value; _s = false; try { const element = _j; if (_rdb[idx]) { dk_lib_1.CkLogger.warnLog2(`already connected proxy rdb. index [${idx}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); return; } const modelAbsPath = element.MODEL_ABS_PATH; const nModel = Number(modelAbsPath); const modelTarget = []; if (nModel === CkDBType_1.TABLE_TYPE.CORE_MODEL) { const modelPath = path.join(__dirname, '../../../ck-lib-models/dist/core'); dk_lib_1.CkLogger.debugLog2(`used ck-lib-models/core package. model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath)) { dk_lib_1.CkLogger.debugLog2(`found ck-lib-models/core model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath); } else { dk_lib_1.CkLogger.errLog2(`not found ck-lib-models/core model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } } else if (nModel === CkDBType_1.TABLE_TYPE.BOARD_MODEL) { const modelPath = path.join(__dirname, '../../../ck-lib-models/dist/board'); dk_lib_1.CkLogger.debugLog2(`used ck-lib-models/board package. model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath)) { dk_lib_1.CkLogger.debugLog2(`found ck-lib-models/board model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath); } else { dk_lib_1.CkLogger.errLog2(`not found ck-lib-models/board model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } } else if (nModel === CkDBType_1.TABLE_TYPE.CORE_AND_BOARD_MODEL) { const modelPath1 = path.join(__dirname, '../../../ck-lib-models/dist/core'); dk_lib_1.CkLogger.debugLog2(`used ck-lib-models/core package. model directory [${modelPath1}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath1)) { dk_lib_1.CkLogger.debugLog2(`found ck-lib-models/core model directory [${modelPath1}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath1); } else { dk_lib_1.CkLogger.errLog2(`not found ck-lib-models/core model directory [${modelPath1}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } const modelPath2 = path.join(__dirname, '../../../ck-lib-models/dist/board'); dk_lib_1.CkLogger.debugLog2(`used ck-lib-models/board package. model directory [${modelPath2}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath2)) { dk_lib_1.CkLogger.debugLog2(`found ck-lib-models/board model directory [${modelPath2}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath2); } else { dk_lib_1.CkLogger.errLog2(`not found ck-lib-models/board model directory [${modelPath2}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } } else if (nModel === CkDBType_1.TABLE_TYPE.PUSH_TABLE_MODEL) { const modelPath = path.join(__dirname, '../../../ck-lib-models/dist/core'); dk_lib_1.CkLogger.debugLog2(`used ck-push-cron package. default model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath)) { dk_lib_1.CkLogger.debugLog2(`found ck-push-cron default model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath); } else { dk_lib_1.CkLogger.errLog2(`not found ck-push-cron default model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } } else if (nModel === CkDBType_1.TABLE_TYPE.PUSH_TABLE_INFOMATION_SCHEMA_MODEL) { const modelPath = path.join(__dirname, '../../../ck-lib-models/dist/information_schema'); dk_lib_1.CkLogger.debugLog2(`used ck-push-cron package. information model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); if (fs.existsSync(modelPath)) { dk_lib_1.CkLogger.debugLog2(`found ck-push-cron information model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); modelTarget.push(modelPath); } else { dk_lib_1.CkLogger.errLog2(`not found ck-push-cron information model directory [${modelPath}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); } } else if (modelAbsPath) { modelTarget.push(modelAbsPath); } if (element.OPT_MODEL_ABS_PATH) { for (const row of element.OPT_MODEL_ABS_PATH) { modelTarget.push(row); } } if (modelTarget.length <= 0) { dk_lib_1.CkLogger.errLog2(`not found sequelize model directory [${element.MODEL_ABS_PATH}], modelTarget [${JSON5.stringify(modelTarget)}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); throw new dk_lib_1.CkError(MyValues_1.PROC_LHD.IP_LHD_HTTP, dk_lib_1.CkValue.ERROR_TYPE.rdb_connection, 500); } try { for (var _v = true, modelTarget_2 = (e_4 = void 0, __asyncValues(modelTarget)), modelTarget_2_1; modelTarget_2_1 = yield modelTarget_2.next(), _k = modelTarget_2_1.done, !_k;) { _m = modelTarget_2_1.value; _v = false; try { const row1 = _m; if (!fs.existsSync(row1)) { dk_lib_1.CkLogger.errLog2(`not found sequelize model directory [${element.MODEL_ABS_PATH}], row1 [${row1}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); throw new dk_lib_1.CkError(MyValues_1.PROC_LHD.IP_LHD_HTTP, dk_lib_1.CkValue.ERROR_TYPE.rdb_connection, 500); } } finally { _v = true; } } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (!_v && !_k && (_l = modelTarget_2.return)) yield _l.call(modelTarget_2); } finally { if (e_4) throw e_4.error; } } const opt = { host: element.HOST, port: element.PORT, dialect: element.DIALECT, benchmark: element.SEQUELIZE_BENCH, pool: { max: element.POOL_MAX_COUNT, min: element.POOL_MIN_COUNT || 2, acquire: element.POOL_ACQUIRE, idle: element.POOL_IDLE, evict: element.POOL_EVICT || 1000, }, operatorsAliases, dialectOptions: { decimalNumbers: true, maxPreparedStatements: 100, }, models: modelTarget, timezone: element.TIMEZONE || '+09:00', hooks: { beforeConnect(config) { dk_lib_1.CkLogger.debugLog2(`database connect hook. before database connect. host [${config.host}], port [${config.port}], database [${config.database}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); }, afterConnect(connection, config) { const threadId = connection ? connection.threadId : 'unknown'; dk_lib_1.CkLogger.debugLog2(`database connect hook. after database connect. host [${config.host}], port [${config.port}], database [${config.database}], threadId [${threadId}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); }, beforeDisconnect(connection) { const threadId = connection ? connection.threadId : 'unknown'; dk_lib_1.CkLogger.debugLog2(`database connect hook. before disconnect. threadId [${threadId}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); }, afterDisconnect(connection) { const threadId = connection ? connection.threadId : 'unknown'; dk_lib_1.CkLogger.debugLog2(`database connect hook. after disconnect. threadId [${threadId}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); }, }, }; if (element.DIALECT === 'mysql') { opt.dialectOptions = Object.assign({ charset: 'utf8_general_ci' }, opt.dialectOptions); } if (element.SEQUELIZE_LOGGING) { opt.logging = (msg) => dk_lib_1.CkLogger.debugLog(`${msg || ''}`, '[rdb-query:]'); } else { opt.logging = false; } dk_lib_1.CkLogger.infoLog2(`try connect to proxy database. dialect [${element.DIALECT}], host [${element.HOST}], port [${element.PORT}], db [${element.SCHEMA}], user [${element.USER}], opt [${JSON5.stringify(opt)}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); _rdb[idx] = new sequelize_typescript_1.Sequelize(element.SCHEMA || 'mydb', element.USER || 'user', element.PASSWORD, opt); const tunnelConfig = { host: element.TUNNEL_SSH_HOST, port: element.TUNNEL_SSH_PORT, username: element.TUNNEL_SSH_USER_ID, dstHost: element.TUNNEL_DATABASE_HOST, dstPort: element.TUNNEL_DATABASE_PORT, localHost: opt.host, localPort: opt.port, }; let validPassword = true; if (element.TUNNEL_SSH_USER_PASSWORD) { tunnelConfig.password = element.TUNNEL_SSH_USER_PASSWORD; } else if (element.TUNNEL_SSH_USER_PASSWORD_PEM) { try { tunnelConfig.privateKey = fs.readFileSync(element.TUNNEL_SSH_USER_PASSWORD_PEM); } catch (err) { dk_lib_1.CkLogger.errLog2(`can't connect to proxy database. invalid password. tunnel info [${JSON5.stringify(tunnelConfig)}], error [${err.toString()}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); validPassword = false; } } if (validPassword) { try { yield CkSequelize.__connectByTunnel(tunnelConfig); } catch (err) { dk_lib_1.CkLogger.errLog2(`can't connect to proxy database. tunnel info [${JSON5.stringify(tunnelConfig)}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); throw err; } } idx += 1; } finally { _s = true; } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (!_s && !_g && (_h = _t.return)) yield _h.call(_t); } finally { if (e_3) throw e_3.error; } } } }); } static close() { var _a, e_5, _b, _c; return __awaiter(this, void 0, void 0, function* () { if (_rdb.length > 0) { let idx = 0; try { for (var _d = true, _rdb_1 = __asyncValues(_rdb), _rdb_1_1; _rdb_1_1 = yield _rdb_1.next(), _a = _rdb_1_1.done, !_a;) { _c = _rdb_1_1.value; _d = false; try { const rdb = _c; yield rdb.close(); dk_lib_1.CkLogger.infoLog2(`close database. index [${idx}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); idx += 1; } finally { _d = true; } } } catch (e_5_1) { e_5 = { error: e_5_1 }; } finally { try { if (!_d && !_a && (_b = _rdb_1.return)) yield _b.call(_rdb_1); } finally { if (e_5) throw e_5.error; } } } }); } static connectTest() { var _a, e_6, _b, _c; return __awaiter(this, void 0, void 0, function* () { dk_lib_1.CkLogger.infoLog2('try checking connect to database', MyValues_1.PROC_LHD.IP_LHD_HTTP); if (_.size(_rdb) <= 0) { dk_lib_1.CkLogger.errLog2('It is not connected to any RDB.', MyValues_1.PROC_LHD.IP_LHD_HTTP); return false; } let result = true; try { for (var _d = true, _rdb_2 = __asyncValues(_rdb), _rdb_2_1; _rdb_2_1 = yield _rdb_2.next(), _a = _rdb_2_1.done, !_a;) { _c = _rdb_2_1.value; _d = false; try { const rdb = _c; yield rdb.authenticate() .then(() => { dk_lib_1.CkLogger.infoLog2(`success connect to database. options [${JSON5.stringify(rdb.options)}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); }) .catch((err) => { dk_lib_1.CkLogger.errLog2(`Unable to connection the database. options [${JSON5.stringify(rdb.options)}], err [${err}]`, MyValues_1.PROC_LHD.IP_LHD_HTTP); result = false; }); } finally { _d = true; } } } catch (e_6_1) { e_6 = { error: e_6_1 }; } finally { try { if (!_d && !_a && (_b = _rdb_2.return)) yield _b.call(_rdb_2); } finally { if (e_6) throw e_6.error; } } dk_lib_1.CkLogger.infoLog2('complete checking connect to database', MyValues_1.PROC_LHD.IP_LHD_HTTP); return result; }); } static getOrder(order, sort) { const _order = order; _.each(sort, (row) => { _order.push([row.column, row.dir]); }); return _order; } static parseSearchText(reqIpAndLhdAndHttpInfo, where, dbOrigColumnName, sequelizeColumnName, columnValue, isAndCond) { const lhd = reqIpAndLhdAndHttpInfo || dk_lib_1.CkValue.getDefaultLogKey(true); const _where = where || { $or: [] }; if (_.size(columnValue) > 0) { dk_lib_1.CkLogger.debugLog2(`parseSearchText. ${sequelizeColumnName} filter exist. ${sequelizeColumnName} filter [${JSON5.stringify(columnValue)}]`, lhd); if (columnValue.used && columnValue.used.length > 0) { const key = isAndCond ? '$and' : '$or'; if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.EQUAL) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.EQUAL]) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push({ $eq: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.EQUAL] }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.FULL_TEXT) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.FULL_TEXT]) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push(sequelize_typescript_1.Sequelize.literal(`MATCH (${dbOrigColumnName}) AGAINST ('${columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.FULL_TEXT]}' IN BOOLEAN MODE)`)); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.RLIKE) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.RLIKE]) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push({ $like: `${columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.RLIKE]}%` }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.LIKE) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.LIKE]) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push({ $like: `%${columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.LIKE]}%` }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.IN) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.IN]) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push({ $in: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.IN] }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.START) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.START]) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push({ $gte: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.START] }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.START_NE) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.START_NE]) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push({ $gt: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.START_NE] }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.END) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.END]) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push({ $lte: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.END] }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.END_NE) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.END_NE]) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push({ $lt: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.END_NE] }); } } return _where; } return _where; } static parseSearchText2(reqIpAndLhdAndHttpInfo, where, colName, columnValue, isAndCond) { const lhd = reqIpAndLhdAndHttpInfo || dk_lib_1.CkValue.getDefaultLogKey(true); const _where = where || { $or: [] }; if (_.size(columnValue) > 0) { dk_lib_1.CkLogger.debugLog2(`parseSearchText2. ${colName} filter exist. ${colName} filter [${JSON5.stringify(columnValue)}]`, lhd); if (columnValue.used && columnValue.used.length > 0) { const key = isAndCond ? '$and' : '$or'; if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.EQUAL) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.EQUAL]) { _where[colName] = _where[colName] || { [key]: [] }; _where[colName][key].push({ $eq: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.EQUAL] }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.FULL_TEXT) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.FULL_TEXT]) { _where[colName] = _where[colName] || { [key]: [] }; _where[colName][key].push(sequelize_typescript_1.Sequelize.literal(`MATCH (${colName}) AGAINST ('${colName}' IN BOOLEAN MODE)`)); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.RLIKE) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.RLIKE]) { _where[colName] = _where[colName] || { [key]: [] }; _where[colName][key].push({ $like: `${columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.RLIKE]}%` }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.LIKE) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.LIKE]) { _where[colName] = _where[colName] || { [key]: [] }; _where[colName][key].push({ $like: `%${columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.LIKE]}%` }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.IN) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.IN]) { _where[colName] = _where[colName] || { [key]: [] }; const _arr = columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.IN]; if (_arr.length === 1) { _where[colName][key].push({ $eq: _arr[0] }); } else { _where[colName][key].push({ $in: _arr }); } } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.START) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.START]) { _where[colName] = _where[colName] || { [key]: [] }; _where[colName][key].push({ $gte: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.START] }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.START_NE) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.START_NE]) { _where[colName] = _where[colName] || { [key]: [] }; _where[colName][key].push({ $gt: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.START_NE] }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.END) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.END]) { _where[colName] = _where[colName] || { [key]: [] }; _where[colName][key].push({ $lte: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.END] }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_TEXT_SEARCH.END_NE) && columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.END_NE]) { _where[colName] = _where[colName] || { [key]: [] }; _where[colName][key].push({ $lt: columnValue[dk_lib_1.CkValue.SQL_TEXT_SEARCH.END_NE] }); } } return _where; } return _where; } static parseSearchNumber(reqIpAndLhdAndHttpInfo, where, sequelizeColumnName, columnValue, isAndCond) { const lhd = reqIpAndLhdAndHttpInfo || dk_lib_1.CkValue.getDefaultLogKey(true); const _where = where || { $and: [] }; if (_.size(columnValue) > 0) { dk_lib_1.CkLogger.debugLog2(`parseSearchNumber. ${sequelizeColumnName} filter exist. ${sequelizeColumnName} filter [${JSON5.stringify(columnValue)}]`, lhd); const key = isAndCond ? '$and' : '$or'; if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_NUMBER_SEARCH.EQUAL) && columnValue[dk_lib_1.CkValue.SQL_NUMBER_SEARCH.EQUAL] !== undefined && columnValue[dk_lib_1.CkValue.SQL_NUMBER_SEARCH.EQUAL] !== null && !Number.isNaN(columnValue[dk_lib_1.CkValue.SQL_NUMBER_SEARCH.EQUAL])) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push({ $eq: columnValue[dk_lib_1.CkValue.SQL_NUMBER_SEARCH.EQUAL] }); } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_NUMBER_SEARCH.IN) && columnValue[dk_lib_1.CkValue.SQL_NUMBER_SEARCH.IN]) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; const _arr = columnValue[dk_lib_1.CkValue.SQL_NUMBER_SEARCH.IN]; if (_arr.length === 1) { _where[sequelizeColumnName][key].push({ $eq: _arr[0] }); } else { _where[sequelizeColumnName][key].push({ $in: _arr }); } } if (_.contains(columnValue.used, dk_lib_1.CkValue.SQL_NUMBER_SEARCH.START) && columnValue[dk_lib_1.CkValue.SQL_NUMBER_SEARCH.START] !== undefined && columnValue[dk_lib_1.CkValue.SQL_NUMBER_SEARCH.START] !== null && !Number.isNaN(columnValue[dk_lib_1.CkValue.SQL_NUMBER_SEARCH.START])) { _where[sequelizeColumnName] = _where[sequelizeColumnName] || { [key]: [] }; _where[sequelizeColumnName][key].push({ $gte: columnValue[dk_lib_1.CkValue.SQL_N