oak-backend-base 2.3.4 → 2.4.2

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.
@@ -1,21 +1,25 @@
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
- import { Endpoint } from 'oak-domain/lib/types/Endpoint';
7
- export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends GeneralAppLoader<ED, Cxt> {
8
- private dbStore;
9
- private aspectDict;
10
- private contextBuilder;
11
- constructor(path: string, contextBuilder: (scene?: string) => (store: DbStore<ED, Cxt>) => Promise<Cxt>, dbConfig: MySQLConfiguration);
12
- mount(initialize?: true): Promise<void>;
13
- unmount(): Promise<void>;
14
- execAspect(name: string, context: Cxt, params?: any): Promise<any>;
15
- initialize(dropIfExists?: boolean): Promise<void>;
16
- getStore(): DbStore<ED, Cxt>;
17
- getEndpoints(): Record<string, Endpoint<ED, Cxt>>;
18
- startWatchers(): void;
19
- startTimers(): void;
20
- execStartRoutines(): Promise<void>;
21
- }
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
+ import { Endpoint } from 'oak-domain/lib/types/Endpoint';
7
+ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends GeneralAppLoader<ED, Cxt> {
8
+ private dbStore;
9
+ private aspectDict;
10
+ private externalDependencies;
11
+ private contextBuilder;
12
+ private requireSth;
13
+ constructor(path: string, contextBuilder: (scene?: string) => (store: DbStore<ED, Cxt>) => Promise<Cxt>, dbConfig: MySQLConfiguration);
14
+ initTriggers(): void;
15
+ startWatchers(): void;
16
+ mount(initialize?: true): Promise<void>;
17
+ unmount(): Promise<void>;
18
+ execAspect(name: string, context: Cxt, params?: any): Promise<any>;
19
+ initialize(dropIfExists?: boolean): Promise<void>;
20
+ getStore(): DbStore<ED, Cxt>;
21
+ getEndpoints(): Record<string, Endpoint<ED, Cxt>>;
22
+ startTimers(): void;
23
+ execStartRoutines(): Promise<void>;
24
+ execRoutine(routine: (context: Cxt) => Promise<void>): Promise<void>;
25
+ }
package/lib/AppLoader.js CHANGED
@@ -1,188 +1,225 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AppLoader = void 0;
4
- const tslib_1 = require("tslib");
5
- const node_schedule_1 = require("node-schedule");
6
- const actionDef_1 = require("oak-domain/lib/store/actionDef");
7
- const checkers_1 = require("oak-domain/lib/checkers");
8
- const triggers_1 = require("oak-domain/lib/triggers");
9
- const uuid_1 = require("oak-domain/lib/utils/uuid");
10
- const types_1 = require("oak-domain/lib/types");
11
- const DbStore_1 = require("./DbStore");
12
- const index_1 = tslib_1.__importStar(require("oak-common-aspect/lib/index"));
13
- function initTriggers(dbStore, path) {
14
- const triggers = require(`${path}/lib/triggers/index`).default;
15
- const checkers = require(`${path}/lib/checkers/index`).default;
16
- const authDict = require(`${path}/lib/auth/index`).default;
17
- const { ActionDefDict } = require(`${path}/lib/oak-app-domain/ActionDefDict`);
18
- const { triggers: adTriggers, checkers: adCheckers } = (0, actionDef_1.analyzeActionDefDict)(dbStore.getSchema(), ActionDefDict);
19
- triggers.forEach((trigger) => dbStore.registerTrigger(trigger));
20
- adTriggers.forEach((trigger) => dbStore.registerTrigger(trigger));
21
- checkers.forEach((checker) => dbStore.registerChecker(checker));
22
- adCheckers.forEach((checker) => dbStore.registerChecker(checker));
23
- const dynamicCheckers = (0, checkers_1.createDynamicCheckers)(dbStore.getSchema(), authDict);
24
- dynamicCheckers.forEach((checker) => dbStore.registerChecker(checker));
25
- const dynamicTriggers = (0, triggers_1.createDynamicTriggers)(dbStore.getSchema());
26
- dynamicTriggers.forEach((trigger) => dbStore.registerTrigger(trigger));
27
- }
28
- function startWatchers(dbStore, path, contextBuilder) {
29
- const watchers = require(`${path}/lib/watchers/index`).default;
30
- const { ActionDefDict } = require(`${path}/lib/oak-app-domain/ActionDefDict`);
31
- const { watchers: adWatchers } = (0, actionDef_1.analyzeActionDefDict)(dbStore.getSchema(), ActionDefDict);
32
- const totalWatchers = watchers.concat(adWatchers);
33
- let count = 0;
34
- const doWatchers = async () => {
35
- count++;
36
- const start = Date.now();
37
- const context = await contextBuilder()(dbStore);
38
- for (const w of totalWatchers) {
39
- await context.begin();
40
- try {
41
- if (w.hasOwnProperty('actionData')) {
42
- const { entity, action, filter, actionData } = w;
43
- const filter2 = typeof filter === 'function' ? await filter() : filter;
44
- const data = typeof actionData === 'function' ? await actionData() : actionData; // 这里有个奇怪的编译错误,不理解 by Xc
45
- const result = await dbStore.operate(entity, {
46
- id: await (0, uuid_1.generateNewIdAsync)(),
47
- action,
48
- data,
49
- filter: filter2
50
- }, context, {
51
- dontCollect: true,
52
- });
53
- console.log(`执行了watcher【${w.name}】,结果是:`, result);
54
- }
55
- else {
56
- const { entity, projection, fn, filter } = w;
57
- const filter2 = typeof filter === 'function' ? await filter() : filter;
58
- const projection2 = typeof projection === 'function' ? await projection() : projection;
59
- const rows = await dbStore.select(entity, {
60
- data: projection2,
61
- filter: filter2,
62
- }, context, {
63
- dontCollect: true,
64
- blockTrigger: true,
65
- });
66
- const result = await fn(context, rows);
67
- console.log(`执行了watcher【${w.name}】,结果是:`, result);
68
- }
69
- await context.commit();
70
- }
71
- catch (err) {
72
- await context.rollback();
73
- console.error(`执行了watcher【${w.name}】,发生错误:`, err);
74
- }
75
- }
76
- const duration = Date.now() - start;
77
- console.log(`第${count}次执行watchers,共执行${watchers.length}个,耗时${duration}毫秒`);
78
- setTimeout(() => doWatchers(), 120000);
79
- };
80
- doWatchers();
81
- }
82
- class AppLoader extends types_1.AppLoader {
83
- dbStore;
84
- aspectDict;
85
- contextBuilder;
86
- constructor(path, contextBuilder, dbConfig) {
87
- super(path);
88
- const { storageSchema } = require(`${path}/lib/oak-app-domain/Storage`);
89
- this.aspectDict = Object.assign({}, index_1.default, require(`${path}/lib/aspects/index`).default);
90
- this.dbStore = new DbStore_1.DbStore(storageSchema, contextBuilder, dbConfig);
91
- this.contextBuilder = contextBuilder;
92
- }
93
- async mount(initialize) {
94
- const { path } = this;
95
- if (!initialize) {
96
- initTriggers(this.dbStore, path);
97
- }
98
- const { importations, exportations } = require(`${path}/lib/ports/index`);
99
- (0, index_1.registerPorts)(importations || [], exportations || []);
100
- this.dbStore.connect();
101
- }
102
- async unmount() {
103
- (0, index_1.clearPorts)();
104
- this.dbStore.disconnect();
105
- }
106
- async execAspect(name, context, params) {
107
- const fn = this.aspectDict[name];
108
- if (!fn) {
109
- throw new Error(`不存在的接口名称: ${name}`);
110
- }
111
- return await fn(params, context);
112
- }
113
- async initialize(dropIfExists) {
114
- await this.dbStore.initialize(dropIfExists);
115
- const data = require(`${this.path}/lib/data/index`).default;
116
- const context = await this.contextBuilder()(this.dbStore);
117
- await context.begin();
118
- for (const entity in data) {
119
- let rows = data[entity];
120
- if (entity === 'area') {
121
- // 对area暂时处理一下
122
- rows = require('./data/area.json');
123
- }
124
- if (rows.length > 0) {
125
- await this.dbStore.operate(entity, {
126
- data: rows,
127
- action: 'create',
128
- }, context, {
129
- dontCollect: true,
130
- dontCreateOper: true,
131
- });
132
- console.log(`data in ${entity} initialized!`);
133
- }
134
- }
135
- await context.commit();
136
- this.dbStore.disconnect();
137
- }
138
- getStore() {
139
- return this.dbStore;
140
- }
141
- getEndpoints() {
142
- const endpoints = require(`${this.path}/lib/endpoints/index`).default;
143
- return endpoints;
144
- }
145
- startWatchers() {
146
- startWatchers(this.dbStore, this.path, this.contextBuilder);
147
- }
148
- startTimers() {
149
- const timers = require(`${this.path}/lib/timers/index`).default;
150
- for (const timer of timers) {
151
- const { cron, fn, name } = timer;
152
- (0, node_schedule_1.scheduleJob)(name, cron, async (date) => {
153
- const start = Date.now();
154
- const context = await this.contextBuilder()(this.dbStore);
155
- await context.begin();
156
- console.log(`定时器【${name}】开始执行,时间是【${date.toLocaleTimeString()}】`);
157
- try {
158
- const result = await fn(context);
159
- console.log(`定时器【${name}】执行完成,耗时${Date.now() - start}毫秒,结果是【${result}】`);
160
- await context.commit();
161
- }
162
- catch (err) {
163
- console.warn(`定时器【${name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
164
- await context.rollback();
165
- }
166
- });
167
- }
168
- }
169
- async execStartRoutines() {
170
- const routines = require(`${this.path}/lib/routines/start`).default;
171
- for (const routine of routines) {
172
- const { name, fn } = routine;
173
- const context = await this.contextBuilder()(this.dbStore);
174
- const start = Date.now();
175
- await context.begin();
176
- try {
177
- const result = await fn(context);
178
- console.log(`例程【${name}】执行完成,耗时${Date.now() - start}毫秒,结果是【${result}】`);
179
- await context.commit();
180
- }
181
- catch (err) {
182
- console.warn(`例程【${name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
183
- await context.rollback();
184
- }
185
- }
186
- }
187
- }
188
- exports.AppLoader = AppLoader;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AppLoader = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fs_1 = require("fs");
6
+ const path_1 = require("path");
7
+ const node_schedule_1 = require("node-schedule");
8
+ const actionDef_1 = require("oak-domain/lib/store/actionDef");
9
+ 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
+ const uuid_1 = require("oak-domain/lib/utils/uuid");
13
+ const types_1 = require("oak-domain/lib/types");
14
+ const DbStore_1 = require("./DbStore");
15
+ const index_1 = tslib_1.__importStar(require("oak-common-aspect/lib/index"));
16
+ const assert_1 = tslib_1.__importDefault(require("assert"));
17
+ class AppLoader extends types_1.AppLoader {
18
+ dbStore;
19
+ aspectDict;
20
+ externalDependencies;
21
+ contextBuilder;
22
+ requireSth(filePath) {
23
+ const sth = require((0, path_1.join)(this.path, filePath)).default;
24
+ const sthExternal = this.externalDependencies.map(ele => {
25
+ const depFilePath = (0, path_1.join)(this.path, 'node_modules', ele, filePath);
26
+ if ((0, fs_1.existsSync)(`${depFilePath}.js`)) {
27
+ return require(depFilePath).default;
28
+ }
29
+ }).filter(ele => !!ele);
30
+ if (sth instanceof Array) {
31
+ sthExternal.forEach((sth2, idx) => {
32
+ (0, assert_1.default)(sth2 instanceof Array, `${(0, path_1.join)(this.path, 'node_modules', this.externalDependencies[idx], filePath)}中的default输出对象不是数组,与项目对应路径的输出不一致`);
33
+ sth.push(...sth2);
34
+ });
35
+ return sth;
36
+ }
37
+ (0, assert_1.default)(typeof sth === 'object');
38
+ const sthOut = {};
39
+ sthExternal.forEach((sth2, idx) => {
40
+ (0, assert_1.default)(typeof sth2 === 'object' && !(sth2 instanceof Array), `${(0, path_1.join)(this.path, 'node_modules', this.externalDependencies[idx], filePath)}中的default输出对象不是非数组对象,与项目对应路径的输出不一致`);
41
+ const inter = (0, lodash_1.intersection)(Object.keys(sthOut), Object.keys(sth2));
42
+ if (inter.length > 0) {
43
+ console.warn(`${(0, path_1.join)(this.path, 'node_modules', this.externalDependencies[idx], filePath)}中的default输出对象中的key值【${inter.join(',')}】与其它对应路径输出的key值有冲突,请仔细检查避免错误`);
44
+ }
45
+ Object.assign(sthOut, sth2);
46
+ });
47
+ const inter = (0, lodash_1.intersection)(Object.keys(sthOut), Object.keys(sth));
48
+ (0, assert_1.default)(inter.length === 0, `项目${filePath}中的default输出与第三方库中的输出在键值${inter.join(',')}上冲突,请处理`);
49
+ Object.assign(sthOut, sth);
50
+ return sthOut;
51
+ }
52
+ constructor(path, contextBuilder, dbConfig) {
53
+ super(path);
54
+ const { storageSchema } = require(`${path}/lib/oak-app-domain/Storage`);
55
+ this.externalDependencies = require(`${path}/lib/config/externalDependencies`).default;
56
+ this.aspectDict = Object.assign({}, index_1.default, this.requireSth('lib/aspects/index'));
57
+ this.dbStore = new DbStore_1.DbStore(storageSchema, contextBuilder, dbConfig);
58
+ this.contextBuilder = contextBuilder;
59
+ }
60
+ initTriggers() {
61
+ const triggers = this.requireSth('lib/triggers/index');
62
+ const checkers = this.requireSth('lib/checkers/index');
63
+ const authDict = this.requireSth('lib/auth/index');
64
+ const { ActionDefDict } = require(`${this.path}/lib/oak-app-domain/ActionDefDict`);
65
+ const { triggers: adTriggers, checkers: adCheckers } = (0, actionDef_1.analyzeActionDefDict)(this.dbStore.getSchema(), ActionDefDict);
66
+ triggers.forEach((trigger) => this.dbStore.registerTrigger(trigger));
67
+ adTriggers.forEach((trigger) => this.dbStore.registerTrigger(trigger));
68
+ checkers.forEach((checker) => this.dbStore.registerChecker(checker));
69
+ 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
+ }
75
+ startWatchers() {
76
+ const watchers = this.requireSth('lib/watchers/index');
77
+ const { ActionDefDict } = require(`${this.path}/lib/oak-app-domain/ActionDefDict`);
78
+ const { watchers: adWatchers } = (0, actionDef_1.analyzeActionDefDict)(this.dbStore.getSchema(), ActionDefDict);
79
+ const totalWatchers = watchers.concat(adWatchers);
80
+ let count = 0;
81
+ const doWatchers = async () => {
82
+ count++;
83
+ const start = Date.now();
84
+ const context = await this.contextBuilder()(this.dbStore);
85
+ for (const w of totalWatchers) {
86
+ await context.begin();
87
+ try {
88
+ if (w.hasOwnProperty('actionData')) {
89
+ const { entity, action, filter, actionData } = w;
90
+ const filter2 = typeof filter === 'function' ? await filter() : filter;
91
+ const data = typeof actionData === 'function' ? await actionData() : actionData; // 这里有个奇怪的编译错误,不理解 by Xc
92
+ const result = await this.dbStore.operate(entity, {
93
+ id: await (0, uuid_1.generateNewIdAsync)(),
94
+ action,
95
+ data,
96
+ filter: filter2
97
+ }, context, {
98
+ dontCollect: true,
99
+ });
100
+ console.log(`执行了watcher【${w.name}】,结果是:`, result);
101
+ }
102
+ else {
103
+ const { entity, projection, fn, filter } = w;
104
+ const filter2 = typeof filter === 'function' ? await filter() : filter;
105
+ const projection2 = typeof projection === 'function' ? await projection() : projection;
106
+ const rows = await this.dbStore.select(entity, {
107
+ data: projection2,
108
+ filter: filter2,
109
+ }, context, {
110
+ dontCollect: true,
111
+ blockTrigger: true,
112
+ });
113
+ const result = await fn(context, rows);
114
+ console.log(`执行了watcher【${w.name}】,结果是:`, result);
115
+ }
116
+ await context.commit();
117
+ }
118
+ catch (err) {
119
+ await context.rollback();
120
+ console.error(`执行了watcher【${w.name}】,发生错误:`, err);
121
+ }
122
+ }
123
+ const duration = Date.now() - start;
124
+ console.log(`第${count}次执行watchers,共执行${watchers.length}个,耗时${duration}毫秒`);
125
+ setTimeout(() => doWatchers(), 120000);
126
+ };
127
+ doWatchers();
128
+ }
129
+ async mount(initialize) {
130
+ const { path } = this;
131
+ if (!initialize) {
132
+ this.initTriggers();
133
+ }
134
+ const { importations, exportations } = require(`${path}/lib/ports/index`);
135
+ (0, index_1.registerPorts)(importations || [], exportations || []);
136
+ this.dbStore.connect();
137
+ }
138
+ async unmount() {
139
+ (0, index_1.clearPorts)();
140
+ this.dbStore.disconnect();
141
+ }
142
+ async execAspect(name, context, params) {
143
+ const fn = this.aspectDict[name];
144
+ if (!fn) {
145
+ throw new Error(`不存在的接口名称: ${name}`);
146
+ }
147
+ return await fn(params, context);
148
+ }
149
+ async initialize(dropIfExists) {
150
+ await this.dbStore.initialize(dropIfExists);
151
+ const data = this.requireSth('lib/data/index');
152
+ const context = await this.contextBuilder()(this.dbStore);
153
+ await context.begin();
154
+ for (const entity in data) {
155
+ let rows = data[entity];
156
+ if (entity === 'area') {
157
+ // 对area暂时处理一下
158
+ rows = require('./data/area.json');
159
+ }
160
+ if (rows.length > 0) {
161
+ await this.dbStore.operate(entity, {
162
+ data: rows,
163
+ action: 'create',
164
+ }, context, {
165
+ dontCollect: true,
166
+ dontCreateOper: true,
167
+ });
168
+ console.log(`data in ${entity} initialized!`);
169
+ }
170
+ }
171
+ await context.commit();
172
+ this.dbStore.disconnect();
173
+ }
174
+ getStore() {
175
+ return this.dbStore;
176
+ }
177
+ getEndpoints() {
178
+ const endpoints = this.requireSth('lib/endpoints/index');
179
+ return endpoints;
180
+ }
181
+ startTimers() {
182
+ const timers = this.requireSth('lib/timers/index');
183
+ for (const timer of timers) {
184
+ const { cron, fn, name } = timer;
185
+ (0, node_schedule_1.scheduleJob)(name, cron, async (date) => {
186
+ const start = Date.now();
187
+ const context = await this.contextBuilder()(this.dbStore);
188
+ await context.begin();
189
+ console.log(`定时器【${name}】开始执行,时间是【${date.toLocaleTimeString()}】`);
190
+ try {
191
+ const result = await fn(context);
192
+ console.log(`定时器【${name}】执行完成,耗时${Date.now() - start}毫秒,结果是【${result}】`);
193
+ await context.commit();
194
+ }
195
+ catch (err) {
196
+ console.warn(`定时器【${name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
197
+ await context.rollback();
198
+ }
199
+ });
200
+ }
201
+ }
202
+ async execStartRoutines() {
203
+ const routines = this.requireSth('lib/routines/start');
204
+ for (const routine of routines) {
205
+ const { name, fn } = routine;
206
+ const context = await this.contextBuilder()(this.dbStore);
207
+ const start = Date.now();
208
+ await context.begin();
209
+ try {
210
+ const result = await fn(context);
211
+ console.log(`例程【${name}】执行完成,耗时${Date.now() - start}毫秒,结果是【${result}】`);
212
+ await context.commit();
213
+ }
214
+ catch (err) {
215
+ console.warn(`例程【${name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
216
+ await context.rollback();
217
+ }
218
+ }
219
+ }
220
+ async execRoutine(routine) {
221
+ const context = await this.contextBuilder()(this.dbStore);
222
+ await routine(context);
223
+ }
224
+ }
225
+ 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 = await 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
- Object.assign(selection, {
48
- action: 'select',
49
- });
50
- if (!option.blockTrigger) {
51
- await this.executor.preOperation(entity, selection, context, option);
52
- }
53
- try {
54
- result = await super.select(entity, selection, context, option);
55
- if (!option.blockTrigger) {
56
- await this.executor.postOperation(entity, selection, 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 = await 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
+ Object.assign(selection, {
48
+ action: 'select',
49
+ });
50
+ if (!option.blockTrigger) {
51
+ await this.executor.preOperation(entity, selection, context, option);
52
+ }
53
+ try {
54
+ result = await super.select(entity, selection, context, option);
55
+ if (!option.blockTrigger) {
56
+ await this.executor.postOperation(entity, selection, 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,37 +1,37 @@
1
- {
2
- "name": "oak-backend-base",
3
- "version": "2.3.4",
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
- "@types/node-schedule": "^2.1.0",
19
- "lodash": "^4.17.21",
20
- "mysql": "^2.18.1",
21
- "mysql2": "^2.3.3",
22
- "node-schedule": "^2.1.0",
23
- "oak-common-aspect": "^2.1.5",
24
- "oak-db": "^2.2.5",
25
- "oak-domain": "^2.4.3",
26
- "uuid": "^8.3.2"
27
- },
28
- "license": "ISC",
29
- "devDependencies": {
30
- "@types/node": "^17.0.40",
31
- "@types/uuid": "^8.3.4",
32
- "copyfiles": "^2.4.1",
33
- "ts-node": "~10.9.1",
34
- "tslib": "^2.4.0",
35
- "typescript": "~4.7.4"
36
- }
37
- }
1
+ {
2
+ "name": "oak-backend-base",
3
+ "version": "2.4.2",
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
+ "@types/node-schedule": "^2.1.0",
19
+ "lodash": "^4.17.21",
20
+ "mysql": "^2.18.1",
21
+ "mysql2": "^2.3.3",
22
+ "node-schedule": "^2.1.0",
23
+ "oak-common-aspect": "^2.1.9",
24
+ "oak-db": "^2.2.11",
25
+ "oak-domain": "^2.6.9",
26
+ "uuid": "^8.3.2"
27
+ },
28
+ "license": "ISC",
29
+ "devDependencies": {
30
+ "@types/node": "^17.0.40",
31
+ "@types/uuid": "^8.3.4",
32
+ "copyfiles": "^2.4.1",
33
+ "ts-node": "~10.9.1",
34
+ "tslib": "^2.4.0",
35
+ "typescript": "~4.7.4"
36
+ }
37
+ }