oak-backend-base 2.1.0 → 2.2.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.
@@ -14,4 +14,6 @@ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt exten
14
14
  initialize(dropIfExists?: boolean): Promise<void>;
15
15
  getStore(): DbStore<ED, Cxt>;
16
16
  startWatchers(): void;
17
+ startTimers(): void;
18
+ execStartRoutines(): Promise<void>;
17
19
  }
package/lib/AppLoader.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AppLoader = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const node_schedule_1 = require("node-schedule");
5
6
  const actionDef_1 = require("oak-domain/lib/store/actionDef");
6
7
  const checkers_1 = require("oak-domain/lib/checkers");
7
8
  const triggers_1 = require("oak-domain/lib/triggers");
@@ -10,8 +11,8 @@ const types_1 = require("oak-domain/lib/types");
10
11
  const DbStore_1 = require("./DbStore");
11
12
  const index_1 = tslib_1.__importDefault(require("oak-common-aspect/lib/index"));
12
13
  function initTriggers(dbStore, path) {
13
- const { triggers } = require(`${path}/lib/triggers/index`);
14
- const { checkers } = require(`${path}/lib/checkers/index`);
14
+ const triggers = require(`${path}/lib/triggers/index`);
15
+ const checkers = require(`${path}/lib/checkers/index`);
15
16
  const { ActionDefDict } = require(`${path}/lib/oak-app-domain/ActionDefDict`);
16
17
  const { triggers: adTriggers, checkers: adCheckers } = (0, actionDef_1.analyzeActionDefDict)(dbStore.getSchema(), ActionDefDict);
17
18
  triggers.forEach((trigger) => dbStore.registerTrigger(trigger));
@@ -24,7 +25,7 @@ function initTriggers(dbStore, path) {
24
25
  dynamicTriggers.forEach((trigger) => dbStore.registerTrigger(trigger));
25
26
  }
26
27
  function startWatchers(dbStore, path, contextBuilder) {
27
- const { watchers } = require(`${path}/lib/watchers/index`);
28
+ const watchers = require(`${path}/lib/watchers/index`);
28
29
  const { ActionDefDict } = require(`${path}/lib/oak-app-domain/ActionDefDict`);
29
30
  const { watchers: adWatchers } = (0, actionDef_1.analyzeActionDefDict)(dbStore.getSchema(), ActionDefDict);
30
31
  const totalWatchers = watchers.concat(adWatchers);
@@ -84,7 +85,7 @@ class AppLoader extends types_1.AppLoader {
84
85
  constructor(path, contextBuilder, dbConfig) {
85
86
  super(path);
86
87
  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.aspectDict = Object.assign({}, index_1.default, require(`${path}/lib/aspects/index`));
88
89
  this.dbStore = new DbStore_1.DbStore(storageSchema, contextBuilder, dbConfig);
89
90
  this.contextBuilder = contextBuilder;
90
91
  }
@@ -107,7 +108,7 @@ class AppLoader extends types_1.AppLoader {
107
108
  }
108
109
  async initialize(dropIfExists) {
109
110
  await this.dbStore.initialize(dropIfExists);
110
- const { data } = require(`${this.path}/lib/data/index`);
111
+ const data = require(`${this.path}/lib/data/index`);
111
112
  const context = await this.contextBuilder()(this.dbStore);
112
113
  await context.begin();
113
114
  for (const entity in data) {
@@ -134,5 +135,44 @@ class AppLoader extends types_1.AppLoader {
134
135
  startWatchers() {
135
136
  startWatchers(this.dbStore, this.path, this.contextBuilder);
136
137
  }
138
+ startTimers() {
139
+ const timers = require(`${this.path}/lib/timer/index`);
140
+ for (const timer of timers) {
141
+ const { cron, fn, name } = timer;
142
+ (0, node_schedule_1.scheduleJob)(name, cron, async (date) => {
143
+ const start = Date.now();
144
+ const context = await this.contextBuilder()(this.dbStore);
145
+ await context.begin();
146
+ console.log(`定时器【${name}】开始执行,时间是【${date.toLocaleTimeString()}】`);
147
+ try {
148
+ const result = await fn(context);
149
+ console.log(`定时器【${name}】执行完成,耗时${Date.now() - start}毫秒,结果是【${result}】`);
150
+ await context.commit();
151
+ }
152
+ catch (err) {
153
+ console.warn(`定时器【${name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
154
+ await context.rollback();
155
+ }
156
+ });
157
+ }
158
+ }
159
+ async execStartRoutines() {
160
+ const routines = require(`${this.path}/lib/routines/start`);
161
+ for (const routine of routines) {
162
+ const { name, fn } = routine;
163
+ const context = await this.contextBuilder()(this.dbStore);
164
+ const start = Date.now();
165
+ await context.begin();
166
+ try {
167
+ const result = await fn(context);
168
+ console.log(`例程【${name}】执行完成,耗时${Date.now() - start}毫秒,结果是【${result}】`);
169
+ await context.commit();
170
+ }
171
+ catch (err) {
172
+ console.warn(`例程【${name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
173
+ await context.rollback();
174
+ }
175
+ }
176
+ }
137
177
  }
138
178
  exports.AppLoader = AppLoader;
package/lib/DbStore.js CHANGED
@@ -44,16 +44,16 @@ class DbStore extends oak_db_1.MysqlStore {
44
44
  }
45
45
  let result;
46
46
  // select的trigger应加在根select之前,cascade的select不加处理
47
- const selection2 = Object.assign({
47
+ Object.assign(selection, {
48
48
  action: 'select',
49
- }, selection);
49
+ });
50
50
  if (!option.blockTrigger) {
51
- await this.executor.preOperation(entity, selection2, context, option);
51
+ await this.executor.preOperation(entity, selection, context, option);
52
52
  }
53
53
  try {
54
54
  result = await super.select(entity, selection, context, option);
55
55
  if (!option.blockTrigger) {
56
- await this.executor.postOperation(entity, selection2, context, option, result);
56
+ await this.executor.postOperation(entity, selection, context, option, result);
57
57
  }
58
58
  }
59
59
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-backend-base",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "oak-backend-base",
5
5
  "main": "lib/index",
6
6
  "author": {
@@ -15,12 +15,14 @@
15
15
  "build": "tsc && npm run copy-files"
16
16
  },
17
17
  "dependencies": {
18
+ "@types/node-schedule": "^2.1.0",
18
19
  "lodash": "^4.17.21",
19
20
  "mysql": "^2.18.1",
20
21
  "mysql2": "^2.3.3",
21
- "oak-common-aspect": "^2.1.0",
22
+ "node-schedule": "^2.1.0",
23
+ "oak-common-aspect": "^2.1.1",
22
24
  "oak-db": "^2.1.1",
23
- "oak-domain": "^2.1.0",
25
+ "oak-domain": "^2.2.0",
24
26
  "uuid": "^8.3.2"
25
27
  },
26
28
  "license": "ISC",