oak-backend-base 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/AppLoader.js +2 -4
- package/package.json +12 -8
- package/src/AppLoader.ts +0 -85
- package/src/BackendContext.ts +0 -23
- package/src/DbStore.ts +0 -94
- package/src/data/area.json +0 -1
- package/src/index.ts +0 -2
- package/src/polyfill.ts +0 -16
- package/test/test.ts +0 -21
- package/tsconfig.json +0 -73
package/lib/AppLoader.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.AppLoader = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
7
5
|
const actionDef_1 = require("oak-domain/lib/store/actionDef");
|
|
8
6
|
const types_1 = require("oak-domain/lib/types");
|
|
9
7
|
const DbStore_1 = require("./DbStore");
|
|
10
|
-
const index_1 = __importDefault(require("oak-common-aspect/lib/index"));
|
|
8
|
+
const index_1 = tslib_1.__importDefault(require("oak-common-aspect/lib/index"));
|
|
11
9
|
function initTriggers(dbStore, path) {
|
|
12
10
|
const { triggers } = require(`${path}/lib/triggers/index`);
|
|
13
11
|
const { checkers } = require(`${path}/lib/checkers/index`);
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oak-backend-base",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "oak-backend-base",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "lib/index.js",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "XuChang"
|
|
8
8
|
},
|
|
9
|
+
"files": [
|
|
10
|
+
"lib/**/*"
|
|
11
|
+
],
|
|
9
12
|
"scripts": {
|
|
10
13
|
"test": "ts-node test/test.ts",
|
|
11
14
|
"build": "tsc"
|
|
@@ -17,10 +20,10 @@
|
|
|
17
20
|
"lodash": "^4.17.21",
|
|
18
21
|
"mysql": "^2.18.1",
|
|
19
22
|
"mysql2": "^2.3.3",
|
|
20
|
-
"oak-db": "^1.0.
|
|
21
|
-
"oak-domain": "^1.0.
|
|
22
|
-
"oak-general-business": "^1.0.
|
|
23
|
-
"oak-common-aspect": "^1.0.
|
|
23
|
+
"oak-db": "^1.0.1",
|
|
24
|
+
"oak-domain": "^1.0.4",
|
|
25
|
+
"oak-general-business": "^1.0.1",
|
|
26
|
+
"oak-common-aspect": "^1.0.1",
|
|
24
27
|
"uuid": "^8.3.2"
|
|
25
28
|
},
|
|
26
29
|
"license": "ISC",
|
|
@@ -29,7 +32,8 @@
|
|
|
29
32
|
"@types/koa-router": "^7.4.4",
|
|
30
33
|
"@types/node": "^17.0.40",
|
|
31
34
|
"@types/uuid": "^8.3.4",
|
|
32
|
-
"ts-node": "
|
|
33
|
-
"
|
|
35
|
+
"ts-node": "~10.9.1",
|
|
36
|
+
"tslib": "^2.4.0",
|
|
37
|
+
"typescript": "~4.7.4"
|
|
34
38
|
}
|
|
35
39
|
}
|
package/src/AppLoader.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { analyzeActionDefDict } from "oak-domain/lib/store/actionDef";
|
|
2
|
-
import { AppLoader as GeneralAppLoader, Trigger, Checker, Aspect, RowStore, Context, EntityDict } from "oak-domain/lib/types";
|
|
3
|
-
import { DbStore } from "./DbStore";
|
|
4
|
-
import generalAspectDict from 'oak-common-aspect/lib/index';
|
|
5
|
-
import { MySQLConfiguration } from 'oak-db/lib/MySQL/types/Configuration';
|
|
6
|
-
|
|
7
|
-
function initTriggers<ED extends EntityDict, Cxt extends Context<ED>>(dbStore: DbStore<ED, Cxt>, path: string) {
|
|
8
|
-
const { triggers } = require(`${path}/lib/triggers/index`);
|
|
9
|
-
const { checkers } = require(`${path}/lib/checkers/index`);
|
|
10
|
-
const { ActionDefDict } = require(`${path}/lib/oak-app-domain/ActionDefDict`);
|
|
11
|
-
|
|
12
|
-
const { triggers: adTriggers, checkers: adCheckers } = analyzeActionDefDict(dbStore.getSchema(), ActionDefDict);
|
|
13
|
-
triggers.forEach(
|
|
14
|
-
(trigger: Trigger<ED, keyof ED, Cxt>) => dbStore.registerTrigger(trigger)
|
|
15
|
-
);
|
|
16
|
-
adTriggers.forEach(
|
|
17
|
-
(trigger) => dbStore.registerTrigger(trigger)
|
|
18
|
-
);
|
|
19
|
-
checkers.forEach(
|
|
20
|
-
(checker: Checker<ED, keyof ED, Cxt>) => dbStore.registerChecker(checker)
|
|
21
|
-
);
|
|
22
|
-
adCheckers.forEach(
|
|
23
|
-
(checker) => dbStore.registerChecker(checker)
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
export class AppLoader<ED extends EntityDict, Cxt extends Context<ED>> extends GeneralAppLoader<ED, Cxt> {
|
|
29
|
-
private dbStore: DbStore<ED, Cxt>;
|
|
30
|
-
private aspectDict: Record<string, Aspect<ED, Cxt>>;
|
|
31
|
-
private contextBuilder: (scene?: string) => (store: RowStore<ED, Cxt>) => Cxt;
|
|
32
|
-
constructor(path: string, contextBuilder: (scene?: string) => (store: RowStore<ED, Cxt>) => Cxt, dbConfig: MySQLConfiguration) {
|
|
33
|
-
super(path);
|
|
34
|
-
const { storageSchema } = require(`${path}/lib/oak-app-domain/Storage`);
|
|
35
|
-
this.aspectDict = Object.assign({}, generalAspectDict, require(`${path}/lib/aspects/index`).aspectDict);
|
|
36
|
-
this.dbStore = new DbStore<ED, Cxt>(storageSchema, contextBuilder, dbConfig);
|
|
37
|
-
this.contextBuilder = contextBuilder;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async mount(initialize?: true) {
|
|
41
|
-
const { path } = this;
|
|
42
|
-
if (!initialize) {
|
|
43
|
-
initTriggers(this.dbStore, path);
|
|
44
|
-
}
|
|
45
|
-
this.dbStore.connect();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async unmount() {
|
|
49
|
-
this.dbStore.disconnect();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async execAspect(name: string, context: Cxt, params?: any): Promise<any> {
|
|
53
|
-
const fn = this.aspectDict[name];
|
|
54
|
-
if (!fn) {
|
|
55
|
-
throw new Error(`不存在的接口名称: ${name}`);
|
|
56
|
-
}
|
|
57
|
-
return await fn(params, context);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async initialize(dropIfExists?: boolean) {
|
|
61
|
-
await this.dbStore.initialize(dropIfExists);
|
|
62
|
-
|
|
63
|
-
const { data } = require(`${this.path}/lib/data/index`);
|
|
64
|
-
const context = this.contextBuilder()(this.dbStore);
|
|
65
|
-
await context.begin();
|
|
66
|
-
for (const entity in data) {
|
|
67
|
-
let rows = data[entity];
|
|
68
|
-
if (entity === 'area') {
|
|
69
|
-
// 对area暂时处理一下
|
|
70
|
-
rows = require('./data/area.json');
|
|
71
|
-
}
|
|
72
|
-
await this.dbStore.operate(entity as keyof ED, {
|
|
73
|
-
data: rows,
|
|
74
|
-
action: 'create',
|
|
75
|
-
} as any, context);
|
|
76
|
-
console.log(`data in ${entity} initialized!`);
|
|
77
|
-
}
|
|
78
|
-
await context.commit();
|
|
79
|
-
this.dbStore.disconnect();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
getStore(): RowStore<ED, Cxt> {
|
|
83
|
-
return this.dbStore;
|
|
84
|
-
}
|
|
85
|
-
}
|
package/src/BackendContext.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { RowStore } from 'oak-domain/lib/types';
|
|
2
|
-
import { GeneralRuntimeContext } from 'oak-general-business';
|
|
3
|
-
import { EntityDict } from 'oak-general-business/lib/general-app-domain';
|
|
4
|
-
|
|
5
|
-
export class Context<ED extends EntityDict> extends GeneralRuntimeContext<ED> {
|
|
6
|
-
static FromCxtStr(cxtStr?: string){
|
|
7
|
-
const {
|
|
8
|
-
token,
|
|
9
|
-
applicationId,
|
|
10
|
-
scene
|
|
11
|
-
} = cxtStr ? GeneralRuntimeContext.fromString(cxtStr) : {
|
|
12
|
-
token: undefined,
|
|
13
|
-
applicationId: undefined,
|
|
14
|
-
scene: undefined,
|
|
15
|
-
};
|
|
16
|
-
return <ED extends EntityDict>(store: RowStore<ED, Context<ED>>) => {
|
|
17
|
-
const context = new Context<ED>(store, applicationId);
|
|
18
|
-
context.setScene(scene);
|
|
19
|
-
context.setToken(token);
|
|
20
|
-
return context;
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
}
|
package/src/DbStore.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { MysqlStore, MySqlSelectOption, MysqlOperateOption } from 'oak-db';
|
|
2
|
-
import { EntityDict, Context, StorageSchema, OperateOption, SelectionResult, Trigger, Checker, SelectRowShape, RowStore } from 'oak-domain/lib/types';
|
|
3
|
-
import { TriggerExecutor } from 'oak-domain/lib/store/TriggerExecutor';
|
|
4
|
-
import { MySQLConfiguration, } from 'oak-db/lib/MySQL/types/Configuration';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export class DbStore<ED extends EntityDict, Cxt extends Context<ED>> extends MysqlStore<ED, Cxt> {
|
|
8
|
-
private executor: TriggerExecutor<ED, Cxt>;
|
|
9
|
-
|
|
10
|
-
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (scene?: string) => (store: RowStore<ED, Cxt>) => Cxt, mysqlConfiguration: MySQLConfiguration) {
|
|
11
|
-
super(storageSchema, mysqlConfiguration);
|
|
12
|
-
this.executor = new TriggerExecutor(async (scene) => contextBuilder(scene)(this));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
protected async cascadeUpdate<T extends keyof ED>(entity: T, operation: ED[T]['Create'] | ED[T]['Update'] | ED[T]['Remove'], context: Cxt, option?: MysqlOperateOption) {
|
|
17
|
-
await this.executor.preOperation(entity, operation, context, option);
|
|
18
|
-
const result = super.cascadeUpdate(entity, operation, context, option);
|
|
19
|
-
await this.executor.postOperation(entity, operation, context, option);
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
protected async cascadeSelect<T extends keyof ED, S extends ED[T]["Selection"]>(entity: T, selection: S, context: Cxt, option?: MySqlSelectOption): Promise<SelectRowShape<ED[T]['Schema'], S['data']>[]> {
|
|
24
|
-
const selection2 = Object.assign({
|
|
25
|
-
action: 'select',
|
|
26
|
-
}, selection) as ED[T]['Operation'];
|
|
27
|
-
|
|
28
|
-
if (!option?.ignoreTrigger) {
|
|
29
|
-
await this.executor.preOperation(entity, selection2, context, option);
|
|
30
|
-
}
|
|
31
|
-
const result = await super.cascadeSelect(entity, selection2 as any, context, option);
|
|
32
|
-
if (!option?.ignoreTrigger) {
|
|
33
|
-
await this.executor.postOperation(entity, selection2, context, option, result);
|
|
34
|
-
}
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async operate<T extends keyof ED>(
|
|
39
|
-
entity: T,
|
|
40
|
-
operation: ED[T]['Operation'],
|
|
41
|
-
context: Cxt,
|
|
42
|
-
params?: MysqlOperateOption
|
|
43
|
-
) {
|
|
44
|
-
const autoCommit = !context.getCurrentTxnId();
|
|
45
|
-
let result;
|
|
46
|
-
if (autoCommit) {
|
|
47
|
-
await context.begin();
|
|
48
|
-
}
|
|
49
|
-
try {
|
|
50
|
-
result = await super.operate(entity, operation, context, params);
|
|
51
|
-
}
|
|
52
|
-
catch (err) {
|
|
53
|
-
await context.rollback();
|
|
54
|
-
throw err;
|
|
55
|
-
}
|
|
56
|
-
if (autoCommit) {
|
|
57
|
-
await context.commit();
|
|
58
|
-
}
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async select<T extends keyof ED, S extends ED[T]['Selection']>(
|
|
63
|
-
entity: T,
|
|
64
|
-
selection: S,
|
|
65
|
-
context: Cxt,
|
|
66
|
-
params?: MySqlSelectOption
|
|
67
|
-
) {
|
|
68
|
-
const autoCommit = !context.getCurrentTxnId();
|
|
69
|
-
if (autoCommit) {
|
|
70
|
-
await context.begin();
|
|
71
|
-
}
|
|
72
|
-
let result: SelectionResult<ED[T]['Schema'], S['data']>;
|
|
73
|
-
|
|
74
|
-
try {
|
|
75
|
-
result = await super.select(entity, selection, context, params);
|
|
76
|
-
}
|
|
77
|
-
catch (err) {
|
|
78
|
-
await context.rollback();
|
|
79
|
-
throw err;
|
|
80
|
-
}
|
|
81
|
-
if (autoCommit) {
|
|
82
|
-
await context.commit();
|
|
83
|
-
}
|
|
84
|
-
return result;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
registerTrigger<T extends keyof ED>(trigger: Trigger<ED, T, Cxt>) {
|
|
88
|
-
this.executor.registerTrigger(trigger);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
registerChecker<T extends keyof ED>(checker: Checker<ED, T, Cxt>) {
|
|
92
|
-
this.executor.registerChecker(checker);
|
|
93
|
-
}
|
|
94
|
-
}
|