fix: 避免修改配置文件中的type
This commit is contained in:
parent
5f2af054fc
commit
bda6e43ca7
|
|
@ -5,7 +5,7 @@ const TriggerExecutor_1 = require("oak-domain/lib/store/TriggerExecutor");
|
|||
const RelationAuth_1 = require("oak-domain/lib/store/RelationAuth");
|
||||
const dbPriority_1 = require("./utils/dbPriority");
|
||||
const createDbStore = (storageSchema, contextBuilder, dbConfiguration, authDeduceRelationMap, selectFreeEntities = [], updateFreeDict = {}, onVolatileTrigger) => {
|
||||
const BaseStoreClass = (0, dbPriority_1.getDbStoreClass)(dbConfiguration);
|
||||
const BaseStoreClass = (0, dbPriority_1.getDbStoreClass)();
|
||||
// 动态创建继承类
|
||||
class DynamicDbStore extends BaseStoreClass {
|
||||
executor;
|
||||
|
|
|
|||
|
|
@ -13,4 +13,4 @@ export declare const dbList: {
|
|||
postgres: typeof PostgreSQLStore;
|
||||
};
|
||||
export declare const getDbConfig: (path: string) => DbConfiguration;
|
||||
export declare const getDbStoreClass: <ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>(dbConfig: DbConfiguration) => new (schema: StorageSchema<ED>, config: DbConfiguration) => AsyncRowStore<ED, Cxt> & CascadeStore<ED>;
|
||||
export declare const getDbStoreClass: <ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>() => new (schema: StorageSchema<ED>, config: DbConfiguration) => AsyncRowStore<ED, Cxt> & CascadeStore<ED>;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ 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");
|
||||
/**
|
||||
* 数据库优先级列表,按顺序尝试获取配置文件
|
||||
*/
|
||||
|
|
@ -10,14 +11,22 @@ 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
|
||||
const dbConfigFile = (0, path_1.join)(path, 'configuration', `${db}.json`);
|
||||
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}作为数据库`);
|
||||
return Object.assign({}, { type: db }, config);
|
||||
usedDbType = db;
|
||||
return Object.assign({}, config);
|
||||
}
|
||||
catch (err) {
|
||||
// do nothing
|
||||
|
|
@ -26,8 +35,10 @@ const getDbConfig = (path) => {
|
|||
throw new Error(`没有找到数据库配置文件,请在configuration目录下添加任一配置文件:${Object.keys(exports.dbList).map(ele => `${ele}.json`).join('、')}`);
|
||||
};
|
||||
exports.getDbConfig = getDbConfig;
|
||||
const getDbStoreClass = (dbConfig) => {
|
||||
const dbType = dbConfig.type || 'mysql';
|
||||
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('、')}`);
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ export const createDbStore = <ED extends EntityDict & BaseEntityDict, Cxt extend
|
|||
onVolatileTrigger?: <T extends keyof ED>(entity: T, trigger: VolatileTrigger<ED, T, Cxt>, ids: string[], cxtStr: string, option: OperateOption) => Promise<void>
|
||||
): AppDbStore<ED, Cxt> => {
|
||||
|
||||
const BaseStoreClass = getDbStoreClass<ED, Cxt>(dbConfiguration) as any
|
||||
const BaseStoreClass = getDbStoreClass<ED, Cxt>() as any
|
||||
|
||||
// 动态创建继承类
|
||||
class DynamicDbStore extends BaseStoreClass implements TriggerStore<ED, Cxt> {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import { EntityDict, StorageSchema, Trigger, Checker, SelectOption, SelectFreeEn
|
|||
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
||||
import { BackendRuntimeContext } from 'oak-frontend-base/lib/context/BackendRuntimeContext';
|
||||
import { CascadeStore } from "oak-domain/lib/store/CascadeStore";
|
||||
import { existsSync } from "fs";
|
||||
|
||||
/**
|
||||
* 数据库优先级列表,按顺序尝试获取配置文件
|
||||
|
|
@ -15,14 +16,25 @@ export const dbList = {
|
|||
postgres: PostgreSQLStore
|
||||
}
|
||||
|
||||
let usedDbType: string | null = null;
|
||||
|
||||
export const getDbConfig = (path: string): DbConfiguration => {
|
||||
for (const db of Object.keys(dbList)) {
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const dbConfigFile = join(path, 'configuration', `${db}.json`);
|
||||
let dbConfigFile = join(path, 'configuration', `${db}.${process.env.NODE_ENV}.json`);
|
||||
if (existsSync(dbConfigFile) === false) {
|
||||
dbConfigFile = join(path, 'configuration', `${db}.json`);
|
||||
}
|
||||
|
||||
if (existsSync(dbConfigFile) === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const config = require(dbConfigFile);
|
||||
console.log(`使用${db}作为数据库`);
|
||||
return Object.assign({}, { type: db }, config);
|
||||
usedDbType = db;
|
||||
return Object.assign({}, config);
|
||||
}
|
||||
catch (err) {
|
||||
// do nothing
|
||||
|
|
@ -33,8 +45,10 @@ export const getDbConfig = (path: string): DbConfiguration => {
|
|||
}
|
||||
|
||||
export const getDbStoreClass = <ED extends EntityDict & BaseEntityDict, Cxt extends BackendRuntimeContext<ED>>
|
||||
(dbConfig: DbConfiguration) => {
|
||||
const dbType = dbConfig.type || 'mysql';
|
||||
() => {
|
||||
const dbType = usedDbType || (() => {
|
||||
throw new Error('无法确定数据库类型');
|
||||
})();
|
||||
const DbStoreClass = dbList[dbType.toLowerCase() as keyof typeof dbList];
|
||||
if (!DbStoreClass) {
|
||||
throw new Error(`不支持的数据库类型:${dbType},请确认是否存在以下配置文件:${Object.keys(dbList).map(ele => `${ele}.json`).join('、')}`);
|
||||
|
|
|
|||
Loading…
Reference in New Issue