oak-backend-base 2.0.3 → 2.1.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 +17 -16
- package/lib/AppLoader.js +138 -80
- package/lib/DbStore.d.ts +15 -15
- package/lib/DbStore.js +103 -103
- package/lib/index.d.ts +1 -1
- package/lib/index.js +5 -5
- package/lib/polyfill.d.ts +3 -3
- package/lib/polyfill.js +12 -12
- package/package.json +35 -35
package/lib/AppLoader.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
|
2
|
-
import { AppLoader as GeneralAppLoader, EntityDict } from "oak-domain/lib/types";
|
|
3
|
-
import { DbStore } from "./DbStore";
|
|
4
|
-
import { MySQLConfiguration } from 'oak-db/lib/MySQL/types/Configuration';
|
|
5
|
-
import { AsyncContext } from "oak-domain/lib/store/AsyncRowStore";
|
|
6
|
-
export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends GeneralAppLoader<ED, Cxt> {
|
|
7
|
-
private dbStore;
|
|
8
|
-
private aspectDict;
|
|
9
|
-
private contextBuilder;
|
|
10
|
-
constructor(path: string, contextBuilder: (scene?: string) => (store: DbStore<ED, Cxt>) => Promise<Cxt>, dbConfig: MySQLConfiguration);
|
|
11
|
-
mount(initialize?: true): Promise<void>;
|
|
12
|
-
unmount(): Promise<void>;
|
|
13
|
-
execAspect(name: string, context: Cxt, params?: any): Promise<any>;
|
|
14
|
-
initialize(dropIfExists?: boolean): Promise<void>;
|
|
15
|
-
getStore(): DbStore<ED, Cxt>;
|
|
16
|
-
|
|
1
|
+
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
|
2
|
+
import { AppLoader as GeneralAppLoader, EntityDict } from "oak-domain/lib/types";
|
|
3
|
+
import { DbStore } from "./DbStore";
|
|
4
|
+
import { MySQLConfiguration } from 'oak-db/lib/MySQL/types/Configuration';
|
|
5
|
+
import { AsyncContext } from "oak-domain/lib/store/AsyncRowStore";
|
|
6
|
+
export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends GeneralAppLoader<ED, Cxt> {
|
|
7
|
+
private dbStore;
|
|
8
|
+
private aspectDict;
|
|
9
|
+
private contextBuilder;
|
|
10
|
+
constructor(path: string, contextBuilder: (scene?: string) => (store: DbStore<ED, Cxt>) => Promise<Cxt>, dbConfig: MySQLConfiguration);
|
|
11
|
+
mount(initialize?: true): Promise<void>;
|
|
12
|
+
unmount(): Promise<void>;
|
|
13
|
+
execAspect(name: string, context: Cxt, params?: any): Promise<any>;
|
|
14
|
+
initialize(dropIfExists?: boolean): Promise<void>;
|
|
15
|
+
getStore(): DbStore<ED, Cxt>;
|
|
16
|
+
startWatchers(): void;
|
|
17
|
+
}
|
package/lib/AppLoader.js
CHANGED
|
@@ -1,80 +1,138 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AppLoader = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const actionDef_1 = require("oak-domain/lib/store/actionDef");
|
|
6
|
-
const checkers_1 = require("oak-domain/lib/checkers");
|
|
7
|
-
const triggers_1 = require("oak-domain/lib/triggers");
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
const {
|
|
15
|
-
const {
|
|
16
|
-
triggers.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
dynamicCheckers.
|
|
22
|
-
|
|
23
|
-
dynamicTriggers.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AppLoader = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const actionDef_1 = require("oak-domain/lib/store/actionDef");
|
|
6
|
+
const checkers_1 = require("oak-domain/lib/checkers");
|
|
7
|
+
const triggers_1 = require("oak-domain/lib/triggers");
|
|
8
|
+
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
|
9
|
+
const types_1 = require("oak-domain/lib/types");
|
|
10
|
+
const DbStore_1 = require("./DbStore");
|
|
11
|
+
const index_1 = tslib_1.__importDefault(require("oak-common-aspect/lib/index"));
|
|
12
|
+
function initTriggers(dbStore, path) {
|
|
13
|
+
const { triggers } = require(`${path}/lib/triggers/index`);
|
|
14
|
+
const { checkers } = require(`${path}/lib/checkers/index`);
|
|
15
|
+
const { ActionDefDict } = require(`${path}/lib/oak-app-domain/ActionDefDict`);
|
|
16
|
+
const { triggers: adTriggers, checkers: adCheckers } = (0, actionDef_1.analyzeActionDefDict)(dbStore.getSchema(), ActionDefDict);
|
|
17
|
+
triggers.forEach((trigger) => dbStore.registerTrigger(trigger));
|
|
18
|
+
adTriggers.forEach((trigger) => dbStore.registerTrigger(trigger));
|
|
19
|
+
checkers.forEach((checker) => dbStore.registerChecker(checker));
|
|
20
|
+
adCheckers.forEach((checker) => dbStore.registerChecker(checker));
|
|
21
|
+
const dynamicCheckers = (0, checkers_1.createDynamicCheckers)(dbStore.getSchema());
|
|
22
|
+
dynamicCheckers.forEach((checker) => dbStore.registerChecker(checker));
|
|
23
|
+
const dynamicTriggers = (0, triggers_1.createDynamicTriggers)(dbStore.getSchema());
|
|
24
|
+
dynamicTriggers.forEach((trigger) => dbStore.registerTrigger(trigger));
|
|
25
|
+
}
|
|
26
|
+
function startWatchers(dbStore, path, contextBuilder) {
|
|
27
|
+
const { watchers } = require(`${path}/lib/watchers/index`);
|
|
28
|
+
const { ActionDefDict } = require(`${path}/lib/oak-app-domain/ActionDefDict`);
|
|
29
|
+
const { watchers: adWatchers } = (0, actionDef_1.analyzeActionDefDict)(dbStore.getSchema(), ActionDefDict);
|
|
30
|
+
const totalWatchers = watchers.concat(adWatchers);
|
|
31
|
+
let count = 0;
|
|
32
|
+
const doWatchers = async () => {
|
|
33
|
+
count++;
|
|
34
|
+
const start = Date.now();
|
|
35
|
+
const context = await contextBuilder()(dbStore);
|
|
36
|
+
for (const w of totalWatchers) {
|
|
37
|
+
await context.begin();
|
|
38
|
+
try {
|
|
39
|
+
if (w.hasOwnProperty('actionData')) {
|
|
40
|
+
const { entity, action, filter, actionData } = w;
|
|
41
|
+
const filter2 = typeof filter === 'function' ? filter() : filter;
|
|
42
|
+
const data = typeof actionData === 'function' ? await actionData() : actionData; // 这里有个奇怪的编译错误,不理解 by Xc
|
|
43
|
+
const result = await dbStore.operate(entity, {
|
|
44
|
+
id: await (0, uuid_1.generateNewIdAsync)(),
|
|
45
|
+
action,
|
|
46
|
+
data,
|
|
47
|
+
filter: filter2
|
|
48
|
+
}, context, {
|
|
49
|
+
dontCollect: true,
|
|
50
|
+
});
|
|
51
|
+
console.log(`执行了watcher【${w.name}】,结果是:`, result);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
const { entity, projection, fn, filter } = w;
|
|
55
|
+
const filter2 = typeof filter === 'function' ? await filter() : filter;
|
|
56
|
+
const projection2 = typeof projection === 'function' ? await projection() : projection;
|
|
57
|
+
const rows = await dbStore.select(entity, {
|
|
58
|
+
data: projection2,
|
|
59
|
+
filter: filter2,
|
|
60
|
+
}, context, {
|
|
61
|
+
dontCollect: true,
|
|
62
|
+
blockTrigger: true,
|
|
63
|
+
});
|
|
64
|
+
const result = await fn(context, rows);
|
|
65
|
+
console.log(`执行了watcher【${w.name}】,结果是:`, result);
|
|
66
|
+
}
|
|
67
|
+
await context.commit();
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
await context.rollback();
|
|
71
|
+
console.error(`执行了watcher【${w.name}】,发生错误:`, err);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const duration = Date.now() - start;
|
|
75
|
+
console.log(`第${count}次执行watchers,共执行${watchers.length}个,耗时${duration}毫秒`);
|
|
76
|
+
setTimeout(() => doWatchers(), 120000);
|
|
77
|
+
};
|
|
78
|
+
doWatchers();
|
|
79
|
+
}
|
|
80
|
+
class AppLoader extends types_1.AppLoader {
|
|
81
|
+
dbStore;
|
|
82
|
+
aspectDict;
|
|
83
|
+
contextBuilder;
|
|
84
|
+
constructor(path, contextBuilder, dbConfig) {
|
|
85
|
+
super(path);
|
|
86
|
+
const { storageSchema } = require(`${path}/lib/oak-app-domain/Storage`);
|
|
87
|
+
this.aspectDict = Object.assign({}, index_1.default, require(`${path}/lib/aspects/index`).aspectDict);
|
|
88
|
+
this.dbStore = new DbStore_1.DbStore(storageSchema, contextBuilder, dbConfig);
|
|
89
|
+
this.contextBuilder = contextBuilder;
|
|
90
|
+
}
|
|
91
|
+
async mount(initialize) {
|
|
92
|
+
const { path } = this;
|
|
93
|
+
if (!initialize) {
|
|
94
|
+
initTriggers(this.dbStore, path);
|
|
95
|
+
}
|
|
96
|
+
this.dbStore.connect();
|
|
97
|
+
}
|
|
98
|
+
async unmount() {
|
|
99
|
+
this.dbStore.disconnect();
|
|
100
|
+
}
|
|
101
|
+
async execAspect(name, context, params) {
|
|
102
|
+
const fn = this.aspectDict[name];
|
|
103
|
+
if (!fn) {
|
|
104
|
+
throw new Error(`不存在的接口名称: ${name}`);
|
|
105
|
+
}
|
|
106
|
+
return await fn(params, context);
|
|
107
|
+
}
|
|
108
|
+
async initialize(dropIfExists) {
|
|
109
|
+
await this.dbStore.initialize(dropIfExists);
|
|
110
|
+
const { data } = require(`${this.path}/lib/data/index`);
|
|
111
|
+
const context = await this.contextBuilder()(this.dbStore);
|
|
112
|
+
await context.begin();
|
|
113
|
+
for (const entity in data) {
|
|
114
|
+
let rows = data[entity];
|
|
115
|
+
if (entity === 'area') {
|
|
116
|
+
// 对area暂时处理一下
|
|
117
|
+
rows = require('./data/area.json');
|
|
118
|
+
}
|
|
119
|
+
await this.dbStore.operate(entity, {
|
|
120
|
+
data: rows,
|
|
121
|
+
action: 'create',
|
|
122
|
+
}, context, {
|
|
123
|
+
dontCollect: true,
|
|
124
|
+
dontCreateOper: true,
|
|
125
|
+
});
|
|
126
|
+
console.log(`data in ${entity} initialized!`);
|
|
127
|
+
}
|
|
128
|
+
await context.commit();
|
|
129
|
+
this.dbStore.disconnect();
|
|
130
|
+
}
|
|
131
|
+
getStore() {
|
|
132
|
+
return this.dbStore;
|
|
133
|
+
}
|
|
134
|
+
startWatchers() {
|
|
135
|
+
startWatchers(this.dbStore, this.path, this.contextBuilder);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
exports.AppLoader = AppLoader;
|
package/lib/DbStore.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { MysqlStore, MySqlSelectOption, MysqlOperateOption } from 'oak-db';
|
|
2
|
-
import { EntityDict, StorageSchema, Trigger, Checker, SelectOption } from 'oak-domain/lib/types';
|
|
3
|
-
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
|
4
|
-
import { MySQLConfiguration } from 'oak-db/lib/MySQL/types/Configuration';
|
|
5
|
-
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
|
|
6
|
-
export declare class DbStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends MysqlStore<ED, Cxt> implements AsyncRowStore<ED, Cxt> {
|
|
7
|
-
private executor;
|
|
8
|
-
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (scene?: string) => (store: DbStore<ED, Cxt>) => Promise<AsyncContext<ED>>, mysqlConfiguration: MySQLConfiguration);
|
|
9
|
-
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
|
-
operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: MysqlOperateOption): Promise<import("oak-domain/lib/types").OperationResult<ED>>;
|
|
11
|
-
select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: MySqlSelectOption): Promise<Partial<ED[T]["Schema"]>[]>;
|
|
12
|
-
count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number>;
|
|
13
|
-
registerTrigger<T extends keyof ED>(trigger: Trigger<ED, T, Cxt>): void;
|
|
14
|
-
registerChecker<T extends keyof ED>(checker: Checker<ED, T, Cxt>): void;
|
|
15
|
-
}
|
|
1
|
+
import { MysqlStore, MySqlSelectOption, MysqlOperateOption } from 'oak-db';
|
|
2
|
+
import { EntityDict, StorageSchema, Trigger, Checker, SelectOption } from 'oak-domain/lib/types';
|
|
3
|
+
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
|
4
|
+
import { MySQLConfiguration } from 'oak-db/lib/MySQL/types/Configuration';
|
|
5
|
+
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
|
|
6
|
+
export declare class DbStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends MysqlStore<ED, Cxt> implements AsyncRowStore<ED, Cxt> {
|
|
7
|
+
private executor;
|
|
8
|
+
constructor(storageSchema: StorageSchema<ED>, contextBuilder: (scene?: string) => (store: DbStore<ED, Cxt>) => Promise<AsyncContext<ED>>, mysqlConfiguration: MySQLConfiguration);
|
|
9
|
+
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
|
+
operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: MysqlOperateOption): Promise<import("oak-domain/lib/types").OperationResult<ED>>;
|
|
11
|
+
select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: MySqlSelectOption): Promise<Partial<ED[T]["Schema"]>[]>;
|
|
12
|
+
count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number>;
|
|
13
|
+
registerTrigger<T extends keyof ED>(trigger: Trigger<ED, T, Cxt>): void;
|
|
14
|
+
registerChecker<T extends keyof ED>(checker: Checker<ED, T, Cxt>): void;
|
|
15
|
+
}
|
package/lib/DbStore.js
CHANGED
|
@@ -1,103 +1,103 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DbStore = void 0;
|
|
4
|
-
const oak_db_1 = require("oak-db");
|
|
5
|
-
const TriggerExecutor_1 = require("oak-domain/lib/store/TriggerExecutor");
|
|
6
|
-
class DbStore extends oak_db_1.MysqlStore {
|
|
7
|
-
executor;
|
|
8
|
-
constructor(storageSchema, contextBuilder, mysqlConfiguration) {
|
|
9
|
-
super(storageSchema, mysqlConfiguration);
|
|
10
|
-
this.executor = new TriggerExecutor_1.TriggerExecutor((scene) => contextBuilder(scene)(this));
|
|
11
|
-
}
|
|
12
|
-
async cascadeUpdateAsync(entity, operation, context, option) {
|
|
13
|
-
if (!option.blockTrigger) {
|
|
14
|
-
await this.executor.preOperation(entity, operation, context, option);
|
|
15
|
-
}
|
|
16
|
-
const result = super.cascadeUpdateAsync(entity, operation, context, option);
|
|
17
|
-
if (!option.blockTrigger) {
|
|
18
|
-
await this.executor.postOperation(entity, operation, context, option);
|
|
19
|
-
}
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
async operate(entity, operation, context, option) {
|
|
23
|
-
const autoCommit = !context.getCurrentTxnId();
|
|
24
|
-
let result;
|
|
25
|
-
if (autoCommit) {
|
|
26
|
-
await context.begin();
|
|
27
|
-
}
|
|
28
|
-
try {
|
|
29
|
-
result = await super.operate(entity, operation, context, option);
|
|
30
|
-
}
|
|
31
|
-
catch (err) {
|
|
32
|
-
await context.rollback();
|
|
33
|
-
throw err;
|
|
34
|
-
}
|
|
35
|
-
if (autoCommit) {
|
|
36
|
-
await context.commit();
|
|
37
|
-
}
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
async select(entity, selection, context, option) {
|
|
41
|
-
const autoCommit = !context.getCurrentTxnId();
|
|
42
|
-
if (autoCommit) {
|
|
43
|
-
await context.begin();
|
|
44
|
-
}
|
|
45
|
-
let result;
|
|
46
|
-
// select的trigger应加在根select之前,cascade的select不加处理
|
|
47
|
-
const selection2 = Object.assign({
|
|
48
|
-
action: 'select',
|
|
49
|
-
}, selection);
|
|
50
|
-
if (!option.blockTrigger) {
|
|
51
|
-
await this.executor.preOperation(entity, selection2, context, option);
|
|
52
|
-
}
|
|
53
|
-
try {
|
|
54
|
-
result = await super.select(entity, selection, context, option);
|
|
55
|
-
if (!option.blockTrigger) {
|
|
56
|
-
await this.executor.postOperation(entity, selection2, context, option, result);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
catch (err) {
|
|
60
|
-
await context.rollback();
|
|
61
|
-
throw err;
|
|
62
|
-
}
|
|
63
|
-
if (autoCommit) {
|
|
64
|
-
await context.commit();
|
|
65
|
-
}
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
async count(entity, selection, context, option) {
|
|
69
|
-
const autoCommit = !context.getCurrentTxnId();
|
|
70
|
-
let result;
|
|
71
|
-
if (autoCommit) {
|
|
72
|
-
await context.begin();
|
|
73
|
-
}
|
|
74
|
-
try {
|
|
75
|
-
const selection2 = Object.assign({
|
|
76
|
-
action: 'select',
|
|
77
|
-
}, selection);
|
|
78
|
-
if (!option.blockTrigger) {
|
|
79
|
-
await this.executor.preOperation(entity, selection2, context, option);
|
|
80
|
-
}
|
|
81
|
-
result = await super.count(entity, selection, context, option);
|
|
82
|
-
/* count应该不存在后trigger吧
|
|
83
|
-
if (!option.blockTrigger) {
|
|
84
|
-
await this.executor.postOperation(entity, selection2, context, option);
|
|
85
|
-
} */
|
|
86
|
-
}
|
|
87
|
-
catch (err) {
|
|
88
|
-
await context.rollback();
|
|
89
|
-
throw err;
|
|
90
|
-
}
|
|
91
|
-
if (autoCommit) {
|
|
92
|
-
await context.commit();
|
|
93
|
-
}
|
|
94
|
-
return result;
|
|
95
|
-
}
|
|
96
|
-
registerTrigger(trigger) {
|
|
97
|
-
this.executor.registerTrigger(trigger);
|
|
98
|
-
}
|
|
99
|
-
registerChecker(checker) {
|
|
100
|
-
this.executor.registerChecker(checker);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
exports.DbStore = DbStore;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DbStore = void 0;
|
|
4
|
+
const oak_db_1 = require("oak-db");
|
|
5
|
+
const TriggerExecutor_1 = require("oak-domain/lib/store/TriggerExecutor");
|
|
6
|
+
class DbStore extends oak_db_1.MysqlStore {
|
|
7
|
+
executor;
|
|
8
|
+
constructor(storageSchema, contextBuilder, mysqlConfiguration) {
|
|
9
|
+
super(storageSchema, mysqlConfiguration);
|
|
10
|
+
this.executor = new TriggerExecutor_1.TriggerExecutor((scene) => contextBuilder(scene)(this));
|
|
11
|
+
}
|
|
12
|
+
async cascadeUpdateAsync(entity, operation, context, option) {
|
|
13
|
+
if (!option.blockTrigger) {
|
|
14
|
+
await this.executor.preOperation(entity, operation, context, option);
|
|
15
|
+
}
|
|
16
|
+
const result = super.cascadeUpdateAsync(entity, operation, context, option);
|
|
17
|
+
if (!option.blockTrigger) {
|
|
18
|
+
await this.executor.postOperation(entity, operation, context, option);
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
async operate(entity, operation, context, option) {
|
|
23
|
+
const autoCommit = !context.getCurrentTxnId();
|
|
24
|
+
let result;
|
|
25
|
+
if (autoCommit) {
|
|
26
|
+
await context.begin();
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
result = await super.operate(entity, operation, context, option);
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
await context.rollback();
|
|
33
|
+
throw err;
|
|
34
|
+
}
|
|
35
|
+
if (autoCommit) {
|
|
36
|
+
await context.commit();
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
async select(entity, selection, context, option) {
|
|
41
|
+
const autoCommit = !context.getCurrentTxnId();
|
|
42
|
+
if (autoCommit) {
|
|
43
|
+
await context.begin();
|
|
44
|
+
}
|
|
45
|
+
let result;
|
|
46
|
+
// select的trigger应加在根select之前,cascade的select不加处理
|
|
47
|
+
const selection2 = Object.assign({
|
|
48
|
+
action: 'select',
|
|
49
|
+
}, selection);
|
|
50
|
+
if (!option.blockTrigger) {
|
|
51
|
+
await this.executor.preOperation(entity, selection2, context, option);
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
result = await super.select(entity, selection, context, option);
|
|
55
|
+
if (!option.blockTrigger) {
|
|
56
|
+
await this.executor.postOperation(entity, selection2, context, option, result);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
await context.rollback();
|
|
61
|
+
throw err;
|
|
62
|
+
}
|
|
63
|
+
if (autoCommit) {
|
|
64
|
+
await context.commit();
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
async count(entity, selection, context, option) {
|
|
69
|
+
const autoCommit = !context.getCurrentTxnId();
|
|
70
|
+
let result;
|
|
71
|
+
if (autoCommit) {
|
|
72
|
+
await context.begin();
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const selection2 = Object.assign({
|
|
76
|
+
action: 'select',
|
|
77
|
+
}, selection);
|
|
78
|
+
if (!option.blockTrigger) {
|
|
79
|
+
await this.executor.preOperation(entity, selection2, context, option);
|
|
80
|
+
}
|
|
81
|
+
result = await super.count(entity, selection, context, option);
|
|
82
|
+
/* count应该不存在后trigger吧
|
|
83
|
+
if (!option.blockTrigger) {
|
|
84
|
+
await this.executor.postOperation(entity, selection2, context, option);
|
|
85
|
+
} */
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
await context.rollback();
|
|
89
|
+
throw err;
|
|
90
|
+
}
|
|
91
|
+
if (autoCommit) {
|
|
92
|
+
await context.commit();
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
registerTrigger(trigger) {
|
|
97
|
+
this.executor.registerTrigger(trigger);
|
|
98
|
+
}
|
|
99
|
+
registerChecker(checker) {
|
|
100
|
+
this.executor.registerChecker(checker);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.DbStore = DbStore;
|
package/lib/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { AppLoader } from './AppLoader';
|
|
1
|
+
export { AppLoader } from './AppLoader';
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AppLoader = void 0;
|
|
4
|
-
var AppLoader_1 = require("./AppLoader");
|
|
5
|
-
Object.defineProperty(exports, "AppLoader", { enumerable: true, get: function () { return AppLoader_1.AppLoader; } });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AppLoader = void 0;
|
|
4
|
+
var AppLoader_1 = require("./AppLoader");
|
|
5
|
+
Object.defineProperty(exports, "AppLoader", { enumerable: true, get: function () { return AppLoader_1.AppLoader; } });
|
package/lib/polyfill.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare type GenerateIdOption = {
|
|
2
|
-
shuffle?: boolean;
|
|
3
|
-
};
|
|
1
|
+
export declare type GenerateIdOption = {
|
|
2
|
+
shuffle?: boolean;
|
|
3
|
+
};
|
package/lib/polyfill.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const uuid_1 = require("uuid");
|
|
4
|
-
async function generateNewId(option) {
|
|
5
|
-
if (option?.shuffle && process.env.NODE_ENV === 'development') {
|
|
6
|
-
return (0, uuid_1.v4)();
|
|
7
|
-
}
|
|
8
|
-
return (0, uuid_1.v1)();
|
|
9
|
-
}
|
|
10
|
-
Object.assign(global, {
|
|
11
|
-
generateNewId,
|
|
12
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const uuid_1 = require("uuid");
|
|
4
|
+
async function generateNewId(option) {
|
|
5
|
+
if (option?.shuffle && process.env.NODE_ENV === 'development') {
|
|
6
|
+
return (0, uuid_1.v4)();
|
|
7
|
+
}
|
|
8
|
+
return (0, uuid_1.v1)();
|
|
9
|
+
}
|
|
10
|
+
Object.assign(global, {
|
|
11
|
+
generateNewId,
|
|
12
|
+
});
|
package/package.json
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "oak-backend-base",
|
|
3
|
-
"version": "2.0
|
|
4
|
-
"description": "oak-backend-base",
|
|
5
|
-
"main": "lib/index",
|
|
6
|
-
"author": {
|
|
7
|
-
"name": "XuChang"
|
|
8
|
-
},
|
|
9
|
-
"files": [
|
|
10
|
-
"lib/**/*"
|
|
11
|
-
],
|
|
12
|
-
"scripts": {
|
|
13
|
-
"copy-files": "copyfiles -u 1 src/**/*.json lib/",
|
|
14
|
-
"test": "ts-node test/test.ts",
|
|
15
|
-
"build": "tsc && npm run copy-files"
|
|
16
|
-
},
|
|
17
|
-
"dependencies": {
|
|
18
|
-
"lodash": "^4.17.21",
|
|
19
|
-
"mysql": "^2.18.1",
|
|
20
|
-
"mysql2": "^2.3.3",
|
|
21
|
-
"oak-common-aspect": "^2.0
|
|
22
|
-
"oak-db": "^2.
|
|
23
|
-
"oak-domain": "^2.0
|
|
24
|
-
"uuid": "^8.3.2"
|
|
25
|
-
},
|
|
26
|
-
"license": "ISC",
|
|
27
|
-
"devDependencies": {
|
|
28
|
-
"@types/node": "^17.0.40",
|
|
29
|
-
"@types/uuid": "^8.3.4",
|
|
30
|
-
"copyfiles": "^2.4.1",
|
|
31
|
-
"ts-node": "~10.9.1",
|
|
32
|
-
"tslib": "^2.4.0",
|
|
33
|
-
"typescript": "~4.7.4"
|
|
34
|
-
}
|
|
35
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "oak-backend-base",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "oak-backend-base",
|
|
5
|
+
"main": "lib/index",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "XuChang"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"lib/**/*"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"copy-files": "copyfiles -u 1 src/**/*.json lib/",
|
|
14
|
+
"test": "ts-node test/test.ts",
|
|
15
|
+
"build": "tsc && npm run copy-files"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"lodash": "^4.17.21",
|
|
19
|
+
"mysql": "^2.18.1",
|
|
20
|
+
"mysql2": "^2.3.3",
|
|
21
|
+
"oak-common-aspect": "^2.1.0",
|
|
22
|
+
"oak-db": "^2.1.1",
|
|
23
|
+
"oak-domain": "^2.1.0",
|
|
24
|
+
"uuid": "^8.3.2"
|
|
25
|
+
},
|
|
26
|
+
"license": "ISC",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^17.0.40",
|
|
29
|
+
"@types/uuid": "^8.3.4",
|
|
30
|
+
"copyfiles": "^2.4.1",
|
|
31
|
+
"ts-node": "~10.9.1",
|
|
32
|
+
"tslib": "^2.4.0",
|
|
33
|
+
"typescript": "~4.7.4"
|
|
34
|
+
}
|
|
35
|
+
}
|