oak-backend-base 4.1.0 → 4.1.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.
@@ -31,7 +31,7 @@ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt exten
31
31
  result: any;
32
32
  message?: string;
33
33
  }>;
34
- initialize(truncate?: boolean): Promise<void>;
34
+ initialize(): Promise<void>;
35
35
  getStore(): DbStore<ED, Cxt>;
36
36
  getEndpoints(prefix: string): [string, "post" | "get" | "put" | "delete", string, (params: Record<string, string>, headers: IncomingHttpHeaders, req: IncomingMessage, body?: any) => Promise<any>][];
37
37
  protected operateInWatcher<T extends keyof ED>(entity: T, operation: ED[T]['Update'], context: Cxt): Promise<OperationResult<ED>>;
package/lib/AppLoader.js CHANGED
@@ -11,7 +11,6 @@ const uuid_1 = require("oak-domain/lib/utils/uuid");
11
11
  const types_1 = require("oak-domain/lib/types");
12
12
  const DbStore_1 = require("./DbStore");
13
13
  const index_1 = tslib_1.__importStar(require("oak-common-aspect/lib/index"));
14
- const assert_1 = tslib_1.__importDefault(require("assert"));
15
14
  const dependencyBuilder_1 = require("oak-domain/lib/compiler/dependencyBuilder");
16
15
  const DataSubscriber_1 = tslib_1.__importDefault(require("./cluster/DataSubscriber"));
17
16
  const env_1 = require("./cluster/env");
@@ -94,7 +93,9 @@ class AppLoader extends types_1.AppLoader {
94
93
  Object.keys(eventOperationMap).forEach((event) => {
95
94
  const ids = eventOperationMap[event];
96
95
  const opRecordsToPublish = opRecords.filter((ele) => !!ele.id && ids.includes(ele.id));
97
- (0, assert_1.default)(opRecordsToPublish.length === ids.length, '要推送的事件的operation数量不足,请检查确保');
96
+ if (opRecordsToPublish.length !== ids.length && process.env.NODE_ENV === 'development') {
97
+ console.warn('要推送的事件的operation数量不足event事件中记录的数据,请检查是否有空operation被加入了推送事件');
98
+ }
98
99
  loaderThis.dataSubscriber.publishEvent(event, opRecordsToPublish, this.getSubscriberId());
99
100
  });
100
101
  }
@@ -163,28 +164,49 @@ class AppLoader extends types_1.AppLoader {
163
164
  throw err;
164
165
  }
165
166
  }
166
- async initialize(truncate) {
167
+ async initialize() {
167
168
  await this.dbStore.initialize({ ifExists: 'dropIfNotStatic' });
168
169
  const data = this.requireSth('lib/data/index');
169
170
  const context = this.contextBuilder(this.dbStore);
171
+ context.openRootMode();
170
172
  for (const entity in data) {
171
173
  let rows = data[entity];
172
- if (entity === 'area') {
173
- // 对area暂时处理一下
174
- rows = require('./data/area.json');
175
- }
176
- if (rows.length > 0 && (!truncate || !this.dbStore.getSchema()[entity].static)) {
174
+ if (rows.length > 0) {
177
175
  await context.begin();
178
- context.openRootMode();
176
+ // 如果是static的对象,只要表中有数据就pass
177
+ const [first] = await this.dbStore.select(entity, {
178
+ data: {
179
+ id: 1,
180
+ },
181
+ indexFrom: 0,
182
+ count: 1,
183
+ }, context, {});
184
+ if (this.dbStore.getSchema()[entity].static) {
185
+ if (first) {
186
+ await context.commit();
187
+ console.log(`data in ${entity} omitted, ${rows.length} rows passed`);
188
+ continue;
189
+ }
190
+ }
191
+ // 再插入所有的行
179
192
  try {
180
- await this.dbStore.operate(entity, {
181
- data: rows,
182
- action: 'create',
183
- }, context, {
184
- dontCollect: true,
185
- dontCreateOper: true,
186
- blockTrigger: true,
187
- });
193
+ const insertRows = async (idx) => {
194
+ const rows2 = rows.slice(idx, 1000);
195
+ if (rows2.length > 0) {
196
+ await this.dbStore.operate(entity, {
197
+ data: rows,
198
+ action: 'create',
199
+ }, context, {
200
+ dontCollect: true,
201
+ dontCreateOper: true,
202
+ blockTrigger: true,
203
+ });
204
+ if (rows2.length === 1000) {
205
+ await insertRows(idx + 1000);
206
+ }
207
+ }
208
+ };
209
+ await insertRows(0);
188
210
  await context.commit();
189
211
  console.log(`data in ${entity} initialized, ${rows.length} rows inserted`);
190
212
  }
@@ -249,14 +271,12 @@ class AppLoader extends types_1.AppLoader {
249
271
  return endPointRouters;
250
272
  }
251
273
  operateInWatcher(entity, operation, context) {
252
- return this.dbStore.operate(entity, operation, context, {
253
- dontCollect: true,
254
- });
274
+ return this.dbStore.operate(entity, operation, context, {});
255
275
  }
256
276
  selectInWatcher(entity, selection, context) {
257
277
  return this.dbStore.select(entity, selection, context, {
258
- dontCollect: true,
259
278
  blockTrigger: true,
279
+ forUpdate: true,
260
280
  });
261
281
  }
262
282
  async execWatcher(watcher) {
@@ -265,19 +285,19 @@ class AppLoader extends types_1.AppLoader {
265
285
  try {
266
286
  if (watcher.hasOwnProperty('actionData')) {
267
287
  const { entity, action, filter, actionData } = watcher;
268
- const filter2 = typeof filter === 'function' ? await filter() : filter;
269
- const data = typeof actionData === 'function' ? await (actionData)() : actionData;
288
+ const filter2 = typeof filter === 'function' ? await filter() : (0, lodash_1.cloneDeep)(filter);
289
+ const data = typeof actionData === 'function' ? await (actionData)() : (0, lodash_1.cloneDeep)(actionData);
270
290
  result = await this.operateInWatcher(entity, {
271
291
  id: await (0, uuid_1.generateNewIdAsync)(),
272
- action,
292
+ action: action,
273
293
  data,
274
294
  filter: filter2
275
295
  }, context);
276
296
  }
277
297
  else {
278
298
  const { entity, projection, fn, filter } = watcher;
279
- const filter2 = typeof filter === 'function' ? await filter() : filter;
280
- const projection2 = typeof projection === 'function' ? await projection() : projection;
299
+ const filter2 = typeof filter === 'function' ? await filter() : (0, lodash_1.cloneDeep)(filter);
300
+ const projection2 = typeof projection === 'function' ? await projection() : (0, lodash_1.cloneDeep)(projection);
281
301
  const rows = await this.selectInWatcher(entity, {
282
302
  data: projection2,
283
303
  filter: filter2,
@@ -102,12 +102,24 @@ class Synchronizer {
102
102
  }
103
103
  if (successIds.length > 0) {
104
104
  try {
105
- await Promise.all(successIds.map((id) => {
105
+ await Promise.all(successIds.map(async (id) => {
106
106
  const { onSynchronized, oper } = queue.find(ele => ele.oper.id === id);
107
+ const operEntityArr = await context.select('operEntity', {
108
+ data: {
109
+ id: 1,
110
+ entity: 1,
111
+ entityId: 1
112
+ },
113
+ filter: {
114
+ operId: oper.id,
115
+ }
116
+ }, {});
117
+ const entityIds = operEntityArr.map(ele => ele.entityId);
107
118
  return onSynchronized && onSynchronized({
108
119
  action: oper.action,
109
120
  data: oper.data,
110
- rowIds: (0, filter_1.getRelevantIds)(oper.filter),
121
+ // rowIds: getRelevantIds(oper.filter!),
122
+ rowIds: entityIds
111
123
  }, context);
112
124
  }));
113
125
  }
@@ -184,9 +196,10 @@ class Synchronizer {
184
196
  };
185
197
  }
186
198
  async dispatchOperToChannels(oper, context) {
187
- const { operatorId, targetEntity, filter, action, data } = oper;
188
- const entityIds = (0, filter_1.getRelevantIds)(filter);
189
- (0, assert_1.default)(entityIds.length > 0);
199
+ const { operatorId, targetEntity, filter, action, data, operEntity$oper } = oper;
200
+ const entityIds = operEntity$oper?.map(ele => ele.entityId);
201
+ // const entityIds = getRelevantIds(filter!);
202
+ (0, assert_1.default)(entityIds && entityIds.length > 0);
190
203
  const pushEntityNodes = this.pushAccessMap[targetEntity];
191
204
  let pushed = false;
192
205
  if (pushEntityNodes && pushEntityNodes.length > 0) {
@@ -229,7 +242,7 @@ class Synchronizer {
229
242
  this.pushOperToChannel(oper2, userId, url, endpoint, entity, entityId, selfEncryptInfo, onSynchronized);
230
243
  };
231
244
  for (const userId in userSendDict) {
232
- if (userId !== operatorId || oper.bornAt) {
245
+ if (userId !== operatorId || !oper.bornAt) {
233
246
  await pushToUserIdFn(userId);
234
247
  pushed = true;
235
248
  }
@@ -277,6 +290,15 @@ class Synchronizer {
277
290
  $$createAt$$: 1,
278
291
  $$seq$$: 1,
279
292
  filter: 1,
293
+ operEntity$oper: {
294
+ $entity: 'operEntity',
295
+ data: {
296
+ entityId: 1,
297
+ operId: 1,
298
+ entity: 1,
299
+ id: 1,
300
+ }
301
+ }
280
302
  },
281
303
  filter: {
282
304
  id: { $in: ids },
@@ -504,7 +526,7 @@ class Synchronizer {
504
526
  // todo 解密
505
527
  const opers = body;
506
528
  const ids = opers.map(ele => ele.id);
507
- const existeIds = (await context.select('oper', {
529
+ const existsIds = (await context.select('oper', {
508
530
  data: {
509
531
  id: 1,
510
532
  },
@@ -514,8 +536,8 @@ class Synchronizer {
514
536
  },
515
537
  }
516
538
  }, {})).map(ele => ele.id);
517
- const staleOpers = opers.filter(ele => existeIds.includes(ele.id));
518
- const freshOpers = opers.filter(ele => !existeIds.includes(ele.id));
539
+ const staleOpers = opers.filter((ele) => existsIds.includes(ele.id));
540
+ const freshOpers = opers.filter((ele) => !existsIds.includes(ele.id));
519
541
  if (process.env.NODE_ENV !== 'production') {
520
542
  const maxStaleSeq = Math.max(...staleOpers.map(ele => ele.$$seq$$));
521
543
  for (const oper of freshOpers) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-backend-base",
3
- "version": "4.1.0",
3
+ "version": "4.1.2",
4
4
  "description": "oak-backend-base",
5
5
  "main": "lib/index",
6
6
  "author": {
@@ -22,8 +22,8 @@
22
22
  "node-schedule": "^2.1.0",
23
23
  "oak-common-aspect": "~3.0.0",
24
24
  "oak-db": "~3.3.0",
25
- "oak-domain": "~5.0.8",
26
- "oak-frontend-base": "~5.1.0",
25
+ "oak-domain": "~5.0.16",
26
+ "oak-frontend-base": "~5.3.0",
27
27
  "socket.io": "^4.7.2",
28
28
  "socket.io-client": "^4.7.2",
29
29
  "uuid": "^8.3.2"