74 lines
2.9 KiB
JavaScript
74 lines
2.9 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.AppLoader = void 0;
|
|
const actionDef_1 = require("oak-domain/lib/store/actionDef");
|
|
const types_1 = require("oak-domain/lib/types");
|
|
const DbStore_1 = require("./DbStore");
|
|
const index_1 = __importDefault(require("oak-common-aspect/lib/index"));
|
|
function initTriggers(dbStore, path) {
|
|
const { triggers } = require(`${path}/lib/triggers/index`);
|
|
const { checkers } = require(`${path}/lib/checkers/index`);
|
|
const { ActionDefDict } = require(`${path}/lib/oak-app-domain/ActionDefDict`);
|
|
const { triggers: adTriggers, checkers: adCheckers } = (0, actionDef_1.analyzeActionDefDict)(dbStore.getSchema(), ActionDefDict);
|
|
triggers.forEach((trigger) => dbStore.registerTrigger(trigger));
|
|
adTriggers.forEach((trigger) => dbStore.registerTrigger(trigger));
|
|
checkers.forEach((checker) => dbStore.registerChecker(checker));
|
|
adCheckers.forEach((checker) => dbStore.registerChecker(checker));
|
|
}
|
|
class AppLoader extends types_1.AppLoader {
|
|
dbStore;
|
|
aspectDict;
|
|
contextBuilder;
|
|
constructor(path, contextBuilder, dbConfig) {
|
|
super(path);
|
|
const { storageSchema } = require(`${path}/lib/oak-app-domain/Storage`);
|
|
this.aspectDict = Object.assign({}, index_1.default, require(`${path}/lib/aspects/index`).aspectDict);
|
|
this.dbStore = new DbStore_1.DbStore(storageSchema, contextBuilder, dbConfig);
|
|
this.contextBuilder = contextBuilder;
|
|
}
|
|
async mount(initialize) {
|
|
const { path } = this;
|
|
if (!initialize) {
|
|
initTriggers(this.dbStore, path);
|
|
}
|
|
this.dbStore.connect();
|
|
}
|
|
async unmount() {
|
|
this.dbStore.disconnect();
|
|
}
|
|
async execAspect(name, context, params) {
|
|
const fn = this.aspectDict[name];
|
|
if (!fn) {
|
|
throw new Error(`不存在的接口名称: ${name}`);
|
|
}
|
|
return await fn(params, context);
|
|
}
|
|
async initialize(dropIfExists) {
|
|
await this.dbStore.initialize(dropIfExists);
|
|
const { data } = require(`${this.path}/lib/data/index`);
|
|
const context = this.contextBuilder()(this.dbStore);
|
|
await context.begin();
|
|
for (const entity in data) {
|
|
let rows = data[entity];
|
|
if (entity === 'area') {
|
|
// 对area暂时处理一下
|
|
rows = require('./data/area.json');
|
|
}
|
|
await this.dbStore.operate(entity, {
|
|
data: rows,
|
|
action: 'create',
|
|
}, context);
|
|
console.log(`data in ${entity} initialized!`);
|
|
}
|
|
await context.commit();
|
|
this.dbStore.disconnect();
|
|
}
|
|
getStore() {
|
|
return this.dbStore;
|
|
}
|
|
}
|
|
exports.AppLoader = AppLoader;
|