oak-backend-base 2.4.1 → 3.0.0
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.d.ts +1 -0
- package/lib/AppLoader.js +51 -21
- package/lib/DbStore.d.ts +3 -2
- package/lib/DbStore.js +18 -9
- package/package.json +5 -5
package/lib/AppLoader.d.ts
CHANGED
|
@@ -21,4 +21,5 @@ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt exten
|
|
|
21
21
|
getEndpoints(): Record<string, Endpoint<ED, Cxt>>;
|
|
22
22
|
startTimers(): void;
|
|
23
23
|
execStartRoutines(): Promise<void>;
|
|
24
|
+
execRoutine(routine: (context: Cxt) => Promise<void>): Promise<void>;
|
|
24
25
|
}
|
package/lib/AppLoader.js
CHANGED
|
@@ -5,10 +5,9 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const path_1 = require("path");
|
|
7
7
|
const node_schedule_1 = require("node-schedule");
|
|
8
|
+
const env_1 = require("oak-domain/lib/compiler/env");
|
|
8
9
|
const actionDef_1 = require("oak-domain/lib/store/actionDef");
|
|
9
10
|
const lodash_1 = require("oak-domain/lib/utils/lodash");
|
|
10
|
-
const checkers_1 = require("oak-domain/lib/checkers");
|
|
11
|
-
const triggers_1 = require("oak-domain/lib/triggers");
|
|
12
11
|
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
|
13
12
|
const types_1 = require("oak-domain/lib/types");
|
|
14
13
|
const DbStore_1 = require("./DbStore");
|
|
@@ -41,20 +40,44 @@ class AppLoader extends types_1.AppLoader {
|
|
|
41
40
|
const inter = (0, lodash_1.intersection)(Object.keys(sthOut), Object.keys(sth2));
|
|
42
41
|
if (inter.length > 0) {
|
|
43
42
|
console.warn(`${(0, path_1.join)(this.path, 'node_modules', this.externalDependencies[idx], filePath)}中的default输出对象中的key值【${inter.join(',')}】与其它对应路径输出的key值有冲突,请仔细检查避免错误`);
|
|
43
|
+
inter.forEach((ele) => {
|
|
44
|
+
if (sth2[ele] instanceof Array && sthOut[ele]) {
|
|
45
|
+
(0, assert_1.default)(sthOut[ele] instanceof Array, `${(0, path_1.join)(this.path, 'node_modules', this.externalDependencies[idx], filePath)}中的default输出对象的${ele}键值是数组,但之前的相应对象的${ele}却不是,请仔细检查以避免错误`);
|
|
46
|
+
console.warn(`${(0, path_1.join)(this.path, 'node_modules', this.externalDependencies[idx], filePath)}中的default输出对象中的key值【${ele}】与其它对应路径输出的key值【${ele}】将以数组格式进行合并,请仔细检查避免错误`);
|
|
47
|
+
sth2[ele].push(...sthOut[ele]);
|
|
48
|
+
}
|
|
49
|
+
else if (!(sth2[ele] instanceof Array) && sthOut[ele]) {
|
|
50
|
+
(0, assert_1.default)(!(sthOut[ele] instanceof Array), `${(0, path_1.join)(this.path, 'node_modules', this.externalDependencies[idx], filePath)}中的default输出对象的${ele}键值不是数组,但之前的相应对象的${ele}却是,请仔细检查以避免错误`);
|
|
51
|
+
console.warn(`${(0, path_1.join)(this.path, 'node_modules', this.externalDependencies[idx], filePath)}中的default输出对象中的key值【${ele}】将对与其它对应路径输出的key值【${ele}】进行覆盖,请仔细检查避免错误`);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
44
54
|
}
|
|
45
55
|
Object.assign(sthOut, sth2);
|
|
46
56
|
});
|
|
47
57
|
const inter = (0, lodash_1.intersection)(Object.keys(sthOut), Object.keys(sth));
|
|
48
|
-
|
|
58
|
+
if (inter.length > 0) {
|
|
59
|
+
inter.forEach((ele) => {
|
|
60
|
+
if (sth[ele] instanceof Array && sthOut[ele]) {
|
|
61
|
+
(0, assert_1.default)(sthOut[ele] instanceof Array, `项目${filePath}中的default输出对象的${ele}键值是数组,但之前的相应对象的${ele}却不是,请仔细检查以避免错误`);
|
|
62
|
+
console.warn(`项目${filePath}中的default输出对象中的key值【${ele}】与其它引用包该路径输出的key值【${ele}】将以数组格式进行合并,请仔细检查避免错误`);
|
|
63
|
+
sth[ele].push(...sthOut[ele]);
|
|
64
|
+
}
|
|
65
|
+
else if (!(sth[ele] instanceof Array) && sthOut[ele]) {
|
|
66
|
+
(0, assert_1.default)(!(sthOut[ele] instanceof Array), `项目${filePath}中的default输出对象的${ele}键值不是数组,但之前的相应对象的${ele}却是,请仔细检查以避免错误`);
|
|
67
|
+
console.warn(`项目${filePath}中的default输出对象中的key值【${ele}】将对其它引用包该路径输出的key值【${ele}】进行覆盖,请仔细检查避免错误`);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
49
71
|
Object.assign(sthOut, sth);
|
|
50
72
|
return sthOut;
|
|
51
73
|
}
|
|
52
74
|
constructor(path, contextBuilder, dbConfig) {
|
|
53
75
|
super(path);
|
|
54
76
|
const { storageSchema } = require(`${path}/lib/oak-app-domain/Storage`);
|
|
55
|
-
|
|
77
|
+
const { ActionCascadePathGraph, RelationCascadePathGraph, selectFreeEntities, createFreeEntities, updateFreeEntities, deducedRelationMap } = require(`${path}/lib/oak-app-domain/Relation`);
|
|
78
|
+
this.externalDependencies = require((0, env_1.OAK_EXTERNAL_LIBS_FILEPATH)((0, path_1.join)(path, 'lib')));
|
|
56
79
|
this.aspectDict = Object.assign({}, index_1.default, this.requireSth('lib/aspects/index'));
|
|
57
|
-
this.dbStore = new DbStore_1.DbStore(storageSchema, contextBuilder, dbConfig);
|
|
80
|
+
this.dbStore = new DbStore_1.DbStore(storageSchema, contextBuilder, dbConfig, ActionCascadePathGraph, RelationCascadePathGraph, deducedRelationMap, selectFreeEntities, createFreeEntities, updateFreeEntities);
|
|
58
81
|
this.contextBuilder = contextBuilder;
|
|
59
82
|
}
|
|
60
83
|
initTriggers() {
|
|
@@ -62,20 +85,16 @@ class AppLoader extends types_1.AppLoader {
|
|
|
62
85
|
const checkers = this.requireSth('lib/checkers/index');
|
|
63
86
|
const authDict = this.requireSth('lib/auth/index');
|
|
64
87
|
const { ActionDefDict } = require(`${this.path}/lib/oak-app-domain/ActionDefDict`);
|
|
65
|
-
const { triggers: adTriggers, checkers: adCheckers } = (0, actionDef_1.
|
|
88
|
+
const { triggers: adTriggers, checkers: adCheckers } = (0, actionDef_1.makeIntrinsicCTWs)(this.dbStore.getSchema(), ActionDefDict);
|
|
66
89
|
triggers.forEach((trigger) => this.dbStore.registerTrigger(trigger));
|
|
67
90
|
adTriggers.forEach((trigger) => this.dbStore.registerTrigger(trigger));
|
|
68
91
|
checkers.forEach((checker) => this.dbStore.registerChecker(checker));
|
|
69
92
|
adCheckers.forEach((checker) => this.dbStore.registerChecker(checker));
|
|
70
|
-
const dynamicCheckers = (0, checkers_1.createDynamicCheckers)(this.dbStore.getSchema(), authDict);
|
|
71
|
-
dynamicCheckers.forEach((checker) => this.dbStore.registerChecker(checker));
|
|
72
|
-
const dynamicTriggers = (0, triggers_1.createDynamicTriggers)(this.dbStore.getSchema());
|
|
73
|
-
dynamicTriggers.forEach((trigger) => this.dbStore.registerTrigger(trigger));
|
|
74
93
|
}
|
|
75
94
|
startWatchers() {
|
|
76
95
|
const watchers = this.requireSth('lib/watchers/index');
|
|
77
96
|
const { ActionDefDict } = require(`${this.path}/lib/oak-app-domain/ActionDefDict`);
|
|
78
|
-
const { watchers: adWatchers } = (0, actionDef_1.
|
|
97
|
+
const { watchers: adWatchers } = (0, actionDef_1.makeIntrinsicCTWs)(this.dbStore.getSchema(), ActionDefDict);
|
|
79
98
|
const totalWatchers = watchers.concat(adWatchers);
|
|
80
99
|
let count = 0;
|
|
81
100
|
const doWatchers = async () => {
|
|
@@ -150,7 +169,6 @@ class AppLoader extends types_1.AppLoader {
|
|
|
150
169
|
await this.dbStore.initialize(dropIfExists);
|
|
151
170
|
const data = this.requireSth('lib/data/index');
|
|
152
171
|
const context = await this.contextBuilder()(this.dbStore);
|
|
153
|
-
await context.begin();
|
|
154
172
|
for (const entity in data) {
|
|
155
173
|
let rows = data[entity];
|
|
156
174
|
if (entity === 'area') {
|
|
@@ -158,17 +176,25 @@ class AppLoader extends types_1.AppLoader {
|
|
|
158
176
|
rows = require('./data/area.json');
|
|
159
177
|
}
|
|
160
178
|
if (rows.length > 0) {
|
|
161
|
-
await
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
179
|
+
await context.begin();
|
|
180
|
+
try {
|
|
181
|
+
await this.dbStore.operate(entity, {
|
|
182
|
+
data: rows,
|
|
183
|
+
action: 'create',
|
|
184
|
+
}, context, {
|
|
185
|
+
dontCollect: true,
|
|
186
|
+
dontCreateOper: true,
|
|
187
|
+
});
|
|
188
|
+
await context.commit();
|
|
189
|
+
console.log(`data in ${entity} initialized!`);
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
await context.rollback();
|
|
193
|
+
console.error(`data on ${entity} initilization failed!`);
|
|
194
|
+
throw err;
|
|
195
|
+
}
|
|
169
196
|
}
|
|
170
197
|
}
|
|
171
|
-
await context.commit();
|
|
172
198
|
this.dbStore.disconnect();
|
|
173
199
|
}
|
|
174
200
|
getStore() {
|
|
@@ -217,5 +243,9 @@ class AppLoader extends types_1.AppLoader {
|
|
|
217
243
|
}
|
|
218
244
|
}
|
|
219
245
|
}
|
|
246
|
+
async execRoutine(routine) {
|
|
247
|
+
const context = await this.contextBuilder()(this.dbStore);
|
|
248
|
+
await routine(context);
|
|
249
|
+
}
|
|
220
250
|
}
|
|
221
251
|
exports.AppLoader = AppLoader;
|
package/lib/DbStore.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { MysqlStore, MySqlSelectOption, MysqlOperateOption } from 'oak-db';
|
|
2
|
-
import { EntityDict, StorageSchema, Trigger, Checker, SelectOption } from 'oak-domain/lib/types';
|
|
2
|
+
import { EntityDict, StorageSchema, Trigger, Checker, AuthDeduceRelationMap, SelectOption, AuthCascadePath } from 'oak-domain/lib/types';
|
|
3
3
|
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
|
4
4
|
import { MySQLConfiguration } from 'oak-db/lib/MySQL/types/Configuration';
|
|
5
5
|
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
|
|
6
6
|
export declare class DbStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends MysqlStore<ED, Cxt> implements AsyncRowStore<ED, Cxt> {
|
|
7
7
|
private executor;
|
|
8
|
-
|
|
8
|
+
private relationAuth;
|
|
9
|
+
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (scene?: string) => (store: DbStore<ED, Cxt>) => Promise<Cxt>, mysqlConfiguration: MySQLConfiguration, actionCascadeGraph: AuthCascadePath<ED>[], relationCascadeGraph: AuthCascadePath<ED>[], authDeduceRelationMap: AuthDeduceRelationMap<ED>, selectFreeEntities?: (keyof ED)[], createFreeEntities?: (keyof ED)[], updateFreeEntities?: (keyof ED)[]);
|
|
9
10
|
protected cascadeUpdateAsync<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: AsyncContext<ED>, option: MysqlOperateOption): Promise<import("oak-domain/lib/types").OperationResult<ED>>;
|
|
10
11
|
operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: MysqlOperateOption): Promise<import("oak-domain/lib/types").OperationResult<ED>>;
|
|
11
12
|
select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: MySqlSelectOption): Promise<Partial<ED[T]["Schema"]>[]>;
|
package/lib/DbStore.js
CHANGED
|
@@ -3,18 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.DbStore = void 0;
|
|
4
4
|
const oak_db_1 = require("oak-db");
|
|
5
5
|
const TriggerExecutor_1 = require("oak-domain/lib/store/TriggerExecutor");
|
|
6
|
+
const RelationAuth_1 = require("oak-domain/lib/store/RelationAuth");
|
|
6
7
|
class DbStore extends oak_db_1.MysqlStore {
|
|
7
8
|
executor;
|
|
8
|
-
|
|
9
|
+
relationAuth;
|
|
10
|
+
constructor(storageSchema, contextBuilder, mysqlConfiguration, actionCascadeGraph, relationCascadeGraph, authDeduceRelationMap, selectFreeEntities = [], createFreeEntities = [], updateFreeEntities = []) {
|
|
9
11
|
super(storageSchema, mysqlConfiguration);
|
|
10
12
|
this.executor = new TriggerExecutor_1.TriggerExecutor((scene) => contextBuilder(scene)(this));
|
|
13
|
+
this.relationAuth = new RelationAuth_1.RelationAuth(storageSchema, actionCascadeGraph, relationCascadeGraph, authDeduceRelationMap, selectFreeEntities, createFreeEntities, updateFreeEntities);
|
|
11
14
|
}
|
|
12
15
|
async cascadeUpdateAsync(entity, operation, context, option) {
|
|
13
|
-
|
|
16
|
+
// 如果是在modi处理过程中,所有的trigger也可以延时到apply时再处理(这时候因为modi中的数据并不实际存在,处理会有问题)
|
|
17
|
+
if (!option.blockTrigger && !option.modiParentEntity) {
|
|
14
18
|
await this.executor.preOperation(entity, operation, context, option);
|
|
15
19
|
}
|
|
16
20
|
const result = await super.cascadeUpdateAsync(entity, operation, context, option);
|
|
17
|
-
if (!option.blockTrigger) {
|
|
21
|
+
if (!option.blockTrigger && !option.modiParentEntity) {
|
|
18
22
|
await this.executor.postOperation(entity, operation, context, option);
|
|
19
23
|
}
|
|
20
24
|
return result;
|
|
@@ -26,6 +30,7 @@ class DbStore extends oak_db_1.MysqlStore {
|
|
|
26
30
|
await context.begin();
|
|
27
31
|
}
|
|
28
32
|
try {
|
|
33
|
+
await this.relationAuth.checkRelationAsync(entity, operation, context);
|
|
29
34
|
result = await super.operate(entity, operation, context, option);
|
|
30
35
|
}
|
|
31
36
|
catch (err) {
|
|
@@ -50,6 +55,9 @@ class DbStore extends oak_db_1.MysqlStore {
|
|
|
50
55
|
if (!option.blockTrigger) {
|
|
51
56
|
await this.executor.preOperation(entity, selection, context, option);
|
|
52
57
|
}
|
|
58
|
+
if (!option.dontCollect) {
|
|
59
|
+
await this.relationAuth.checkRelationAsync(entity, selection, context);
|
|
60
|
+
}
|
|
53
61
|
try {
|
|
54
62
|
result = await super.select(entity, selection, context, option);
|
|
55
63
|
if (!option.blockTrigger) {
|
|
@@ -72,12 +80,13 @@ class DbStore extends oak_db_1.MysqlStore {
|
|
|
72
80
|
await context.begin();
|
|
73
81
|
}
|
|
74
82
|
try {
|
|
75
|
-
const selection2 = Object.assign({
|
|
76
|
-
|
|
77
|
-
}, selection);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
83
|
+
// const selection2 = Object.assign({
|
|
84
|
+
// action: 'select',
|
|
85
|
+
// }, selection) as ED[T]['Operation'];
|
|
86
|
+
// await this.relationAuth.checkRelationAsync(entity, selection2, context);
|
|
87
|
+
// if (!option.blockTrigger) {
|
|
88
|
+
// await this.executor.preOperation(entity, selection2, context, option);
|
|
89
|
+
// }
|
|
81
90
|
result = await super.count(entity, selection, context, option);
|
|
82
91
|
/* count应该不存在后trigger吧
|
|
83
92
|
if (!option.blockTrigger) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oak-backend-base",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "oak-backend-base",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"author": {
|
|
@@ -20,9 +20,9 @@
|
|
|
20
20
|
"mysql": "^2.18.1",
|
|
21
21
|
"mysql2": "^2.3.3",
|
|
22
22
|
"node-schedule": "^2.1.0",
|
|
23
|
-
"oak-common-aspect": "^2.1
|
|
24
|
-
"oak-db": "^
|
|
25
|
-
"oak-domain": "^
|
|
23
|
+
"oak-common-aspect": "^2.2.1",
|
|
24
|
+
"oak-db": "^3.0.0",
|
|
25
|
+
"oak-domain": "^3.0.0",
|
|
26
26
|
"uuid": "^8.3.2"
|
|
27
27
|
},
|
|
28
28
|
"license": "ISC",
|
|
@@ -32,6 +32,6 @@
|
|
|
32
32
|
"copyfiles": "^2.4.1",
|
|
33
33
|
"ts-node": "~10.9.1",
|
|
34
34
|
"tslib": "^2.4.0",
|
|
35
|
-
"typescript": "
|
|
35
|
+
"typescript": "^4.7.4"
|
|
36
36
|
}
|
|
37
37
|
}
|