"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDbStoreClass = exports.getDbConfig = exports.dbList = void 0; const oak_db_1 = require("oak-db"); const path_1 = require("path"); const fs_1 = require("fs"); /** * 数据库优先级列表,按顺序尝试获取配置文件 */ exports.dbList = { mysql: oak_db_1.MysqlStore, postgres: oak_db_1.PostgreSQLStore }; let usedDbType = null; const getDbConfig = (path) => { for (const db of Object.keys(exports.dbList)) { try { // eslint-disable-next-line @typescript-eslint/no-var-requires let dbConfigFile = (0, path_1.join)(path, 'configuration', `${db}.${process.env.NODE_ENV}.json`); if ((0, fs_1.existsSync)(dbConfigFile) === false) { dbConfigFile = (0, path_1.join)(path, 'configuration', `${db}.json`); } if ((0, fs_1.existsSync)(dbConfigFile) === false) { continue; } const config = require(dbConfigFile); console.log(`使用${db}作为数据库`); usedDbType = db; return Object.assign({}, config); } catch (err) { // do nothing } } throw new Error(`没有找到数据库配置文件,请在configuration目录下添加任一配置文件:${Object.keys(exports.dbList).map(ele => `${ele}.json`).join('、')}`); }; exports.getDbConfig = getDbConfig; const getDbStoreClass = () => { const dbType = usedDbType || (() => { throw new Error('无法确定数据库类型'); })(); const DbStoreClass = exports.dbList[dbType.toLowerCase()]; if (!DbStoreClass) { throw new Error(`不支持的数据库类型:${dbType},请确认是否存在以下配置文件:${Object.keys(exports.dbList).map(ele => `${ele}.json`).join('、')}`); } return DbStoreClass; }; exports.getDbStoreClass = getDbStoreClass;