oak-domain 2.3.2 → 2.4.1

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.
Files changed (160) hide show
  1. package/lib/actions/action.d.ts +16 -16
  2. package/lib/actions/action.js +17 -17
  3. package/lib/actions/relation.d.ts +5 -1
  4. package/lib/actions/relation.js +38 -2
  5. package/lib/base-app-domain/ActionDefDict.d.ts +5 -5
  6. package/lib/base-app-domain/ActionDefDict.js +7 -7
  7. package/lib/base-app-domain/EntityDict.d.ts +12 -12
  8. package/lib/base-app-domain/EntityDict.js +2 -2
  9. package/lib/base-app-domain/Modi/Action.d.ts +10 -10
  10. package/lib/base-app-domain/Modi/Action.js +14 -14
  11. package/lib/base-app-domain/Modi/Schema.d.ts +146 -148
  12. package/lib/base-app-domain/Modi/Schema.js +2 -2
  13. package/lib/base-app-domain/Modi/Storage.d.ts +3 -3
  14. package/lib/base-app-domain/Modi/Storage.js +60 -60
  15. package/lib/base-app-domain/ModiEntity/Schema.d.ts +162 -164
  16. package/lib/base-app-domain/ModiEntity/Schema.js +2 -2
  17. package/lib/base-app-domain/ModiEntity/Storage.d.ts +3 -3
  18. package/lib/base-app-domain/ModiEntity/Storage.js +26 -26
  19. package/lib/base-app-domain/Oper/Schema.d.ts +144 -146
  20. package/lib/base-app-domain/Oper/Schema.js +2 -2
  21. package/lib/base-app-domain/Oper/Storage.d.ts +3 -3
  22. package/lib/base-app-domain/Oper/Storage.js +29 -29
  23. package/lib/base-app-domain/OperEntity/Schema.d.ts +178 -180
  24. package/lib/base-app-domain/OperEntity/Schema.js +2 -2
  25. package/lib/base-app-domain/OperEntity/Storage.d.ts +3 -3
  26. package/lib/base-app-domain/OperEntity/Storage.js +26 -26
  27. package/lib/base-app-domain/Storage.d.ts +3 -3
  28. package/lib/base-app-domain/Storage.js +15 -15
  29. package/lib/base-app-domain/User/Schema.d.ts +128 -130
  30. package/lib/base-app-domain/User/Schema.js +2 -2
  31. package/lib/base-app-domain/User/Storage.d.ts +3 -3
  32. package/lib/base-app-domain/User/Storage.js +25 -25
  33. package/lib/base-app-domain/_SubQuery.d.ts +36 -36
  34. package/lib/base-app-domain/_SubQuery.js +2 -2
  35. package/lib/base-app-domain/index.d.ts +3 -3
  36. package/lib/base-app-domain/index.js +6 -6
  37. package/lib/checkers/index.d.ts +5 -5
  38. package/lib/checkers/index.js +15 -13
  39. package/lib/compiler/env.d.ts +11 -11
  40. package/lib/compiler/env.js +39 -39
  41. package/lib/compiler/schemalBuilder.d.ts +2 -2
  42. package/lib/compiler/schemalBuilder.js +3301 -3245
  43. package/lib/compiler/uiBuilder.d.ts +1 -1
  44. package/lib/compiler/uiBuilder.js +3 -3
  45. package/lib/entities/Modi.d.ts +11 -11
  46. package/lib/entities/Modi.js +46 -46
  47. package/lib/entities/ModiEntity.d.ts +8 -8
  48. package/lib/entities/ModiEntity.js +15 -15
  49. package/lib/entities/Oper.d.ts +10 -10
  50. package/lib/entities/Oper.js +17 -17
  51. package/lib/entities/OperEntity.d.ts +8 -8
  52. package/lib/entities/OperEntity.js +15 -15
  53. package/lib/entities/User.d.ts +7 -7
  54. package/lib/entities/User.js +12 -12
  55. package/lib/store/AsyncRowStore.d.ts +48 -48
  56. package/lib/store/AsyncRowStore.js +183 -183
  57. package/lib/store/CascadeStore.d.ts +91 -91
  58. package/lib/store/CascadeStore.js +1594 -1594
  59. package/lib/store/SyncRowStore.d.ts +29 -29
  60. package/lib/store/SyncRowStore.js +48 -48
  61. package/lib/store/TriggerExecutor.d.ts +32 -32
  62. package/lib/store/TriggerExecutor.js +527 -508
  63. package/lib/store/actionDef.d.ts +9 -9
  64. package/lib/store/actionDef.js +135 -135
  65. package/lib/store/checker.d.ts +13 -7
  66. package/lib/store/checker.js +498 -391
  67. package/lib/store/filter.d.ts +109 -100
  68. package/lib/store/filter.js +893 -878
  69. package/lib/store/modi.d.ts +13 -13
  70. package/lib/store/modi.js +198 -206
  71. package/lib/store/relation.d.ts +13 -13
  72. package/lib/store/relation.js +66 -66
  73. package/lib/store/selection.d.ts +7 -7
  74. package/lib/store/selection.js +235 -235
  75. package/lib/triggers/index.d.ts +6 -6
  76. package/lib/triggers/index.js +11 -11
  77. package/lib/triggers/modi.d.ts +5 -5
  78. package/lib/triggers/modi.js +72 -72
  79. package/lib/types/Action.d.ts +18 -14
  80. package/lib/types/Action.js +2 -2
  81. package/lib/types/AppLoader.d.ts +11 -11
  82. package/lib/types/AppLoader.js +10 -10
  83. package/lib/types/Aspect.d.ts +12 -12
  84. package/lib/types/Aspect.js +4 -4
  85. package/lib/types/Auth.d.ts +66 -66
  86. package/lib/types/Auth.js +2 -2
  87. package/lib/types/Connector.d.ts +26 -26
  88. package/lib/types/Connector.js +9 -9
  89. package/lib/types/Context.d.ts +7 -7
  90. package/lib/types/Context.js +3 -3
  91. package/lib/types/DataType.d.ts +18 -18
  92. package/lib/types/DataType.js +5 -5
  93. package/lib/types/Demand.d.ts +77 -77
  94. package/lib/types/Demand.js +9 -9
  95. package/lib/types/Endpoint.d.ts +11 -0
  96. package/lib/types/Endpoint.js +3 -0
  97. package/lib/types/Entity.d.ts +176 -190
  98. package/lib/types/Entity.js +14 -15
  99. package/lib/types/Exception.d.ts +88 -83
  100. package/lib/types/Exception.js +267 -252
  101. package/lib/types/Expression.d.ts +163 -141
  102. package/lib/types/Expression.js +427 -402
  103. package/lib/types/Geo.d.ts +18 -18
  104. package/lib/types/Geo.js +2 -2
  105. package/lib/types/Locale.d.ts +24 -24
  106. package/lib/types/Locale.js +2 -2
  107. package/lib/types/Logger.d.ts +5 -5
  108. package/lib/types/Logger.js +3 -3
  109. package/lib/types/Polyfill.d.ts +23 -23
  110. package/lib/types/Polyfill.js +2 -2
  111. package/lib/types/Port.d.ts +17 -0
  112. package/lib/types/Port.js +2 -0
  113. package/lib/types/RowStore.d.ts +12 -12
  114. package/lib/types/RowStore.js +34 -34
  115. package/lib/types/Storage.d.ts +56 -56
  116. package/lib/types/Storage.js +2 -2
  117. package/lib/types/Timer.d.ts +13 -13
  118. package/lib/types/Timer.js +2 -2
  119. package/lib/types/Trigger.d.ts +100 -100
  120. package/lib/types/Trigger.js +39 -39
  121. package/lib/types/Txn.d.ts +2 -2
  122. package/lib/types/Txn.js +3 -3
  123. package/lib/types/Watcher.d.ts +19 -19
  124. package/lib/types/Watcher.js +4 -4
  125. package/lib/types/index.d.ts +21 -19
  126. package/lib/types/index.js +24 -22
  127. package/lib/types/schema/DataTypes.d.ts +32 -32
  128. package/lib/types/schema/DataTypes.js +3 -3
  129. package/lib/utils/SimpleConnector.d.ts +30 -30
  130. package/lib/utils/SimpleConnector.js +117 -117
  131. package/lib/utils/assert.d.ts +5 -5
  132. package/lib/utils/assert.js +9 -9
  133. package/lib/utils/concurrent.d.ts +15 -15
  134. package/lib/utils/concurrent.js +89 -89
  135. package/lib/utils/cron.d.ts +1 -1
  136. package/lib/utils/cron.js +18 -18
  137. package/lib/utils/date.d.ts +1 -0
  138. package/lib/utils/date.js +18 -0
  139. package/lib/utils/geo.d.ts +4 -4
  140. package/lib/utils/geo.js +24 -24
  141. package/lib/utils/lodash.d.ts +20 -20
  142. package/lib/utils/lodash.js +55 -55
  143. package/lib/utils/random/random.d.ts +1 -1
  144. package/lib/utils/random/random.js +24 -24
  145. package/lib/utils/random/random.mp.d.ts +1 -1
  146. package/lib/utils/random/random.mp.js +25 -25
  147. package/lib/utils/random/random.web.d.ts +1 -1
  148. package/lib/utils/random/random.web.js +17 -17
  149. package/lib/utils/string.d.ts +22 -22
  150. package/lib/utils/string.js +70 -70
  151. package/lib/utils/uuid.d.ts +12 -12
  152. package/lib/utils/uuid.js +194 -194
  153. package/lib/utils/validator.d.ts +23 -23
  154. package/lib/utils/validator.js +123 -123
  155. package/package.json +48 -48
  156. package/src/entities/Modi.ts +68 -68
  157. package/src/entities/ModiEntity.ts +24 -24
  158. package/src/entities/Oper.ts +28 -28
  159. package/src/entities/OperEntity.ts +24 -24
  160. package/src/entities/User.ts +19 -19
@@ -1,1594 +1,1594 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CascadeStore = void 0;
4
- var tslib_1 = require("tslib");
5
- var assert_1 = tslib_1.__importDefault(require("assert"));
6
- var Entity_1 = require("../types/Entity");
7
- var RowStore_1 = require("../types/RowStore");
8
- var filter_1 = require("./filter");
9
- var relation_1 = require("./relation");
10
- var types_1 = require("../types");
11
- var lodash_1 = require("../utils/lodash");
12
- var filter_2 = require("./filter");
13
- var uuid_1 = require("../utils/uuid");
14
- /**这个用来处理级联的select和update,对不同能力的 */
15
- var CascadeStore = /** @class */ (function (_super) {
16
- tslib_1.__extends(CascadeStore, _super);
17
- function CascadeStore(storageSchema) {
18
- return _super.call(this, storageSchema) || this;
19
- }
20
- CascadeStore.prototype.destructCascadeSelect = function (entity, projection2, context, cascadeSelectFn, aggregateFn, option) {
21
- var _this = this;
22
- var projection = {};
23
- var cascadeSelectionFns = [];
24
- var supportMtoJoin = this.supportManyToOneJoin();
25
- var toModi = this.getSchema()[entity].toModi;
26
- var _loop_1 = function (attr) {
27
- var _a, _b, _c, _d;
28
- var relation = (0, relation_1.judgeRelation)(this_1.storageSchema, entity, attr);
29
- if (relation === 1 || relation == 0) {
30
- Object.assign(projection, (_a = {},
31
- _a[attr] = projection2[attr],
32
- _a));
33
- }
34
- else if (relation === 2) {
35
- // 基于entity/entityId的多对一
36
- Object.assign(projection, {
37
- entity: 1,
38
- entityId: 1,
39
- });
40
- if (supportMtoJoin) {
41
- cascadeSelectionFns.push(function (result) {
42
- if (!toModi) {
43
- result.forEach(function (ele) {
44
- if (ele.entity === attr) {
45
- (0, assert_1.default)(ele.entityId);
46
- if (!ele[attr]) {
47
- throw new types_1.OakRowUnexistedException([{
48
- entity: attr,
49
- selection: {
50
- data: projection2[attr],
51
- filter: {
52
- id: ele.entityId,
53
- }
54
- }
55
- }]);
56
- }
57
- }
58
- });
59
- }
60
- });
61
- var _e = this_1.destructCascadeSelect(attr, projection2[attr], context, cascadeSelectFn, aggregateFn, option), subProjection = _e.projection, subCascadeSelectionFns = _e.cascadeSelectionFns;
62
- Object.assign(projection, (_b = {},
63
- _b[attr] = subProjection,
64
- _b));
65
- subCascadeSelectionFns.forEach(function (ele) { return cascadeSelectionFns.push(function (result) {
66
- return ele(result.map(function (ele2) { return ele2[attr]; }).filter(function (ele2) { return !!ele2; }));
67
- }); });
68
- }
69
- else {
70
- cascadeSelectionFns.push(function (result) {
71
- var dealWithSubRows = function (subRows) {
72
- (0, assert_1.default)(subRows.length <= entityIds.length);
73
- if (subRows.length < entityIds.length && !toModi) {
74
- throw new types_1.OakRowUnexistedException([{
75
- entity: attr,
76
- selection: {
77
- data: projection2[attr],
78
- filter: {
79
- id: {
80
- $in: entityIds
81
- },
82
- },
83
- },
84
- }]);
85
- }
86
- result.forEach(function (ele) {
87
- var _a, _b;
88
- if (ele.entity === attr) {
89
- var subRow = subRows.find(function (ele2) { return ele2.id === ele.entityId; });
90
- if (subRow) {
91
- Object.assign(ele, (_a = {},
92
- _a[attr] = subRow,
93
- _a));
94
- }
95
- else {
96
- Object.assign(ele, (_b = {},
97
- _b[attr] = null,
98
- _b));
99
- }
100
- }
101
- });
102
- };
103
- var entityIds = (0, lodash_1.uniq)(result.filter(function (ele) { return ele.entity === attr; }).map(function (ele) { return ele.entityId; }));
104
- var subRows = cascadeSelectFn.call(_this, attr, {
105
- data: projection2[attr],
106
- filter: {
107
- id: {
108
- $in: entityIds
109
- },
110
- },
111
- }, context, option);
112
- if (subRows instanceof Promise) {
113
- return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
114
- }
115
- else {
116
- dealWithSubRows(subRows);
117
- }
118
- });
119
- }
120
- }
121
- else if (typeof relation === 'string') {
122
- Object.assign(projection, (_c = {},
123
- _c["".concat(attr, "Id")] = 1,
124
- _c));
125
- if (supportMtoJoin) {
126
- if (!toModi) {
127
- // 如果不是modi,要保证外键没有空指针
128
- cascadeSelectionFns.push(function (result) {
129
- if (!toModi) {
130
- result.forEach(function (ele) {
131
- if (ele["".concat(attr, "Id")] && !ele[attr]) {
132
- throw new types_1.OakRowUnexistedException([{
133
- entity: relation,
134
- selection: {
135
- data: projection2[attr],
136
- filter: {
137
- id: ele["".concat(attr, "Id")],
138
- }
139
- }
140
- }]);
141
- }
142
- });
143
- }
144
- });
145
- }
146
- var _f = this_1.destructCascadeSelect(relation, projection2[attr], context, cascadeSelectFn, aggregateFn, option), subProjection = _f.projection, subCascadeSelectionFns = _f.cascadeSelectionFns;
147
- Object.assign(projection, (_d = {},
148
- _d[attr] = subProjection,
149
- _d));
150
- subCascadeSelectionFns.forEach(function (ele) { return cascadeSelectionFns.push(function (result) {
151
- return ele(result.map(function (ele2) { return ele2[attr]; }).filter(function (ele2) { return !!ele2; }));
152
- }); });
153
- }
154
- else {
155
- cascadeSelectionFns.push(function (result) {
156
- var dealWithSubRows = function (subRows) {
157
- (0, assert_1.default)(subRows.length <= ids.length);
158
- if (subRows.length < ids.length && !toModi) {
159
- throw new types_1.OakRowUnexistedException([{
160
- entity: relation,
161
- selection: {
162
- data: projection2[attr],
163
- filter: {
164
- id: {
165
- $in: ids
166
- },
167
- },
168
- }
169
- }]);
170
- }
171
- result.forEach(function (ele) {
172
- var _a, _b, _c;
173
- if (ele["".concat(attr, "Id")]) {
174
- var subRow = subRows.find(function (ele2) { return ele2.id === ele["".concat(attr, "Id")]; });
175
- if (subRow) {
176
- Object.assign(ele, (_a = {},
177
- _a[attr] = subRow,
178
- _a));
179
- }
180
- else {
181
- Object.assign(ele, (_b = {},
182
- _b[attr] = null,
183
- _b));
184
- }
185
- }
186
- else {
187
- Object.assign(ele, (_c = {},
188
- _c[attr] = null,
189
- _c));
190
- }
191
- });
192
- };
193
- var ids = (0, lodash_1.uniq)(result.filter(function (ele) { return !!(ele["".concat(attr, "Id")]); }).map(function (ele) { return ele["".concat(attr, "Id")]; }));
194
- var subRows = cascadeSelectFn.call(_this, relation, {
195
- data: projection2[attr],
196
- filter: {
197
- id: {
198
- $in: ids
199
- },
200
- },
201
- }, context, option);
202
- if (subRows instanceof Promise) {
203
- return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
204
- }
205
- dealWithSubRows(subRows);
206
- });
207
- }
208
- }
209
- else {
210
- (0, assert_1.default)(relation instanceof Array);
211
- var _g = projection2[attr], subProjection_1 = _g.data, subFilter_1 = _g.filter, indexFrom_1 = _g.indexFrom, count_1 = _g.count, subSorter_1 = _g.sorter;
212
- var _h = tslib_1.__read(relation, 2), entity2_1 = _h[0], foreignKey_1 = _h[1];
213
- var isAggr = attr.endsWith('$$aggr');
214
- if (foreignKey_1) {
215
- // 基于属性的一对多
216
- if (isAggr) {
217
- // 是聚合运算
218
- cascadeSelectionFns.push(function (result) {
219
- var aggrResults = result.map(function (row) {
220
- var _a, _b;
221
- var aggrResult = aggregateFn.call(_this, entity2_1, {
222
- data: subProjection_1,
223
- filter: (0, filter_1.combineFilters)([(_a = {},
224
- _a[foreignKey_1] = row.id,
225
- _a), subFilter_1]),
226
- sorter: subSorter_1,
227
- indexFrom: indexFrom_1,
228
- count: count_1
229
- }, context, option);
230
- if (aggrResult instanceof Promise) {
231
- return aggrResult.then(function (aggrResultResult) {
232
- var _a;
233
- return Object.assign(row, (_a = {},
234
- _a[attr] = aggrResultResult,
235
- _a));
236
- });
237
- }
238
- else {
239
- Object.assign(row, (_b = {},
240
- _b[attr] = aggrResult,
241
- _b));
242
- }
243
- });
244
- if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
245
- return Promise.all(aggrResults).then(function () { return undefined; });
246
- }
247
- });
248
- }
249
- else {
250
- // 是一对多查询
251
- cascadeSelectionFns.push(function (result) {
252
- var _a;
253
- var ids = result.map(function (ele) { return ele.id; });
254
- var dealWithSubRows = function (subRows) {
255
- result.forEach(function (ele) {
256
- var _a;
257
- var subRowss = subRows.filter(function (ele2) { return ele2[foreignKey_1] === ele.id; });
258
- (0, assert_1.default)(subRowss);
259
- Object.assign(ele, (_a = {},
260
- _a[attr] = subRowss,
261
- _a));
262
- });
263
- };
264
- var subRows = cascadeSelectFn.call(_this, entity2_1, {
265
- data: subProjection_1,
266
- filter: (0, filter_1.combineFilters)([(_a = {},
267
- _a[foreignKey_1] = {
268
- $in: ids,
269
- },
270
- _a), subFilter_1]),
271
- sorter: subSorter_1,
272
- indexFrom: indexFrom_1,
273
- count: count_1
274
- }, context, option);
275
- if (subRows instanceof Promise) {
276
- return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
277
- }
278
- dealWithSubRows(subRows);
279
- });
280
- }
281
- }
282
- else {
283
- // 基于entity的多对一
284
- if (isAggr) {
285
- // 是聚合运算
286
- cascadeSelectionFns.push(function (result) {
287
- var aggrResults = result.map(function (row) {
288
- var _a;
289
- var aggrResult = aggregateFn.call(_this, entity2_1, {
290
- data: subProjection_1,
291
- filter: (0, filter_1.combineFilters)([{
292
- entity: entity,
293
- entityId: row.id,
294
- }, subFilter_1]),
295
- sorter: subSorter_1,
296
- indexFrom: indexFrom_1,
297
- count: count_1
298
- }, context, option);
299
- if (aggrResult instanceof Promise) {
300
- return aggrResult.then(function (aggrResultResult) {
301
- var _a;
302
- return Object.assign(row, (_a = {},
303
- _a[attr] = aggrResultResult,
304
- _a));
305
- });
306
- }
307
- else {
308
- Object.assign(row, (_a = {},
309
- _a[attr] = aggrResult,
310
- _a));
311
- }
312
- });
313
- if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
314
- return Promise.all(aggrResults).then(function () { return undefined; });
315
- }
316
- });
317
- }
318
- else {
319
- // 是一对多查询
320
- cascadeSelectionFns.push(function (result) {
321
- var ids = result.map(function (ele) { return ele.id; });
322
- var dealWithSubRows = function (subRows) {
323
- result.forEach(function (ele) {
324
- var _a;
325
- var subRowss = subRows.filter(function (ele2) { return ele2.entityId === ele.id; });
326
- (0, assert_1.default)(subRowss);
327
- Object.assign(ele, (_a = {},
328
- _a[attr] = subRowss,
329
- _a));
330
- });
331
- };
332
- var subRows = cascadeSelectFn.call(_this, entity2_1, {
333
- data: subProjection_1,
334
- filter: (0, filter_1.combineFilters)([{
335
- entity: entity,
336
- entityId: {
337
- $in: ids,
338
- }
339
- }, subFilter_1]),
340
- sorter: subSorter_1,
341
- indexFrom: indexFrom_1,
342
- count: count_1
343
- }, context, option);
344
- if (subRows instanceof Promise) {
345
- return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
346
- }
347
- dealWithSubRows(subRows);
348
- });
349
- }
350
- }
351
- }
352
- };
353
- var this_1 = this;
354
- for (var attr in projection2) {
355
- _loop_1(attr);
356
- }
357
- return {
358
- projection: projection,
359
- cascadeSelectionFns: cascadeSelectionFns,
360
- };
361
- };
362
- /**
363
- * 级联更新
364
- * A --> B
365
- 多对一:A CREATE/B CREATE,B data的主键赋到A的data上
366
- A CREATE/B UPDATE,B filter的主键来自A的data
367
- A UPDATE/B CREATE,B data的主键赋到A的data上
368
- A UPDATE/B UPDATE,B filter的主键来自A的row
369
- A UPDATE/B REMOVE,B filter的主键来自A的row
370
- A REMOVE/B UPDATE,B filter的主键来自A的row
371
- A REMOVE/B REMOVE,B filter的主键来自A的row
372
-
373
- 一对多:A CREATE/B CREATE,A data上的主键赋到B的data上
374
- A CREATE/B UPDATE,A data上的主键赋到B的data上
375
- A UPDATE/B CREATE,A filter上的主键赋到B的data上(一定是带主键的filter)
376
- A UPDATE/B UPDATE,A filter上的主键赋到B的filter上(一定是带主键的filter)
377
- A UPDATE/B REMOVE,A filter上的主键赋到B的filter上(一定是带主键的filter)
378
- A REMOVE/B UPDATE,A filter上的主键赋到B的filter上(且B关于A的外键清空)
379
- A REMOVE/B REMOVE,A filter上的主键赋到B的filter上
380
- *
381
- * 延时更新,
382
- * A(业务级别的申请对象) ---> B(业务级别需要更新的对象)
383
- * 两者必须通过entity/entityId关联
384
- * 此时需要把对B的更新记录成一条新插入的Modi对象,并将A上的entity/entityId指向该对象(新生成的Modi对象的id与此operation的id保持一致)
385
- * @param entity
386
- * @param action
387
- * @param data
388
- * @param context
389
- * @param option
390
- * @param result
391
- * @param filter
392
- * @returns
393
- */
394
- CascadeStore.prototype.destructCascadeUpdate = function (entity, action, data, context, option, cascadeUpdate, filter) {
395
- var _this = this;
396
- var modiAttr = this.getSchema()[entity].toModi;
397
- var option2 = Object.assign({}, option);
398
- var opData = {};
399
- var beforeFns = [];
400
- var afterFns = [];
401
- if (modiAttr && action !== 'remove' && !option.dontCreateModi) {
402
- // create/update具有modi对象的对象,对其子对象的update行为全部是create modi对象(缓存动作)
403
- // delete此对象,所有的modi子对象应该通过触发器作废,这个目前先通过系统的trigger来实现
404
- (0, assert_1.default)(!option2.modiParentId && !option2.modiParentEntity);
405
- if (action === 'create') {
406
- option2.modiParentId = data.id;
407
- }
408
- else {
409
- (0, assert_1.default)((filter === null || filter === void 0 ? void 0 : filter.id) && typeof filter.id === 'string');
410
- option2.modiParentId = filter.id;
411
- }
412
- option2.modiParentEntity = entity;
413
- }
414
- var _loop_2 = function (attr) {
415
- var _a, _b, _c, e_1, _d;
416
- var relation = (0, relation_1.judgeRelation)(this_2.storageSchema, entity, attr);
417
- if (relation === 1) {
418
- Object.assign(opData, (_a = {},
419
- _a[attr] = data[attr],
420
- _a));
421
- }
422
- else if (relation === 2) {
423
- // 基于entity/entityId的many-to-one
424
- var operationMto_1 = data[attr];
425
- var actionMto = operationMto_1.action, dataMto = operationMto_1.data, filterMto = operationMto_1.filter;
426
- if (actionMto === 'create') {
427
- Object.assign(opData, {
428
- entityId: dataMto.id,
429
- entity: attr,
430
- });
431
- }
432
- else if (action === 'create') {
433
- var fkId = data.entityId, entity_1 = data.entity;
434
- (0, assert_1.default)(typeof fkId === 'string' || entity_1 === attr);
435
- if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
436
- // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
437
- (0, assert_1.default)(filterMto.id === fkId);
438
- }
439
- else {
440
- // A中data的entityId作为B中filter的主键
441
- Object.assign(operationMto_1, {
442
- filter: (0, filter_1.addFilterSegment)({
443
- id: fkId,
444
- }),
445
- filterMto: filterMto,
446
- });
447
- }
448
- }
449
- else {
450
- // 剩下三种情况都是B中的filter的id来自A中row的entityId
451
- (0, assert_1.default)(!data.hasOwnProperty('entityId') && !data.hasOwnProperty('entity'));
452
- if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
453
- // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
454
- (0, assert_1.default)(typeof filterMto.id === 'string');
455
- }
456
- else {
457
- // A中data的entityId作为B中filter的主键
458
- Object.assign(operationMto_1, {
459
- filter: (0, filter_1.addFilterSegment)({
460
- id: {
461
- $in: {
462
- entity: entity,
463
- data: {
464
- entityId: 1,
465
- },
466
- filter: (0, filter_1.addFilterSegment)({
467
- entity: attr,
468
- }, filter),
469
- }
470
- },
471
- }, filterMto),
472
- });
473
- }
474
- }
475
- beforeFns.push(function () { return cascadeUpdate.call(_this, attr, operationMto_1, context, option2); });
476
- }
477
- else if (typeof relation === 'string') {
478
- // 基于attr的外键的many-to-one
479
- var operationMto_2 = data[attr];
480
- var actionMto = operationMto_2.action, dataMto = operationMto_2.data, filterMto = operationMto_2.filter;
481
- if (actionMto === 'create') {
482
- Object.assign(opData, (_b = {},
483
- _b["".concat(attr, "Id")] = dataMto.id,
484
- _b));
485
- }
486
- else if (action === 'create') {
487
- var _e = data, _f = "".concat(attr, "Id"), fkId = _e[_f];
488
- (0, assert_1.default)(typeof fkId === 'string');
489
- if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
490
- // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
491
- (0, assert_1.default)(filterMto.id === fkId);
492
- }
493
- else {
494
- // A中data的entityId作为B中filter的主键
495
- Object.assign(operationMto_2, {
496
- filter: (0, filter_1.addFilterSegment)(filterMto || {}, {
497
- id: fkId,
498
- }),
499
- });
500
- }
501
- }
502
- else {
503
- (0, assert_1.default)(!data.hasOwnProperty("".concat(attr, "Id")));
504
- if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
505
- // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
506
- (0, assert_1.default)(typeof filterMto.id === 'string');
507
- }
508
- else {
509
- // A中data的entityId作为B中filter的主键
510
- Object.assign(operationMto_2, {
511
- filter: (0, filter_1.addFilterSegment)(filterMto || {}, {
512
- id: {
513
- $in: {
514
- entity: entity,
515
- data: (_c = {},
516
- _c["".concat(attr, "Id")] = 1,
517
- _c),
518
- filter: filter,
519
- }
520
- },
521
- }),
522
- });
523
- }
524
- }
525
- beforeFns.push(function () { return cascadeUpdate.call(_this, relation, operationMto_2, context, option2); });
526
- }
527
- else {
528
- (0, assert_1.default)(relation instanceof Array);
529
- var _g = tslib_1.__read(relation, 2), entityOtm_1 = _g[0], foreignKey_2 = _g[1];
530
- var otmOperations = data[attr];
531
- var dealWithOneToMany = function (otm) {
532
- var _a, _b, _c, _d;
533
- var actionOtm = otm.action, dataOtm = otm.data, filterOtm = otm.filter;
534
- if (!foreignKey_2) {
535
- // 基于entity/entityId的one-to-many
536
- if (action === 'create') {
537
- var id_1 = data.id;
538
- if (dataOtm instanceof Array) {
539
- dataOtm.forEach(function (ele) { return Object.assign(ele, {
540
- entity: entity,
541
- entityId: id_1,
542
- }); });
543
- }
544
- else {
545
- Object.assign(dataOtm, {
546
- entity: entity,
547
- entityId: id_1,
548
- });
549
- }
550
- }
551
- else if (actionOtm === 'create') {
552
- // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
553
- // todo 这个假设成立吗?等遇到create/create一对多的case再完善
554
- var id_2 = filter.id;
555
- (0, assert_1.default)(typeof id_2 === 'string');
556
- if (dataOtm instanceof Array) {
557
- dataOtm.forEach(function (ele) { return Object.assign(ele, {
558
- entity: entity,
559
- entityId: id_2,
560
- }); });
561
- }
562
- else {
563
- Object.assign(dataOtm, {
564
- entity: entity,
565
- entityId: id_2,
566
- });
567
- }
568
- }
569
- else {
570
- // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
571
- // 这个倒是好像不可能出现create/update的一对多,如果遇到了再完善
572
- var id = filter.id;
573
- Object.assign(otm, {
574
- filter: (0, filter_1.addFilterSegment)({
575
- entity: entity,
576
- entityId: id,
577
- }, filterOtm),
578
- });
579
- if (action === 'remove' && actionOtm === 'update') {
580
- Object.assign(dataOtm, {
581
- entity: null,
582
- entityId: null,
583
- });
584
- }
585
- }
586
- }
587
- else {
588
- // 基于foreignKey的one-to-many
589
- if (action === 'create') {
590
- var id_3 = data.id;
591
- if (dataOtm instanceof Array) {
592
- dataOtm.forEach(function (ele) {
593
- var _a;
594
- return Object.assign(ele, (_a = {},
595
- _a[foreignKey_2] = id_3,
596
- _a));
597
- });
598
- }
599
- else {
600
- Object.assign(dataOtm, (_a = {},
601
- _a[foreignKey_2] = id_3,
602
- _a));
603
- }
604
- }
605
- else if (actionOtm === 'create') {
606
- // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
607
- // todo 这个假设成立吗?等遇到create/create一对多的case再完善
608
- var id_4 = filter.id;
609
- (0, assert_1.default)(typeof id_4 === 'string');
610
- if (dataOtm instanceof Array) {
611
- dataOtm.forEach(function (ele) {
612
- var _a;
613
- return Object.assign(ele, (_a = {},
614
- _a[foreignKey_2] = id_4,
615
- _a));
616
- });
617
- }
618
- else {
619
- Object.assign(dataOtm, (_b = {},
620
- _b[foreignKey_2] = id_4,
621
- _b));
622
- }
623
- }
624
- else {
625
- // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
626
- // 这个倒是好像不可能出现create/update的一对多,如果遇到了再完善
627
- var id = filter.id;
628
- Object.assign(otm, {
629
- filter: (0, filter_1.addFilterSegment)((_c = {},
630
- _c[foreignKey_2] = id,
631
- _c), filterOtm),
632
- });
633
- if (action === 'remove' && actionOtm === 'update') {
634
- Object.assign(dataOtm, (_d = {},
635
- _d[foreignKey_2] = null,
636
- _d));
637
- }
638
- }
639
- }
640
- afterFns.push(function () { return cascadeUpdate.call(_this, entityOtm_1, otm, context, option2); });
641
- };
642
- if (otmOperations instanceof Array) {
643
- try {
644
- for (var otmOperations_1 = (e_1 = void 0, tslib_1.__values(otmOperations)), otmOperations_1_1 = otmOperations_1.next(); !otmOperations_1_1.done; otmOperations_1_1 = otmOperations_1.next()) {
645
- var oper = otmOperations_1_1.value;
646
- dealWithOneToMany(oper);
647
- }
648
- }
649
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
650
- finally {
651
- try {
652
- if (otmOperations_1_1 && !otmOperations_1_1.done && (_d = otmOperations_1.return)) _d.call(otmOperations_1);
653
- }
654
- finally { if (e_1) throw e_1.error; }
655
- }
656
- }
657
- else {
658
- dealWithOneToMany(otmOperations);
659
- }
660
- }
661
- };
662
- var this_2 = this;
663
- for (var attr in data) {
664
- _loop_2(attr);
665
- }
666
- return {
667
- data: opData,
668
- beforeFns: beforeFns,
669
- afterFns: afterFns,
670
- };
671
- };
672
- // 对插入的数据,没有初始值的属性置null
673
- CascadeStore.prototype.preProcessDataCreated = function (entity, data) {
674
- var now = Date.now();
675
- var attributes = this.getSchema()[entity].attributes;
676
- var processSingle = function (data2) {
677
- var _a, _b;
678
- for (var key in attributes) {
679
- if (data2[key] === undefined) {
680
- Object.assign(data2, (_a = {},
681
- _a[key] = null,
682
- _a));
683
- }
684
- }
685
- Object.assign(data2, (_b = {},
686
- _b[Entity_1.CreateAtAttribute] = now,
687
- _b[Entity_1.UpdateAtAttribute] = now,
688
- _b[Entity_1.DeleteAtAttribute] = null,
689
- _b));
690
- };
691
- if (data instanceof Array) {
692
- data.forEach(function (ele) { return processSingle(ele); });
693
- }
694
- else {
695
- processSingle(data);
696
- }
697
- };
698
- // 对更新的数据,去掉所有的undefined属性
699
- CascadeStore.prototype.preProcessDataUpdated = function (data) {
700
- var undefinedKeys = Object.keys(data).filter(function (ele) { return data[ele] === undefined; });
701
- undefinedKeys.forEach(function (ele) { return (0, lodash_1.unset)(data, ele); });
702
- };
703
- CascadeStore.prototype.judgeRelation = function (entity, attr) {
704
- return (0, relation_1.judgeRelation)(this.storageSchema, entity, attr);
705
- };
706
- /**
707
- * 和具体的update过程无关的例程放在这里,包括对later动作的处理、对oper的记录以及对record的收集等
708
- * @param entity
709
- * @param operation
710
- * @param context
711
- * @param option
712
- */
713
- CascadeStore.prototype.doUpdateSingleRowAsync = function (entity, operation, context, option) {
714
- return tslib_1.__awaiter(this, void 0, void 0, function () {
715
- var data, action, operId, filter, now, _a, modiCreate, result_1, createInner, multipleCreate, data_1, data_1_1, d, createSingleOper, e_2_1, operatorId, createOper, _b, ids_1, selection, rows, modiUpsert, upsertModis, _c, originData, originId, createOper, updateAttrCount, result;
716
- var e_2, _d, _e, _f, _g, _h, _j, _k, _l, _m;
717
- var _this = this;
718
- return tslib_1.__generator(this, function (_o) {
719
- switch (_o.label) {
720
- case 0:
721
- data = operation.data, action = operation.action, operId = operation.id, filter = operation.filter;
722
- now = Date.now();
723
- _a = action;
724
- switch (_a) {
725
- case 'create': return [3 /*break*/, 1];
726
- }
727
- return [3 /*break*/, 23];
728
- case 1:
729
- this.preProcessDataCreated(entity, data);
730
- if (!(option.modiParentEntity && !['modi', 'modiEntity', 'oper', 'operEntity'].includes(entity))) return [3 /*break*/, 3];
731
- // 变成对modi的插入
732
- (0, assert_1.default)(option.modiParentId);
733
- modiCreate = {
734
- id: 'dummy',
735
- action: 'create',
736
- data: {
737
- id: operId,
738
- targetEntity: entity,
739
- action: action,
740
- entity: option.modiParentEntity,
741
- entityId: option.modiParentId,
742
- filter: {
743
- id: {
744
- $in: [data.id], //这里记录这个filter是为了后面update的时候直接在其上面update,参见本函数后半段关于modiUpsert相关的优化
745
- },
746
- },
747
- data: data,
748
- iState: 'active',
749
- },
750
- };
751
- return [4 /*yield*/, this.cascadeUpdateAsync('modi', modiCreate, context, option)];
752
- case 2:
753
- _o.sent();
754
- return [2 /*return*/, 1];
755
- case 3:
756
- result_1 = 0;
757
- createInner = function (operation2) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
758
- var _a, e_3;
759
- return tslib_1.__generator(this, function (_b) {
760
- switch (_b.label) {
761
- case 0:
762
- _b.trys.push([0, 2, , 3]);
763
- _a = result_1;
764
- return [4 /*yield*/, this.updateAbjointRowAsync(entity, operation2, context, option)];
765
- case 1:
766
- result_1 = _a + _b.sent();
767
- return [3 /*break*/, 3];
768
- case 2:
769
- e_3 = _b.sent();
770
- /* 这段代码是处理插入时有重复的行,现在看有问题,等实际需求出现再写
771
- if (e instanceof OakCongruentRowExists) {
772
- if (option.allowExists) {
773
- // 如果允许存在,对已存在行进行update,剩下的行继续insert
774
- const congruentRow = e.getData() as ED[T]['OpSchema'];
775
- if (data instanceof Array) {
776
- const rest = data.filter(
777
- ele => ele.id !== congruentRow.id
778
- );
779
- if (rest.length === data.length) {
780
- throw e;
781
- }
782
- const result2 = await this.updateAbjointRow(
783
- entity,
784
- Object.assign({}, operation, {
785
- data: rest,
786
- }),
787
- context,
788
- option
789
- );
790
-
791
- const row = data.find(
792
- ele => ele.id === congruentRow.id
793
- );
794
- const updateData = omit(row, ['id', '$$createAt$$']);
795
- const result3 = await this.updateAbjointRow(
796
- entity,
797
- {
798
- id: await generateNewId(),
799
- action: 'update',
800
- data: updateData,
801
- filter: {
802
- id: congruentRow.id,
803
- } as any,
804
- },
805
- context,
806
- option
807
- );
808
-
809
- return result2 + result3;
810
- }
811
- else {
812
- if (data.id !== congruentRow.id) {
813
- throw e;
814
- }
815
- const updateData = omit(data, ['id', '$$createAt$$']);
816
- const result2 = await this.updateAbjointRow(
817
- entity,
818
- {
819
- id: await generateNewId(),
820
- action: 'update',
821
- data: updateData,
822
- filter: {
823
- id: congruentRow.id,
824
- } as any,
825
- },
826
- context,
827
- option
828
- );
829
- return result2;
830
- }
831
- }
832
- } */
833
- throw e_3;
834
- case 3: return [2 /*return*/];
835
- }
836
- });
837
- }); };
838
- if (!(data instanceof Array)) return [3 /*break*/, 13];
839
- multipleCreate = this.supportMultipleCreate();
840
- if (!multipleCreate) return [3 /*break*/, 5];
841
- return [4 /*yield*/, createInner(operation)];
842
- case 4:
843
- _o.sent();
844
- return [3 /*break*/, 12];
845
- case 5:
846
- _o.trys.push([5, 10, 11, 12]);
847
- data_1 = tslib_1.__values(data), data_1_1 = data_1.next();
848
- _o.label = 6;
849
- case 6:
850
- if (!!data_1_1.done) return [3 /*break*/, 9];
851
- d = data_1_1.value;
852
- createSingleOper = {
853
- id: 'any',
854
- action: 'create',
855
- data: d,
856
- };
857
- return [4 /*yield*/, createInner(createSingleOper)];
858
- case 7:
859
- _o.sent();
860
- _o.label = 8;
861
- case 8:
862
- data_1_1 = data_1.next();
863
- return [3 /*break*/, 6];
864
- case 9: return [3 /*break*/, 12];
865
- case 10:
866
- e_2_1 = _o.sent();
867
- e_2 = { error: e_2_1 };
868
- return [3 /*break*/, 12];
869
- case 11:
870
- try {
871
- if (data_1_1 && !data_1_1.done && (_d = data_1.return)) _d.call(data_1);
872
- }
873
- finally { if (e_2) throw e_2.error; }
874
- return [7 /*endfinally*/];
875
- case 12: return [3 /*break*/, 15];
876
- case 13: return [4 /*yield*/, createInner(operation)];
877
- case 14:
878
- _o.sent();
879
- _o.label = 15;
880
- case 15:
881
- if (!option.dontCollect) {
882
- context.opRecords.push({
883
- a: 'c',
884
- e: entity,
885
- d: data,
886
- });
887
- }
888
- if (!(!option.dontCreateOper && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity))) return [3 /*break*/, 22];
889
- // 按照框架要求生成Oper和OperEntity这两个内置的对象
890
- (0, assert_1.default)(operId);
891
- return [4 /*yield*/, context.getCurrentUserId(true)];
892
- case 16:
893
- operatorId = _o.sent();
894
- if (!operatorId) return [3 /*break*/, 22];
895
- _e = {
896
- id: 'dummy',
897
- action: 'create'
898
- };
899
- _f = {
900
- id: operId,
901
- action: action,
902
- data: data,
903
- operatorId: operatorId
904
- };
905
- if (!(data instanceof Array)) return [3 /*break*/, 18];
906
- _g = {
907
- id: 'dummy',
908
- action: 'create'
909
- };
910
- return [4 /*yield*/, Promise.all(data.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
911
- var _a;
912
- return tslib_1.__generator(this, function (_b) {
913
- switch (_b.label) {
914
- case 0:
915
- _a = {};
916
- return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
917
- case 1: return [2 /*return*/, (_a.id = _b.sent(),
918
- _a.entity = entity,
919
- _a.entityId = ele.id,
920
- _a)];
921
- }
922
- });
923
- }); }))];
924
- case 17:
925
- _b = (_g.data = _o.sent(),
926
- _g);
927
- return [3 /*break*/, 20];
928
- case 18:
929
- _h = {
930
- id: 'dummy',
931
- action: 'create'
932
- };
933
- _j = {};
934
- return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
935
- case 19:
936
- _b = [(_h.data = (_j.id = _o.sent(),
937
- _j.entity = entity,
938
- _j.entityId = data.id,
939
- _j),
940
- _h)];
941
- _o.label = 20;
942
- case 20:
943
- createOper = (_e.data = (_f.operEntity$oper = _b,
944
- _f),
945
- _e);
946
- return [4 /*yield*/, this.cascadeUpdateAsync('oper', createOper, context, {
947
- dontCollect: true,
948
- dontCreateOper: true,
949
- })];
950
- case 21:
951
- _o.sent();
952
- _o.label = 22;
953
- case 22: return [2 /*return*/, result_1];
954
- case 23:
955
- ids_1 = (0, filter_2.getRelevantIds)(filter);
956
- if (!(ids_1.length === 0)) return [3 /*break*/, 25];
957
- selection = {
958
- data: {
959
- id: 1,
960
- },
961
- filter: operation.filter,
962
- indexFrom: operation.indexFrom,
963
- count: operation.count,
964
- };
965
- return [4 /*yield*/, this.selectAbjointRowAsync(entity, selection, context, {
966
- dontCollect: true,
967
- })];
968
- case 24:
969
- rows = _o.sent();
970
- ids_1.push.apply(ids_1, tslib_1.__spreadArray([], tslib_1.__read((rows.map(function (ele) { return ele.id; }))), false));
971
- _o.label = 25;
972
- case 25:
973
- if (data) {
974
- this.preProcessDataUpdated(data);
975
- }
976
- if (!(option.modiParentEntity && !['modi', 'modiEntity'].includes(entity))) return [3 /*break*/, 31];
977
- modiUpsert = void 0;
978
- if (!(action !== 'remove')) return [3 /*break*/, 27];
979
- return [4 /*yield*/, this.selectAbjointRowAsync('modi', {
980
- data: {
981
- id: 1,
982
- data: 1,
983
- },
984
- filter: {
985
- targetEntity: entity,
986
- action: {
987
- $in: ['create', 'update'],
988
- },
989
- entity: option.modiParentEntity,
990
- entityId: option.modiParentId,
991
- iState: 'active',
992
- filter: ids_1.length > 0 ? {
993
- id: {
994
- $in: ids_1,
995
- },
996
- } : filter,
997
- },
998
- sorter: [
999
- {
1000
- $attr: {
1001
- $$createAt$$: 1,
1002
- },
1003
- $direction: 'desc',
1004
- }
1005
- ],
1006
- indexFrom: 0,
1007
- count: 1,
1008
- }, context, option)];
1009
- case 26:
1010
- upsertModis = _o.sent();
1011
- if (upsertModis.length > 0) {
1012
- _c = upsertModis[0], originData = _c.data, originId = _c.id;
1013
- modiUpsert = {
1014
- id: 'dummy',
1015
- action: 'update',
1016
- data: {
1017
- data: Object.assign({}, originData, data),
1018
- },
1019
- filter: {
1020
- id: originId,
1021
- }
1022
- };
1023
- }
1024
- _o.label = 27;
1025
- case 27:
1026
- if (!!modiUpsert) return [3 /*break*/, 29];
1027
- _k = {
1028
- id: 'dummy',
1029
- action: 'create'
1030
- };
1031
- _l = {
1032
- id: operId,
1033
- targetEntity: entity,
1034
- entity: option.modiParentEntity,
1035
- entityId: option.modiParentId,
1036
- action: action,
1037
- data: data,
1038
- iState: 'active',
1039
- filter: filter
1040
- };
1041
- _m = {
1042
- id: 'dummy',
1043
- action: 'create'
1044
- };
1045
- return [4 /*yield*/, Promise.all(ids_1.map(function (id) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1046
- var _a;
1047
- return tslib_1.__generator(this, function (_b) {
1048
- switch (_b.label) {
1049
- case 0:
1050
- _a = {};
1051
- return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1052
- case 1: return [2 /*return*/, (_a.id = _b.sent(),
1053
- _a.entity = entity,
1054
- _a.entityId = id,
1055
- _a)];
1056
- }
1057
- });
1058
- }); }))];
1059
- case 28:
1060
- modiUpsert = (_k.data = (_l.modiEntity$modi = (_m.data = _o.sent(),
1061
- _m),
1062
- _l),
1063
- _k);
1064
- _o.label = 29;
1065
- case 29: return [4 /*yield*/, this.cascadeUpdateAsync('modi', modiUpsert, context, option)];
1066
- case 30:
1067
- _o.sent();
1068
- return [2 /*return*/, 1];
1069
- case 31:
1070
- createOper = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1071
- var createOper_1;
1072
- var _a, _b, _c;
1073
- var _this = this;
1074
- return tslib_1.__generator(this, function (_d) {
1075
- switch (_d.label) {
1076
- case 0:
1077
- if (!(!(option === null || option === void 0 ? void 0 : option.dontCreateOper) && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity) && ids_1.length > 0)) return [3 /*break*/, 3];
1078
- // 按照框架要求生成Oper和OperEntity这两个内置的对象
1079
- (0, assert_1.default)(operId);
1080
- _a = {
1081
- id: 'dummy',
1082
- action: 'create'
1083
- };
1084
- _b = {
1085
- id: operId,
1086
- action: action,
1087
- data: data
1088
- };
1089
- _c = {
1090
- id: 'dummy',
1091
- action: 'create'
1092
- };
1093
- return [4 /*yield*/, Promise.all(ids_1.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1094
- var _a;
1095
- return tslib_1.__generator(this, function (_b) {
1096
- switch (_b.label) {
1097
- case 0:
1098
- _a = {};
1099
- return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1100
- case 1: return [2 /*return*/, (_a.id = _b.sent(),
1101
- _a.entity = entity,
1102
- _a.entityId = ele,
1103
- _a)];
1104
- }
1105
- });
1106
- }); }))];
1107
- case 1:
1108
- createOper_1 = (_a.data = (_b.operEntity$oper = (_c.data = _d.sent(),
1109
- _c),
1110
- _b),
1111
- _a);
1112
- return [4 /*yield*/, this.cascadeUpdateAsync('oper', createOper_1, context, {
1113
- dontCollect: true,
1114
- dontCreateOper: true,
1115
- })];
1116
- case 2:
1117
- _d.sent();
1118
- _d.label = 3;
1119
- case 3: return [2 /*return*/];
1120
- }
1121
- });
1122
- }); };
1123
- if (!(action === 'remove')) return [3 /*break*/, 32];
1124
- if (!option.dontCollect) {
1125
- context.opRecords.push({
1126
- a: 'r',
1127
- e: entity,
1128
- f: {
1129
- id: {
1130
- $in: ids_1,
1131
- }
1132
- },
1133
- });
1134
- }
1135
- return [3 /*break*/, 36];
1136
- case 32:
1137
- updateAttrCount = Object.keys(data).length;
1138
- if (!(updateAttrCount > 0)) return [3 /*break*/, 33];
1139
- // 优化一下,如果不更新任何属性,则不实际执行
1140
- Object.assign(data, {
1141
- $$updateAt$$: now,
1142
- });
1143
- if (!option.dontCollect) {
1144
- context.opRecords.push({
1145
- a: 'u',
1146
- e: entity,
1147
- d: data,
1148
- f: {
1149
- id: {
1150
- $in: ids_1,
1151
- }
1152
- },
1153
- });
1154
- }
1155
- return [3 /*break*/, 36];
1156
- case 33:
1157
- if (!(action !== 'update')) return [3 /*break*/, 35];
1158
- // 如果不是update动作而是用户自定义的动作,这里还是要记录oper
1159
- return [4 /*yield*/, createOper()];
1160
- case 34:
1161
- // 如果不是update动作而是用户自定义的动作,这里还是要记录oper
1162
- _o.sent();
1163
- return [2 /*return*/, 0];
1164
- case 35: return [2 /*return*/, 0];
1165
- case 36: return [4 /*yield*/, this.updateAbjointRowAsync(entity, operation, context, option)];
1166
- case 37:
1167
- result = _o.sent();
1168
- return [4 /*yield*/, createOper()];
1169
- case 38:
1170
- _o.sent();
1171
- return [2 /*return*/, result];
1172
- }
1173
- });
1174
- });
1175
- };
1176
- CascadeStore.prototype.doUpdateSingleRow = function (entity, operation, context, option) {
1177
- var e_4, _a;
1178
- var _this = this;
1179
- var data = operation.data, action = operation.action, operId = operation.id, filter = operation.filter;
1180
- var now = Date.now();
1181
- switch (action) {
1182
- case 'create': {
1183
- this.preProcessDataCreated(entity, data);
1184
- var result_2 = 0;
1185
- var createInner = function (operation2) {
1186
- try {
1187
- result_2 += _this.updateAbjointRow(entity, operation2, context, option);
1188
- }
1189
- catch (e) {
1190
- throw e;
1191
- }
1192
- };
1193
- if (data instanceof Array) {
1194
- var multipleCreate = this.supportMultipleCreate();
1195
- if (multipleCreate) {
1196
- createInner(operation);
1197
- }
1198
- else {
1199
- try {
1200
- for (var data_2 = tslib_1.__values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
1201
- var d = data_2_1.value;
1202
- var createSingleOper = {
1203
- id: 'any',
1204
- action: 'create',
1205
- data: d,
1206
- };
1207
- createInner(createSingleOper);
1208
- }
1209
- }
1210
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
1211
- finally {
1212
- try {
1213
- if (data_2_1 && !data_2_1.done && (_a = data_2.return)) _a.call(data_2);
1214
- }
1215
- finally { if (e_4) throw e_4.error; }
1216
- }
1217
- }
1218
- }
1219
- else {
1220
- createInner(operation);
1221
- }
1222
- return result_2;
1223
- }
1224
- default: {
1225
- if (action === 'remove') {
1226
- }
1227
- else {
1228
- var updateAttrCount = Object.keys(data).length;
1229
- if (updateAttrCount > 0) {
1230
- // 优化一下,如果不更新任何属性,则不实际执行
1231
- Object.assign(data, {
1232
- $$updateAt$$: now,
1233
- });
1234
- this.preProcessDataUpdated(data);
1235
- }
1236
- else {
1237
- return 0;
1238
- }
1239
- }
1240
- return this.updateAbjointRow(entity, operation, context, option);
1241
- }
1242
- }
1243
- };
1244
- CascadeStore.prototype.cascadeUpdate = function (entity, operation, context, option) {
1245
- var e_5, _a, e_6, _b, e_7, _c;
1246
- var action = operation.action, data = operation.data, filter = operation.filter, id = operation.id;
1247
- var opData;
1248
- var wholeBeforeFns = [];
1249
- var wholeAfterFns = [];
1250
- var result = {};
1251
- if (['create', 'create-l'].includes(action) && data instanceof Array) {
1252
- opData = [];
1253
- try {
1254
- for (var data_3 = tslib_1.__values(data), data_3_1 = data_3.next(); !data_3_1.done; data_3_1 = data_3.next()) {
1255
- var d = data_3_1.value;
1256
- var _d = this.destructCascadeUpdate(entity, action, d, context, option, this.cascadeUpdate), od = _d.data, beforeFns = _d.beforeFns, afterFns = _d.afterFns;
1257
- opData.push(od);
1258
- wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1259
- wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1260
- }
1261
- }
1262
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
1263
- finally {
1264
- try {
1265
- if (data_3_1 && !data_3_1.done && (_a = data_3.return)) _a.call(data_3);
1266
- }
1267
- finally { if (e_5) throw e_5.error; }
1268
- }
1269
- }
1270
- else {
1271
- var _e = this.destructCascadeUpdate(entity, action, data, context, option, this.cascadeUpdate, filter), od = _e.data, beforeFns = _e.beforeFns, afterFns = _e.afterFns;
1272
- opData = od;
1273
- wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1274
- wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1275
- }
1276
- var operation2 = Object.assign({}, operation, {
1277
- data: opData,
1278
- });
1279
- try {
1280
- for (var wholeBeforeFns_1 = tslib_1.__values(wholeBeforeFns), wholeBeforeFns_1_1 = wholeBeforeFns_1.next(); !wholeBeforeFns_1_1.done; wholeBeforeFns_1_1 = wholeBeforeFns_1.next()) {
1281
- var before_1 = wholeBeforeFns_1_1.value;
1282
- before_1();
1283
- }
1284
- }
1285
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
1286
- finally {
1287
- try {
1288
- if (wholeBeforeFns_1_1 && !wholeBeforeFns_1_1.done && (_b = wholeBeforeFns_1.return)) _b.call(wholeBeforeFns_1);
1289
- }
1290
- finally { if (e_6) throw e_6.error; }
1291
- }
1292
- var count = this.doUpdateSingleRow(entity, operation2, context, option);
1293
- try {
1294
- for (var wholeAfterFns_1 = tslib_1.__values(wholeAfterFns), wholeAfterFns_1_1 = wholeAfterFns_1.next(); !wholeAfterFns_1_1.done; wholeAfterFns_1_1 = wholeAfterFns_1.next()) {
1295
- var after_1 = wholeAfterFns_1_1.value;
1296
- after_1();
1297
- }
1298
- }
1299
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
1300
- finally {
1301
- try {
1302
- if (wholeAfterFns_1_1 && !wholeAfterFns_1_1.done && (_c = wholeAfterFns_1.return)) _c.call(wholeAfterFns_1);
1303
- }
1304
- finally { if (e_7) throw e_7.error; }
1305
- }
1306
- return result;
1307
- };
1308
- /**
1309
- *
1310
- * @param entity
1311
- * @param operation
1312
- * @param context
1313
- * @param option
1314
- */
1315
- CascadeStore.prototype.cascadeUpdateAsync = function (entity, operation, context, option) {
1316
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1317
- var action, data, filter, id, opData, wholeBeforeFns, wholeAfterFns, result, data_4, data_4_1, d, _a, od, beforeFns, afterFns, _b, od, beforeFns, afterFns, operation2, wholeBeforeFns_2, wholeBeforeFns_2_1, before_2, e_8_1, count, wholeAfterFns_2, wholeAfterFns_2_1, after_2, e_9_1;
1318
- var e_10, _c, e_8, _d, _e, _f, e_9, _g;
1319
- return tslib_1.__generator(this, function (_h) {
1320
- switch (_h.label) {
1321
- case 0:
1322
- action = operation.action, data = operation.data, filter = operation.filter, id = operation.id;
1323
- wholeBeforeFns = [];
1324
- wholeAfterFns = [];
1325
- result = {};
1326
- if (['create', 'create-l'].includes(action) && data instanceof Array) {
1327
- opData = [];
1328
- try {
1329
- for (data_4 = tslib_1.__values(data), data_4_1 = data_4.next(); !data_4_1.done; data_4_1 = data_4.next()) {
1330
- d = data_4_1.value;
1331
- _a = this.destructCascadeUpdate(entity, action, d, context, option, this.cascadeUpdateAsync), od = _a.data, beforeFns = _a.beforeFns, afterFns = _a.afterFns;
1332
- opData.push(od);
1333
- wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1334
- wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1335
- }
1336
- }
1337
- catch (e_10_1) { e_10 = { error: e_10_1 }; }
1338
- finally {
1339
- try {
1340
- if (data_4_1 && !data_4_1.done && (_c = data_4.return)) _c.call(data_4);
1341
- }
1342
- finally { if (e_10) throw e_10.error; }
1343
- }
1344
- }
1345
- else {
1346
- _b = this.destructCascadeUpdate(entity, action, data, context, option, this.cascadeUpdateAsync, filter), od = _b.data, beforeFns = _b.beforeFns, afterFns = _b.afterFns;
1347
- opData = od;
1348
- wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1349
- wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1350
- }
1351
- operation2 = Object.assign({}, operation, {
1352
- data: opData,
1353
- });
1354
- _h.label = 1;
1355
- case 1:
1356
- _h.trys.push([1, 6, 7, 8]);
1357
- wholeBeforeFns_2 = tslib_1.__values(wholeBeforeFns), wholeBeforeFns_2_1 = wholeBeforeFns_2.next();
1358
- _h.label = 2;
1359
- case 2:
1360
- if (!!wholeBeforeFns_2_1.done) return [3 /*break*/, 5];
1361
- before_2 = wholeBeforeFns_2_1.value;
1362
- return [4 /*yield*/, before_2()];
1363
- case 3:
1364
- _h.sent();
1365
- _h.label = 4;
1366
- case 4:
1367
- wholeBeforeFns_2_1 = wholeBeforeFns_2.next();
1368
- return [3 /*break*/, 2];
1369
- case 5: return [3 /*break*/, 8];
1370
- case 6:
1371
- e_8_1 = _h.sent();
1372
- e_8 = { error: e_8_1 };
1373
- return [3 /*break*/, 8];
1374
- case 7:
1375
- try {
1376
- if (wholeBeforeFns_2_1 && !wholeBeforeFns_2_1.done && (_d = wholeBeforeFns_2.return)) _d.call(wholeBeforeFns_2);
1377
- }
1378
- finally { if (e_8) throw e_8.error; }
1379
- return [7 /*endfinally*/];
1380
- case 8: return [4 /*yield*/, this.doUpdateSingleRowAsync(entity, operation2, context, option)];
1381
- case 9:
1382
- count = _h.sent();
1383
- this.mergeOperationResult(result, (_e = {},
1384
- _e[entity] = (_f = {},
1385
- _f[operation2.action] = count,
1386
- _f),
1387
- _e));
1388
- _h.label = 10;
1389
- case 10:
1390
- _h.trys.push([10, 15, 16, 17]);
1391
- wholeAfterFns_2 = tslib_1.__values(wholeAfterFns), wholeAfterFns_2_1 = wholeAfterFns_2.next();
1392
- _h.label = 11;
1393
- case 11:
1394
- if (!!wholeAfterFns_2_1.done) return [3 /*break*/, 14];
1395
- after_2 = wholeAfterFns_2_1.value;
1396
- return [4 /*yield*/, after_2()];
1397
- case 12:
1398
- _h.sent();
1399
- _h.label = 13;
1400
- case 13:
1401
- wholeAfterFns_2_1 = wholeAfterFns_2.next();
1402
- return [3 /*break*/, 11];
1403
- case 14: return [3 /*break*/, 17];
1404
- case 15:
1405
- e_9_1 = _h.sent();
1406
- e_9 = { error: e_9_1 };
1407
- return [3 /*break*/, 17];
1408
- case 16:
1409
- try {
1410
- if (wholeAfterFns_2_1 && !wholeAfterFns_2_1.done && (_g = wholeAfterFns_2.return)) _g.call(wholeAfterFns_2);
1411
- }
1412
- finally { if (e_9) throw e_9.error; }
1413
- return [7 /*endfinally*/];
1414
- case 17: return [2 /*return*/, result];
1415
- }
1416
- });
1417
- });
1418
- };
1419
- CascadeStore.prototype.cascadeSelect = function (entity, selection, context, option) {
1420
- var data = selection.data, filter = selection.filter, indexFrom = selection.indexFrom, count = selection.count, sorter = selection.sorter;
1421
- var _a = this.destructCascadeSelect(entity, data, context, this.cascadeSelect, this.aggregateSync, option), projection = _a.projection, cascadeSelectionFns = _a.cascadeSelectionFns;
1422
- var rows = this.selectAbjointRow(entity, {
1423
- data: projection,
1424
- filter: filter,
1425
- indexFrom: indexFrom,
1426
- count: count,
1427
- sorter: sorter
1428
- }, context, option);
1429
- if (cascadeSelectionFns.length > 0) {
1430
- var ruException_1 = [];
1431
- cascadeSelectionFns.forEach(function (ele) {
1432
- try {
1433
- ele(rows);
1434
- }
1435
- catch (e) {
1436
- if (e instanceof types_1.OakRowUnexistedException) {
1437
- var rows_1 = e.getRows();
1438
- ruException_1.push.apply(ruException_1, tslib_1.__spreadArray([], tslib_1.__read(rows_1), false));
1439
- }
1440
- else {
1441
- throw e;
1442
- }
1443
- }
1444
- });
1445
- if (ruException_1.length > 0) {
1446
- throw new types_1.OakRowUnexistedException(ruException_1);
1447
- }
1448
- }
1449
- return rows;
1450
- };
1451
- /**
1452
- * 将一次查询的结果集加入result
1453
- * todo 如果是supportMtoOJoin,这里还要解构(未充分测试)
1454
- * @param entity
1455
- * @param rows
1456
- * @param context
1457
- */
1458
- CascadeStore.prototype.addToResultSelections = function (entity, rows, context) {
1459
- if (this.supportManyToOneJoin()) {
1460
- var attrsToPick_1 = [];
1461
- var _loop_3 = function (attr) {
1462
- var data = {};
1463
- var rel = this_3.judgeRelation(entity, attr);
1464
- if (rel === 2) {
1465
- this_3.addToResultSelections(attr, rows.map(function (ele) { return ele[attr]; }).filter(function (ele) { return !!ele; }), context);
1466
- }
1467
- else if (typeof rel === 'string') {
1468
- this_3.addToResultSelections(rel, rows.map(function (ele) { return ele[attr]; }).filter(function (ele) { return !!ele; }), context);
1469
- }
1470
- else if (rel instanceof Array) {
1471
- this_3.addToResultSelections(rel[0], rows.map(function (ele) { return ele[attr]; }).reduce(function (prev, current) { return prev.concat(current); }, []), context);
1472
- }
1473
- else {
1474
- attrsToPick_1.push(attr);
1475
- }
1476
- };
1477
- var this_3 = this;
1478
- for (var attr in rows[0]) {
1479
- _loop_3(attr);
1480
- }
1481
- var originRows = rows.map(function (ele) { return (0, lodash_1.pick)(ele, attrsToPick_1); });
1482
- this.addSingleRowToResultSelections(entity, originRows, context);
1483
- }
1484
- else {
1485
- this.addSingleRowToResultSelections(entity, rows, context);
1486
- }
1487
- };
1488
- CascadeStore.prototype.addSingleRowToResultSelections = function (entity, rows, context) {
1489
- var _a;
1490
- var opRecords = context.opRecords;
1491
- var lastOperation = opRecords[opRecords.length - 1];
1492
- if (lastOperation && lastOperation.a === 's') {
1493
- var entityBranch_1 = lastOperation.d[entity];
1494
- if (entityBranch_1) {
1495
- rows.forEach(function (row) {
1496
- var _a;
1497
- if (row) {
1498
- (0, assert_1.default)(row.id);
1499
- var id = row.id;
1500
- if (!entityBranch_1[id]) {
1501
- Object.assign(entityBranch_1, (_a = {},
1502
- _a[id] = row,
1503
- _a));
1504
- }
1505
- else {
1506
- Object.assign(entityBranch_1[id], row);
1507
- }
1508
- }
1509
- });
1510
- return;
1511
- }
1512
- }
1513
- else {
1514
- lastOperation = {
1515
- a: 's',
1516
- d: {},
1517
- };
1518
- opRecords.push(lastOperation);
1519
- }
1520
- var entityBranch = {};
1521
- rows.forEach(function (row) {
1522
- var _a;
1523
- if (row) {
1524
- var id = row.id;
1525
- Object.assign(entityBranch, (_a = {},
1526
- _a[id] = row,
1527
- _a));
1528
- }
1529
- });
1530
- Object.assign(lastOperation.d, (_a = {},
1531
- _a[entity] = entityBranch,
1532
- _a));
1533
- };
1534
- CascadeStore.prototype.cascadeSelectAsync = function (entity, selection, context, option) {
1535
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1536
- var data, filter, indexFrom, count, sorter, _a, projection, cascadeSelectionFns, rows, ruException_2;
1537
- var _this = this;
1538
- return tslib_1.__generator(this, function (_b) {
1539
- switch (_b.label) {
1540
- case 0:
1541
- data = selection.data, filter = selection.filter, indexFrom = selection.indexFrom, count = selection.count, sorter = selection.sorter;
1542
- _a = this.destructCascadeSelect(entity, data, context, this.cascadeSelectAsync, this.aggregateAsync, option), projection = _a.projection, cascadeSelectionFns = _a.cascadeSelectionFns;
1543
- return [4 /*yield*/, this.selectAbjointRowAsync(entity, {
1544
- data: projection,
1545
- filter: filter,
1546
- indexFrom: indexFrom,
1547
- count: count,
1548
- sorter: sorter
1549
- }, context, option)];
1550
- case 1:
1551
- rows = _b.sent();
1552
- if (!option.dontCollect) {
1553
- this.addToResultSelections(entity, rows, context);
1554
- }
1555
- if (!(cascadeSelectionFns.length > 0)) return [3 /*break*/, 3];
1556
- ruException_2 = [];
1557
- return [4 /*yield*/, Promise.all(cascadeSelectionFns.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1558
- var e_11, rows_2;
1559
- return tslib_1.__generator(this, function (_a) {
1560
- switch (_a.label) {
1561
- case 0:
1562
- _a.trys.push([0, 2, , 3]);
1563
- return [4 /*yield*/, ele(rows)];
1564
- case 1:
1565
- _a.sent();
1566
- return [3 /*break*/, 3];
1567
- case 2:
1568
- e_11 = _a.sent();
1569
- if (e_11 instanceof types_1.OakRowUnexistedException) {
1570
- rows_2 = e_11.getRows();
1571
- ruException_2.push.apply(ruException_2, tslib_1.__spreadArray([], tslib_1.__read(rows_2), false));
1572
- }
1573
- else {
1574
- throw e_11;
1575
- }
1576
- return [3 /*break*/, 3];
1577
- case 3: return [2 /*return*/];
1578
- }
1579
- });
1580
- }); }))];
1581
- case 2:
1582
- _b.sent();
1583
- if (ruException_2.length > 0) {
1584
- throw new types_1.OakRowUnexistedException(ruException_2);
1585
- }
1586
- _b.label = 3;
1587
- case 3: return [2 /*return*/, rows];
1588
- }
1589
- });
1590
- });
1591
- };
1592
- return CascadeStore;
1593
- }(RowStore_1.RowStore));
1594
- exports.CascadeStore = CascadeStore;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CascadeStore = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var assert_1 = tslib_1.__importDefault(require("assert"));
6
+ var Entity_1 = require("../types/Entity");
7
+ var RowStore_1 = require("../types/RowStore");
8
+ var filter_1 = require("./filter");
9
+ var relation_1 = require("./relation");
10
+ var types_1 = require("../types");
11
+ var lodash_1 = require("../utils/lodash");
12
+ var filter_2 = require("./filter");
13
+ var uuid_1 = require("../utils/uuid");
14
+ /**这个用来处理级联的select和update,对不同能力的 */
15
+ var CascadeStore = /** @class */ (function (_super) {
16
+ tslib_1.__extends(CascadeStore, _super);
17
+ function CascadeStore(storageSchema) {
18
+ return _super.call(this, storageSchema) || this;
19
+ }
20
+ CascadeStore.prototype.destructCascadeSelect = function (entity, projection2, context, cascadeSelectFn, aggregateFn, option) {
21
+ var _this = this;
22
+ var projection = {};
23
+ var cascadeSelectionFns = [];
24
+ var supportMtoJoin = this.supportManyToOneJoin();
25
+ var toModi = this.getSchema()[entity].toModi;
26
+ var _loop_1 = function (attr) {
27
+ var _a, _b, _c, _d;
28
+ var relation = (0, relation_1.judgeRelation)(this_1.storageSchema, entity, attr);
29
+ if (relation === 1 || relation == 0) {
30
+ Object.assign(projection, (_a = {},
31
+ _a[attr] = projection2[attr],
32
+ _a));
33
+ }
34
+ else if (relation === 2) {
35
+ // 基于entity/entityId的多对一
36
+ Object.assign(projection, {
37
+ entity: 1,
38
+ entityId: 1,
39
+ });
40
+ if (supportMtoJoin) {
41
+ cascadeSelectionFns.push(function (result) {
42
+ if (!toModi) {
43
+ result.forEach(function (ele) {
44
+ if (ele.entity === attr) {
45
+ (0, assert_1.default)(ele.entityId);
46
+ if (!ele[attr]) {
47
+ throw new types_1.OakRowUnexistedException([{
48
+ entity: attr,
49
+ selection: {
50
+ data: projection2[attr],
51
+ filter: {
52
+ id: ele.entityId,
53
+ }
54
+ }
55
+ }]);
56
+ }
57
+ }
58
+ });
59
+ }
60
+ });
61
+ var _e = this_1.destructCascadeSelect(attr, projection2[attr], context, cascadeSelectFn, aggregateFn, option), subProjection = _e.projection, subCascadeSelectionFns = _e.cascadeSelectionFns;
62
+ Object.assign(projection, (_b = {},
63
+ _b[attr] = subProjection,
64
+ _b));
65
+ subCascadeSelectionFns.forEach(function (ele) { return cascadeSelectionFns.push(function (result) {
66
+ return ele(result.map(function (ele2) { return ele2[attr]; }).filter(function (ele2) { return !!ele2; }));
67
+ }); });
68
+ }
69
+ else {
70
+ cascadeSelectionFns.push(function (result) {
71
+ var dealWithSubRows = function (subRows) {
72
+ (0, assert_1.default)(subRows.length <= entityIds.length);
73
+ if (subRows.length < entityIds.length && !toModi) {
74
+ throw new types_1.OakRowUnexistedException([{
75
+ entity: attr,
76
+ selection: {
77
+ data: projection2[attr],
78
+ filter: {
79
+ id: {
80
+ $in: entityIds
81
+ },
82
+ },
83
+ },
84
+ }]);
85
+ }
86
+ result.forEach(function (ele) {
87
+ var _a, _b;
88
+ if (ele.entity === attr) {
89
+ var subRow = subRows.find(function (ele2) { return ele2.id === ele.entityId; });
90
+ if (subRow) {
91
+ Object.assign(ele, (_a = {},
92
+ _a[attr] = subRow,
93
+ _a));
94
+ }
95
+ else {
96
+ Object.assign(ele, (_b = {},
97
+ _b[attr] = null,
98
+ _b));
99
+ }
100
+ }
101
+ });
102
+ };
103
+ var entityIds = (0, lodash_1.uniq)(result.filter(function (ele) { return ele.entity === attr; }).map(function (ele) { return ele.entityId; }));
104
+ var subRows = cascadeSelectFn.call(_this, attr, {
105
+ data: projection2[attr],
106
+ filter: {
107
+ id: {
108
+ $in: entityIds
109
+ },
110
+ },
111
+ }, context, option);
112
+ if (subRows instanceof Promise) {
113
+ return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
114
+ }
115
+ else {
116
+ dealWithSubRows(subRows);
117
+ }
118
+ });
119
+ }
120
+ }
121
+ else if (typeof relation === 'string') {
122
+ Object.assign(projection, (_c = {},
123
+ _c["".concat(attr, "Id")] = 1,
124
+ _c));
125
+ if (supportMtoJoin) {
126
+ if (!toModi) {
127
+ // 如果不是modi,要保证外键没有空指针
128
+ cascadeSelectionFns.push(function (result) {
129
+ if (!toModi) {
130
+ result.forEach(function (ele) {
131
+ if (ele["".concat(attr, "Id")] && !ele[attr]) {
132
+ throw new types_1.OakRowUnexistedException([{
133
+ entity: relation,
134
+ selection: {
135
+ data: projection2[attr],
136
+ filter: {
137
+ id: ele["".concat(attr, "Id")],
138
+ }
139
+ }
140
+ }]);
141
+ }
142
+ });
143
+ }
144
+ });
145
+ }
146
+ var _f = this_1.destructCascadeSelect(relation, projection2[attr], context, cascadeSelectFn, aggregateFn, option), subProjection = _f.projection, subCascadeSelectionFns = _f.cascadeSelectionFns;
147
+ Object.assign(projection, (_d = {},
148
+ _d[attr] = subProjection,
149
+ _d));
150
+ subCascadeSelectionFns.forEach(function (ele) { return cascadeSelectionFns.push(function (result) {
151
+ return ele(result.map(function (ele2) { return ele2[attr]; }).filter(function (ele2) { return !!ele2; }));
152
+ }); });
153
+ }
154
+ else {
155
+ cascadeSelectionFns.push(function (result) {
156
+ var dealWithSubRows = function (subRows) {
157
+ (0, assert_1.default)(subRows.length <= ids.length);
158
+ if (subRows.length < ids.length && !toModi) {
159
+ throw new types_1.OakRowUnexistedException([{
160
+ entity: relation,
161
+ selection: {
162
+ data: projection2[attr],
163
+ filter: {
164
+ id: {
165
+ $in: ids
166
+ },
167
+ },
168
+ }
169
+ }]);
170
+ }
171
+ result.forEach(function (ele) {
172
+ var _a, _b, _c;
173
+ if (ele["".concat(attr, "Id")]) {
174
+ var subRow = subRows.find(function (ele2) { return ele2.id === ele["".concat(attr, "Id")]; });
175
+ if (subRow) {
176
+ Object.assign(ele, (_a = {},
177
+ _a[attr] = subRow,
178
+ _a));
179
+ }
180
+ else {
181
+ Object.assign(ele, (_b = {},
182
+ _b[attr] = null,
183
+ _b));
184
+ }
185
+ }
186
+ else {
187
+ Object.assign(ele, (_c = {},
188
+ _c[attr] = null,
189
+ _c));
190
+ }
191
+ });
192
+ };
193
+ var ids = (0, lodash_1.uniq)(result.filter(function (ele) { return !!(ele["".concat(attr, "Id")]); }).map(function (ele) { return ele["".concat(attr, "Id")]; }));
194
+ var subRows = cascadeSelectFn.call(_this, relation, {
195
+ data: projection2[attr],
196
+ filter: {
197
+ id: {
198
+ $in: ids
199
+ },
200
+ },
201
+ }, context, option);
202
+ if (subRows instanceof Promise) {
203
+ return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
204
+ }
205
+ dealWithSubRows(subRows);
206
+ });
207
+ }
208
+ }
209
+ else {
210
+ (0, assert_1.default)(relation instanceof Array);
211
+ var _g = projection2[attr], subProjection_1 = _g.data, subFilter_1 = _g.filter, indexFrom_1 = _g.indexFrom, count_1 = _g.count, subSorter_1 = _g.sorter;
212
+ var _h = tslib_1.__read(relation, 2), entity2_1 = _h[0], foreignKey_1 = _h[1];
213
+ var isAggr = attr.endsWith('$$aggr');
214
+ if (foreignKey_1) {
215
+ // 基于属性的一对多
216
+ if (isAggr) {
217
+ // 是聚合运算
218
+ cascadeSelectionFns.push(function (result) {
219
+ var aggrResults = result.map(function (row) {
220
+ var _a, _b;
221
+ var aggrResult = aggregateFn.call(_this, entity2_1, {
222
+ data: subProjection_1,
223
+ filter: (0, filter_1.combineFilters)([(_a = {},
224
+ _a[foreignKey_1] = row.id,
225
+ _a), subFilter_1]),
226
+ sorter: subSorter_1,
227
+ indexFrom: indexFrom_1,
228
+ count: count_1
229
+ }, context, option);
230
+ if (aggrResult instanceof Promise) {
231
+ return aggrResult.then(function (aggrResultResult) {
232
+ var _a;
233
+ return Object.assign(row, (_a = {},
234
+ _a[attr] = aggrResultResult,
235
+ _a));
236
+ });
237
+ }
238
+ else {
239
+ Object.assign(row, (_b = {},
240
+ _b[attr] = aggrResult,
241
+ _b));
242
+ }
243
+ });
244
+ if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
245
+ return Promise.all(aggrResults).then(function () { return undefined; });
246
+ }
247
+ });
248
+ }
249
+ else {
250
+ // 是一对多查询
251
+ cascadeSelectionFns.push(function (result) {
252
+ var _a;
253
+ var ids = result.map(function (ele) { return ele.id; });
254
+ var dealWithSubRows = function (subRows) {
255
+ result.forEach(function (ele) {
256
+ var _a;
257
+ var subRowss = subRows.filter(function (ele2) { return ele2[foreignKey_1] === ele.id; });
258
+ (0, assert_1.default)(subRowss);
259
+ Object.assign(ele, (_a = {},
260
+ _a[attr] = subRowss,
261
+ _a));
262
+ });
263
+ };
264
+ var subRows = cascadeSelectFn.call(_this, entity2_1, {
265
+ data: subProjection_1,
266
+ filter: (0, filter_1.combineFilters)([(_a = {},
267
+ _a[foreignKey_1] = {
268
+ $in: ids,
269
+ },
270
+ _a), subFilter_1]),
271
+ sorter: subSorter_1,
272
+ indexFrom: indexFrom_1,
273
+ count: count_1
274
+ }, context, option);
275
+ if (subRows instanceof Promise) {
276
+ return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
277
+ }
278
+ dealWithSubRows(subRows);
279
+ });
280
+ }
281
+ }
282
+ else {
283
+ // 基于entity的多对一
284
+ if (isAggr) {
285
+ // 是聚合运算
286
+ cascadeSelectionFns.push(function (result) {
287
+ var aggrResults = result.map(function (row) {
288
+ var _a;
289
+ var aggrResult = aggregateFn.call(_this, entity2_1, {
290
+ data: subProjection_1,
291
+ filter: (0, filter_1.combineFilters)([{
292
+ entity: entity,
293
+ entityId: row.id,
294
+ }, subFilter_1]),
295
+ sorter: subSorter_1,
296
+ indexFrom: indexFrom_1,
297
+ count: count_1
298
+ }, context, option);
299
+ if (aggrResult instanceof Promise) {
300
+ return aggrResult.then(function (aggrResultResult) {
301
+ var _a;
302
+ return Object.assign(row, (_a = {},
303
+ _a[attr] = aggrResultResult,
304
+ _a));
305
+ });
306
+ }
307
+ else {
308
+ Object.assign(row, (_a = {},
309
+ _a[attr] = aggrResult,
310
+ _a));
311
+ }
312
+ });
313
+ if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
314
+ return Promise.all(aggrResults).then(function () { return undefined; });
315
+ }
316
+ });
317
+ }
318
+ else {
319
+ // 是一对多查询
320
+ cascadeSelectionFns.push(function (result) {
321
+ var ids = result.map(function (ele) { return ele.id; });
322
+ var dealWithSubRows = function (subRows) {
323
+ result.forEach(function (ele) {
324
+ var _a;
325
+ var subRowss = subRows.filter(function (ele2) { return ele2.entityId === ele.id; });
326
+ (0, assert_1.default)(subRowss);
327
+ Object.assign(ele, (_a = {},
328
+ _a[attr] = subRowss,
329
+ _a));
330
+ });
331
+ };
332
+ var subRows = cascadeSelectFn.call(_this, entity2_1, {
333
+ data: subProjection_1,
334
+ filter: (0, filter_1.combineFilters)([{
335
+ entity: entity,
336
+ entityId: {
337
+ $in: ids,
338
+ }
339
+ }, subFilter_1]),
340
+ sorter: subSorter_1,
341
+ indexFrom: indexFrom_1,
342
+ count: count_1
343
+ }, context, option);
344
+ if (subRows instanceof Promise) {
345
+ return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
346
+ }
347
+ dealWithSubRows(subRows);
348
+ });
349
+ }
350
+ }
351
+ }
352
+ };
353
+ var this_1 = this;
354
+ for (var attr in projection2) {
355
+ _loop_1(attr);
356
+ }
357
+ return {
358
+ projection: projection,
359
+ cascadeSelectionFns: cascadeSelectionFns,
360
+ };
361
+ };
362
+ /**
363
+ * 级联更新
364
+ * A --> B
365
+ 多对一:A CREATE/B CREATE,B data的主键赋到A的data上
366
+ A CREATE/B UPDATE,B filter的主键来自A的data
367
+ A UPDATE/B CREATE,B data的主键赋到A的data上
368
+ A UPDATE/B UPDATE,B filter的主键来自A的row
369
+ A UPDATE/B REMOVE,B filter的主键来自A的row
370
+ A REMOVE/B UPDATE,B filter的主键来自A的row
371
+ A REMOVE/B REMOVE,B filter的主键来自A的row
372
+
373
+ 一对多:A CREATE/B CREATE,A data上的主键赋到B的data上
374
+ A CREATE/B UPDATE,A data上的主键赋到B的data上
375
+ A UPDATE/B CREATE,A filter上的主键赋到B的data上(一定是带主键的filter)
376
+ A UPDATE/B UPDATE,A filter上的主键赋到B的filter上(一定是带主键的filter)
377
+ A UPDATE/B REMOVE,A filter上的主键赋到B的filter上(一定是带主键的filter)
378
+ A REMOVE/B UPDATE,A filter上的主键赋到B的filter上(且B关于A的外键清空)
379
+ A REMOVE/B REMOVE,A filter上的主键赋到B的filter上
380
+ *
381
+ * 延时更新,
382
+ * A(业务级别的申请对象) ---> B(业务级别需要更新的对象)
383
+ * 两者必须通过entity/entityId关联
384
+ * 此时需要把对B的更新记录成一条新插入的Modi对象,并将A上的entity/entityId指向该对象(新生成的Modi对象的id与此operation的id保持一致)
385
+ * @param entity
386
+ * @param action
387
+ * @param data
388
+ * @param context
389
+ * @param option
390
+ * @param result
391
+ * @param filter
392
+ * @returns
393
+ */
394
+ CascadeStore.prototype.destructCascadeUpdate = function (entity, action, data, context, option, cascadeUpdate, filter) {
395
+ var _this = this;
396
+ var modiAttr = this.getSchema()[entity].toModi;
397
+ var option2 = Object.assign({}, option);
398
+ var opData = {};
399
+ var beforeFns = [];
400
+ var afterFns = [];
401
+ if (modiAttr && action !== 'remove' && !option.dontCreateModi) {
402
+ // create/update具有modi对象的对象,对其子对象的update行为全部是create modi对象(缓存动作)
403
+ // delete此对象,所有的modi子对象应该通过触发器作废,这个目前先通过系统的trigger来实现
404
+ (0, assert_1.default)(!option2.modiParentId && !option2.modiParentEntity);
405
+ if (action === 'create') {
406
+ option2.modiParentId = data.id;
407
+ }
408
+ else {
409
+ (0, assert_1.default)((filter === null || filter === void 0 ? void 0 : filter.id) && typeof filter.id === 'string');
410
+ option2.modiParentId = filter.id;
411
+ }
412
+ option2.modiParentEntity = entity;
413
+ }
414
+ var _loop_2 = function (attr) {
415
+ var _a, _b, _c, e_1, _d;
416
+ var relation = (0, relation_1.judgeRelation)(this_2.storageSchema, entity, attr);
417
+ if (relation === 1) {
418
+ Object.assign(opData, (_a = {},
419
+ _a[attr] = data[attr],
420
+ _a));
421
+ }
422
+ else if (relation === 2) {
423
+ // 基于entity/entityId的many-to-one
424
+ var operationMto_1 = data[attr];
425
+ var actionMto = operationMto_1.action, dataMto = operationMto_1.data, filterMto = operationMto_1.filter;
426
+ if (actionMto === 'create') {
427
+ Object.assign(opData, {
428
+ entityId: dataMto.id,
429
+ entity: attr,
430
+ });
431
+ }
432
+ else if (action === 'create') {
433
+ var fkId = data.entityId, entity_1 = data.entity;
434
+ (0, assert_1.default)(typeof fkId === 'string' || entity_1 === attr);
435
+ if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
436
+ // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
437
+ (0, assert_1.default)(filterMto.id === fkId);
438
+ }
439
+ else {
440
+ // A中data的entityId作为B中filter的主键
441
+ Object.assign(operationMto_1, {
442
+ filter: (0, filter_1.addFilterSegment)({
443
+ id: fkId,
444
+ }),
445
+ filterMto: filterMto,
446
+ });
447
+ }
448
+ }
449
+ else {
450
+ // 剩下三种情况都是B中的filter的id来自A中row的entityId
451
+ (0, assert_1.default)(!data.hasOwnProperty('entityId') && !data.hasOwnProperty('entity'));
452
+ if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
453
+ // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
454
+ (0, assert_1.default)(typeof filterMto.id === 'string');
455
+ }
456
+ else {
457
+ // A中data的entityId作为B中filter的主键
458
+ Object.assign(operationMto_1, {
459
+ filter: (0, filter_1.addFilterSegment)({
460
+ id: {
461
+ $in: {
462
+ entity: entity,
463
+ data: {
464
+ entityId: 1,
465
+ },
466
+ filter: (0, filter_1.addFilterSegment)({
467
+ entity: attr,
468
+ }, filter),
469
+ }
470
+ },
471
+ }, filterMto),
472
+ });
473
+ }
474
+ }
475
+ beforeFns.push(function () { return cascadeUpdate.call(_this, attr, operationMto_1, context, option2); });
476
+ }
477
+ else if (typeof relation === 'string') {
478
+ // 基于attr的外键的many-to-one
479
+ var operationMto_2 = data[attr];
480
+ var actionMto = operationMto_2.action, dataMto = operationMto_2.data, filterMto = operationMto_2.filter;
481
+ if (actionMto === 'create') {
482
+ Object.assign(opData, (_b = {},
483
+ _b["".concat(attr, "Id")] = dataMto.id,
484
+ _b));
485
+ }
486
+ else if (action === 'create') {
487
+ var _e = data, _f = "".concat(attr, "Id"), fkId = _e[_f];
488
+ (0, assert_1.default)(typeof fkId === 'string');
489
+ if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
490
+ // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
491
+ (0, assert_1.default)(filterMto.id === fkId);
492
+ }
493
+ else {
494
+ // A中data的entityId作为B中filter的主键
495
+ Object.assign(operationMto_2, {
496
+ filter: (0, filter_1.addFilterSegment)(filterMto || {}, {
497
+ id: fkId,
498
+ }),
499
+ });
500
+ }
501
+ }
502
+ else {
503
+ (0, assert_1.default)(!data.hasOwnProperty("".concat(attr, "Id")));
504
+ if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
505
+ // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
506
+ (0, assert_1.default)(typeof filterMto.id === 'string');
507
+ }
508
+ else {
509
+ // A中data的entityId作为B中filter的主键
510
+ Object.assign(operationMto_2, {
511
+ filter: (0, filter_1.addFilterSegment)(filterMto || {}, {
512
+ id: {
513
+ $in: {
514
+ entity: entity,
515
+ data: (_c = {},
516
+ _c["".concat(attr, "Id")] = 1,
517
+ _c),
518
+ filter: filter,
519
+ }
520
+ },
521
+ }),
522
+ });
523
+ }
524
+ }
525
+ beforeFns.push(function () { return cascadeUpdate.call(_this, relation, operationMto_2, context, option2); });
526
+ }
527
+ else {
528
+ (0, assert_1.default)(relation instanceof Array);
529
+ var _g = tslib_1.__read(relation, 2), entityOtm_1 = _g[0], foreignKey_2 = _g[1];
530
+ var otmOperations = data[attr];
531
+ var dealWithOneToMany = function (otm) {
532
+ var _a, _b, _c, _d;
533
+ var actionOtm = otm.action, dataOtm = otm.data, filterOtm = otm.filter;
534
+ if (!foreignKey_2) {
535
+ // 基于entity/entityId的one-to-many
536
+ if (action === 'create') {
537
+ var id_1 = data.id;
538
+ if (dataOtm instanceof Array) {
539
+ dataOtm.forEach(function (ele) { return Object.assign(ele, {
540
+ entity: entity,
541
+ entityId: id_1,
542
+ }); });
543
+ }
544
+ else {
545
+ Object.assign(dataOtm, {
546
+ entity: entity,
547
+ entityId: id_1,
548
+ });
549
+ }
550
+ }
551
+ else if (actionOtm === 'create') {
552
+ // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
553
+ // todo 这个假设成立吗?等遇到create/create一对多的case再完善
554
+ var id_2 = filter.id;
555
+ (0, assert_1.default)(typeof id_2 === 'string');
556
+ if (dataOtm instanceof Array) {
557
+ dataOtm.forEach(function (ele) { return Object.assign(ele, {
558
+ entity: entity,
559
+ entityId: id_2,
560
+ }); });
561
+ }
562
+ else {
563
+ Object.assign(dataOtm, {
564
+ entity: entity,
565
+ entityId: id_2,
566
+ });
567
+ }
568
+ }
569
+ else {
570
+ // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
571
+ // 这个倒是好像不可能出现create/update的一对多,如果遇到了再完善
572
+ var id = filter.id;
573
+ Object.assign(otm, {
574
+ filter: (0, filter_1.addFilterSegment)({
575
+ entity: entity,
576
+ entityId: id,
577
+ }, filterOtm),
578
+ });
579
+ if (action === 'remove' && actionOtm === 'update') {
580
+ Object.assign(dataOtm, {
581
+ entity: null,
582
+ entityId: null,
583
+ });
584
+ }
585
+ }
586
+ }
587
+ else {
588
+ // 基于foreignKey的one-to-many
589
+ if (action === 'create') {
590
+ var id_3 = data.id;
591
+ if (dataOtm instanceof Array) {
592
+ dataOtm.forEach(function (ele) {
593
+ var _a;
594
+ return Object.assign(ele, (_a = {},
595
+ _a[foreignKey_2] = id_3,
596
+ _a));
597
+ });
598
+ }
599
+ else {
600
+ Object.assign(dataOtm, (_a = {},
601
+ _a[foreignKey_2] = id_3,
602
+ _a));
603
+ }
604
+ }
605
+ else if (actionOtm === 'create') {
606
+ // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
607
+ // todo 这个假设成立吗?等遇到create/create一对多的case再完善
608
+ var id_4 = filter.id;
609
+ (0, assert_1.default)(typeof id_4 === 'string');
610
+ if (dataOtm instanceof Array) {
611
+ dataOtm.forEach(function (ele) {
612
+ var _a;
613
+ return Object.assign(ele, (_a = {},
614
+ _a[foreignKey_2] = id_4,
615
+ _a));
616
+ });
617
+ }
618
+ else {
619
+ Object.assign(dataOtm, (_b = {},
620
+ _b[foreignKey_2] = id_4,
621
+ _b));
622
+ }
623
+ }
624
+ else {
625
+ // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
626
+ // 这个倒是好像不可能出现create/update的一对多,如果遇到了再完善
627
+ var id = filter.id;
628
+ Object.assign(otm, {
629
+ filter: (0, filter_1.addFilterSegment)((_c = {},
630
+ _c[foreignKey_2] = id,
631
+ _c), filterOtm),
632
+ });
633
+ if (action === 'remove' && actionOtm === 'update') {
634
+ Object.assign(dataOtm, (_d = {},
635
+ _d[foreignKey_2] = null,
636
+ _d));
637
+ }
638
+ }
639
+ }
640
+ afterFns.push(function () { return cascadeUpdate.call(_this, entityOtm_1, otm, context, option2); });
641
+ };
642
+ if (otmOperations instanceof Array) {
643
+ try {
644
+ for (var otmOperations_1 = (e_1 = void 0, tslib_1.__values(otmOperations)), otmOperations_1_1 = otmOperations_1.next(); !otmOperations_1_1.done; otmOperations_1_1 = otmOperations_1.next()) {
645
+ var oper = otmOperations_1_1.value;
646
+ dealWithOneToMany(oper);
647
+ }
648
+ }
649
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
650
+ finally {
651
+ try {
652
+ if (otmOperations_1_1 && !otmOperations_1_1.done && (_d = otmOperations_1.return)) _d.call(otmOperations_1);
653
+ }
654
+ finally { if (e_1) throw e_1.error; }
655
+ }
656
+ }
657
+ else {
658
+ dealWithOneToMany(otmOperations);
659
+ }
660
+ }
661
+ };
662
+ var this_2 = this;
663
+ for (var attr in data) {
664
+ _loop_2(attr);
665
+ }
666
+ return {
667
+ data: opData,
668
+ beforeFns: beforeFns,
669
+ afterFns: afterFns,
670
+ };
671
+ };
672
+ // 对插入的数据,没有初始值的属性置null
673
+ CascadeStore.prototype.preProcessDataCreated = function (entity, data) {
674
+ var now = Date.now();
675
+ var attributes = this.getSchema()[entity].attributes;
676
+ var processSingle = function (data2) {
677
+ var _a, _b;
678
+ for (var key in attributes) {
679
+ if (data2[key] === undefined) {
680
+ Object.assign(data2, (_a = {},
681
+ _a[key] = null,
682
+ _a));
683
+ }
684
+ }
685
+ Object.assign(data2, (_b = {},
686
+ _b[Entity_1.CreateAtAttribute] = now,
687
+ _b[Entity_1.UpdateAtAttribute] = now,
688
+ _b[Entity_1.DeleteAtAttribute] = null,
689
+ _b));
690
+ };
691
+ if (data instanceof Array) {
692
+ data.forEach(function (ele) { return processSingle(ele); });
693
+ }
694
+ else {
695
+ processSingle(data);
696
+ }
697
+ };
698
+ // 对更新的数据,去掉所有的undefined属性
699
+ CascadeStore.prototype.preProcessDataUpdated = function (data) {
700
+ var undefinedKeys = Object.keys(data).filter(function (ele) { return data[ele] === undefined; });
701
+ undefinedKeys.forEach(function (ele) { return (0, lodash_1.unset)(data, ele); });
702
+ };
703
+ CascadeStore.prototype.judgeRelation = function (entity, attr) {
704
+ return (0, relation_1.judgeRelation)(this.storageSchema, entity, attr);
705
+ };
706
+ /**
707
+ * 和具体的update过程无关的例程放在这里,包括对later动作的处理、对oper的记录以及对record的收集等
708
+ * @param entity
709
+ * @param operation
710
+ * @param context
711
+ * @param option
712
+ */
713
+ CascadeStore.prototype.doUpdateSingleRowAsync = function (entity, operation, context, option) {
714
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
715
+ var data, action, operId, filter, now, _a, modiCreate, result_1, createInner, multipleCreate, data_1, data_1_1, d, createSingleOper, e_2_1, operatorId, createOper, _b, ids_1, selection, rows, modiUpsert, upsertModis, _c, originData, originId, createOper, updateAttrCount, result;
716
+ var e_2, _d, _e, _f, _g, _h, _j, _k, _l, _m;
717
+ var _this = this;
718
+ return tslib_1.__generator(this, function (_o) {
719
+ switch (_o.label) {
720
+ case 0:
721
+ data = operation.data, action = operation.action, operId = operation.id, filter = operation.filter;
722
+ now = Date.now();
723
+ _a = action;
724
+ switch (_a) {
725
+ case 'create': return [3 /*break*/, 1];
726
+ }
727
+ return [3 /*break*/, 23];
728
+ case 1:
729
+ this.preProcessDataCreated(entity, data);
730
+ if (!(option.modiParentEntity && !['modi', 'modiEntity', 'oper', 'operEntity'].includes(entity))) return [3 /*break*/, 3];
731
+ // 变成对modi的插入
732
+ (0, assert_1.default)(option.modiParentId);
733
+ modiCreate = {
734
+ id: 'dummy',
735
+ action: 'create',
736
+ data: {
737
+ id: operId,
738
+ targetEntity: entity,
739
+ action: action,
740
+ entity: option.modiParentEntity,
741
+ entityId: option.modiParentId,
742
+ filter: {
743
+ id: {
744
+ $in: [data.id], //这里记录这个filter是为了后面update的时候直接在其上面update,参见本函数后半段关于modiUpsert相关的优化
745
+ },
746
+ },
747
+ data: data,
748
+ iState: 'active',
749
+ },
750
+ };
751
+ return [4 /*yield*/, this.cascadeUpdateAsync('modi', modiCreate, context, option)];
752
+ case 2:
753
+ _o.sent();
754
+ return [2 /*return*/, 1];
755
+ case 3:
756
+ result_1 = 0;
757
+ createInner = function (operation2) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
758
+ var _a, e_3;
759
+ return tslib_1.__generator(this, function (_b) {
760
+ switch (_b.label) {
761
+ case 0:
762
+ _b.trys.push([0, 2, , 3]);
763
+ _a = result_1;
764
+ return [4 /*yield*/, this.updateAbjointRowAsync(entity, operation2, context, option)];
765
+ case 1:
766
+ result_1 = _a + _b.sent();
767
+ return [3 /*break*/, 3];
768
+ case 2:
769
+ e_3 = _b.sent();
770
+ /* 这段代码是处理插入时有重复的行,现在看有问题,等实际需求出现再写
771
+ if (e instanceof OakCongruentRowExists) {
772
+ if (option.allowExists) {
773
+ // 如果允许存在,对已存在行进行update,剩下的行继续insert
774
+ const congruentRow = e.getData() as ED[T]['OpSchema'];
775
+ if (data instanceof Array) {
776
+ const rest = data.filter(
777
+ ele => ele.id !== congruentRow.id
778
+ );
779
+ if (rest.length === data.length) {
780
+ throw e;
781
+ }
782
+ const result2 = await this.updateAbjointRow(
783
+ entity,
784
+ Object.assign({}, operation, {
785
+ data: rest,
786
+ }),
787
+ context,
788
+ option
789
+ );
790
+
791
+ const row = data.find(
792
+ ele => ele.id === congruentRow.id
793
+ );
794
+ const updateData = omit(row, ['id', '$$createAt$$']);
795
+ const result3 = await this.updateAbjointRow(
796
+ entity,
797
+ {
798
+ id: await generateNewId(),
799
+ action: 'update',
800
+ data: updateData,
801
+ filter: {
802
+ id: congruentRow.id,
803
+ } as any,
804
+ },
805
+ context,
806
+ option
807
+ );
808
+
809
+ return result2 + result3;
810
+ }
811
+ else {
812
+ if (data.id !== congruentRow.id) {
813
+ throw e;
814
+ }
815
+ const updateData = omit(data, ['id', '$$createAt$$']);
816
+ const result2 = await this.updateAbjointRow(
817
+ entity,
818
+ {
819
+ id: await generateNewId(),
820
+ action: 'update',
821
+ data: updateData,
822
+ filter: {
823
+ id: congruentRow.id,
824
+ } as any,
825
+ },
826
+ context,
827
+ option
828
+ );
829
+ return result2;
830
+ }
831
+ }
832
+ } */
833
+ throw e_3;
834
+ case 3: return [2 /*return*/];
835
+ }
836
+ });
837
+ }); };
838
+ if (!(data instanceof Array)) return [3 /*break*/, 13];
839
+ multipleCreate = this.supportMultipleCreate();
840
+ if (!multipleCreate) return [3 /*break*/, 5];
841
+ return [4 /*yield*/, createInner(operation)];
842
+ case 4:
843
+ _o.sent();
844
+ return [3 /*break*/, 12];
845
+ case 5:
846
+ _o.trys.push([5, 10, 11, 12]);
847
+ data_1 = tslib_1.__values(data), data_1_1 = data_1.next();
848
+ _o.label = 6;
849
+ case 6:
850
+ if (!!data_1_1.done) return [3 /*break*/, 9];
851
+ d = data_1_1.value;
852
+ createSingleOper = {
853
+ id: 'any',
854
+ action: 'create',
855
+ data: d,
856
+ };
857
+ return [4 /*yield*/, createInner(createSingleOper)];
858
+ case 7:
859
+ _o.sent();
860
+ _o.label = 8;
861
+ case 8:
862
+ data_1_1 = data_1.next();
863
+ return [3 /*break*/, 6];
864
+ case 9: return [3 /*break*/, 12];
865
+ case 10:
866
+ e_2_1 = _o.sent();
867
+ e_2 = { error: e_2_1 };
868
+ return [3 /*break*/, 12];
869
+ case 11:
870
+ try {
871
+ if (data_1_1 && !data_1_1.done && (_d = data_1.return)) _d.call(data_1);
872
+ }
873
+ finally { if (e_2) throw e_2.error; }
874
+ return [7 /*endfinally*/];
875
+ case 12: return [3 /*break*/, 15];
876
+ case 13: return [4 /*yield*/, createInner(operation)];
877
+ case 14:
878
+ _o.sent();
879
+ _o.label = 15;
880
+ case 15:
881
+ if (!option.dontCollect) {
882
+ context.opRecords.push({
883
+ a: 'c',
884
+ e: entity,
885
+ d: data,
886
+ });
887
+ }
888
+ if (!(!option.dontCreateOper && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity))) return [3 /*break*/, 22];
889
+ // 按照框架要求生成Oper和OperEntity这两个内置的对象
890
+ (0, assert_1.default)(operId);
891
+ return [4 /*yield*/, context.getCurrentUserId(true)];
892
+ case 16:
893
+ operatorId = _o.sent();
894
+ if (!operatorId) return [3 /*break*/, 22];
895
+ _e = {
896
+ id: 'dummy',
897
+ action: 'create'
898
+ };
899
+ _f = {
900
+ id: operId,
901
+ action: action,
902
+ data: data,
903
+ operatorId: operatorId
904
+ };
905
+ if (!(data instanceof Array)) return [3 /*break*/, 18];
906
+ _g = {
907
+ id: 'dummy',
908
+ action: 'create'
909
+ };
910
+ return [4 /*yield*/, Promise.all(data.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
911
+ var _a;
912
+ return tslib_1.__generator(this, function (_b) {
913
+ switch (_b.label) {
914
+ case 0:
915
+ _a = {};
916
+ return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
917
+ case 1: return [2 /*return*/, (_a.id = _b.sent(),
918
+ _a.entity = entity,
919
+ _a.entityId = ele.id,
920
+ _a)];
921
+ }
922
+ });
923
+ }); }))];
924
+ case 17:
925
+ _b = (_g.data = _o.sent(),
926
+ _g);
927
+ return [3 /*break*/, 20];
928
+ case 18:
929
+ _h = {
930
+ id: 'dummy',
931
+ action: 'create'
932
+ };
933
+ _j = {};
934
+ return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
935
+ case 19:
936
+ _b = [(_h.data = (_j.id = _o.sent(),
937
+ _j.entity = entity,
938
+ _j.entityId = data.id,
939
+ _j),
940
+ _h)];
941
+ _o.label = 20;
942
+ case 20:
943
+ createOper = (_e.data = (_f.operEntity$oper = _b,
944
+ _f),
945
+ _e);
946
+ return [4 /*yield*/, this.cascadeUpdateAsync('oper', createOper, context, {
947
+ dontCollect: true,
948
+ dontCreateOper: true,
949
+ })];
950
+ case 21:
951
+ _o.sent();
952
+ _o.label = 22;
953
+ case 22: return [2 /*return*/, result_1];
954
+ case 23:
955
+ ids_1 = (0, filter_2.getRelevantIds)(filter);
956
+ if (!(ids_1.length === 0)) return [3 /*break*/, 25];
957
+ selection = {
958
+ data: {
959
+ id: 1,
960
+ },
961
+ filter: operation.filter,
962
+ indexFrom: operation.indexFrom,
963
+ count: operation.count,
964
+ };
965
+ return [4 /*yield*/, this.selectAbjointRowAsync(entity, selection, context, {
966
+ dontCollect: true,
967
+ })];
968
+ case 24:
969
+ rows = _o.sent();
970
+ ids_1.push.apply(ids_1, tslib_1.__spreadArray([], tslib_1.__read((rows.map(function (ele) { return ele.id; }))), false));
971
+ _o.label = 25;
972
+ case 25:
973
+ if (data) {
974
+ this.preProcessDataUpdated(data);
975
+ }
976
+ if (!(option.modiParentEntity && !['modi', 'modiEntity'].includes(entity))) return [3 /*break*/, 31];
977
+ modiUpsert = void 0;
978
+ if (!(action !== 'remove')) return [3 /*break*/, 27];
979
+ return [4 /*yield*/, this.selectAbjointRowAsync('modi', {
980
+ data: {
981
+ id: 1,
982
+ data: 1,
983
+ },
984
+ filter: {
985
+ targetEntity: entity,
986
+ action: {
987
+ $in: ['create', 'update'],
988
+ },
989
+ entity: option.modiParentEntity,
990
+ entityId: option.modiParentId,
991
+ iState: 'active',
992
+ filter: ids_1.length > 0 ? {
993
+ id: {
994
+ $in: ids_1,
995
+ },
996
+ } : filter,
997
+ },
998
+ sorter: [
999
+ {
1000
+ $attr: {
1001
+ $$createAt$$: 1,
1002
+ },
1003
+ $direction: 'desc',
1004
+ }
1005
+ ],
1006
+ indexFrom: 0,
1007
+ count: 1,
1008
+ }, context, option)];
1009
+ case 26:
1010
+ upsertModis = _o.sent();
1011
+ if (upsertModis.length > 0) {
1012
+ _c = upsertModis[0], originData = _c.data, originId = _c.id;
1013
+ modiUpsert = {
1014
+ id: 'dummy',
1015
+ action: 'update',
1016
+ data: {
1017
+ data: Object.assign({}, originData, data),
1018
+ },
1019
+ filter: {
1020
+ id: originId,
1021
+ }
1022
+ };
1023
+ }
1024
+ _o.label = 27;
1025
+ case 27:
1026
+ if (!!modiUpsert) return [3 /*break*/, 29];
1027
+ _k = {
1028
+ id: 'dummy',
1029
+ action: 'create'
1030
+ };
1031
+ _l = {
1032
+ id: operId,
1033
+ targetEntity: entity,
1034
+ entity: option.modiParentEntity,
1035
+ entityId: option.modiParentId,
1036
+ action: action,
1037
+ data: data,
1038
+ iState: 'active',
1039
+ filter: filter
1040
+ };
1041
+ _m = {
1042
+ id: 'dummy',
1043
+ action: 'create'
1044
+ };
1045
+ return [4 /*yield*/, Promise.all(ids_1.map(function (id) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1046
+ var _a;
1047
+ return tslib_1.__generator(this, function (_b) {
1048
+ switch (_b.label) {
1049
+ case 0:
1050
+ _a = {};
1051
+ return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1052
+ case 1: return [2 /*return*/, (_a.id = _b.sent(),
1053
+ _a.entity = entity,
1054
+ _a.entityId = id,
1055
+ _a)];
1056
+ }
1057
+ });
1058
+ }); }))];
1059
+ case 28:
1060
+ modiUpsert = (_k.data = (_l.modiEntity$modi = (_m.data = _o.sent(),
1061
+ _m),
1062
+ _l),
1063
+ _k);
1064
+ _o.label = 29;
1065
+ case 29: return [4 /*yield*/, this.cascadeUpdateAsync('modi', modiUpsert, context, option)];
1066
+ case 30:
1067
+ _o.sent();
1068
+ return [2 /*return*/, 1];
1069
+ case 31:
1070
+ createOper = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1071
+ var createOper_1;
1072
+ var _a, _b, _c;
1073
+ var _this = this;
1074
+ return tslib_1.__generator(this, function (_d) {
1075
+ switch (_d.label) {
1076
+ case 0:
1077
+ if (!(!(option === null || option === void 0 ? void 0 : option.dontCreateOper) && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity) && ids_1.length > 0)) return [3 /*break*/, 3];
1078
+ // 按照框架要求生成Oper和OperEntity这两个内置的对象
1079
+ (0, assert_1.default)(operId);
1080
+ _a = {
1081
+ id: 'dummy',
1082
+ action: 'create'
1083
+ };
1084
+ _b = {
1085
+ id: operId,
1086
+ action: action,
1087
+ data: data
1088
+ };
1089
+ _c = {
1090
+ id: 'dummy',
1091
+ action: 'create'
1092
+ };
1093
+ return [4 /*yield*/, Promise.all(ids_1.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1094
+ var _a;
1095
+ return tslib_1.__generator(this, function (_b) {
1096
+ switch (_b.label) {
1097
+ case 0:
1098
+ _a = {};
1099
+ return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1100
+ case 1: return [2 /*return*/, (_a.id = _b.sent(),
1101
+ _a.entity = entity,
1102
+ _a.entityId = ele,
1103
+ _a)];
1104
+ }
1105
+ });
1106
+ }); }))];
1107
+ case 1:
1108
+ createOper_1 = (_a.data = (_b.operEntity$oper = (_c.data = _d.sent(),
1109
+ _c),
1110
+ _b),
1111
+ _a);
1112
+ return [4 /*yield*/, this.cascadeUpdateAsync('oper', createOper_1, context, {
1113
+ dontCollect: true,
1114
+ dontCreateOper: true,
1115
+ })];
1116
+ case 2:
1117
+ _d.sent();
1118
+ _d.label = 3;
1119
+ case 3: return [2 /*return*/];
1120
+ }
1121
+ });
1122
+ }); };
1123
+ if (!(action === 'remove')) return [3 /*break*/, 32];
1124
+ if (!option.dontCollect) {
1125
+ context.opRecords.push({
1126
+ a: 'r',
1127
+ e: entity,
1128
+ f: {
1129
+ id: {
1130
+ $in: ids_1,
1131
+ }
1132
+ },
1133
+ });
1134
+ }
1135
+ return [3 /*break*/, 36];
1136
+ case 32:
1137
+ updateAttrCount = Object.keys(data).length;
1138
+ if (!(updateAttrCount > 0)) return [3 /*break*/, 33];
1139
+ // 优化一下,如果不更新任何属性,则不实际执行
1140
+ Object.assign(data, {
1141
+ $$updateAt$$: now,
1142
+ });
1143
+ if (!option.dontCollect) {
1144
+ context.opRecords.push({
1145
+ a: 'u',
1146
+ e: entity,
1147
+ d: data,
1148
+ f: {
1149
+ id: {
1150
+ $in: ids_1,
1151
+ }
1152
+ },
1153
+ });
1154
+ }
1155
+ return [3 /*break*/, 36];
1156
+ case 33:
1157
+ if (!(action !== 'update')) return [3 /*break*/, 35];
1158
+ // 如果不是update动作而是用户自定义的动作,这里还是要记录oper
1159
+ return [4 /*yield*/, createOper()];
1160
+ case 34:
1161
+ // 如果不是update动作而是用户自定义的动作,这里还是要记录oper
1162
+ _o.sent();
1163
+ return [2 /*return*/, 0];
1164
+ case 35: return [2 /*return*/, 0];
1165
+ case 36: return [4 /*yield*/, this.updateAbjointRowAsync(entity, operation, context, option)];
1166
+ case 37:
1167
+ result = _o.sent();
1168
+ return [4 /*yield*/, createOper()];
1169
+ case 38:
1170
+ _o.sent();
1171
+ return [2 /*return*/, result];
1172
+ }
1173
+ });
1174
+ });
1175
+ };
1176
+ CascadeStore.prototype.doUpdateSingleRow = function (entity, operation, context, option) {
1177
+ var e_4, _a;
1178
+ var _this = this;
1179
+ var data = operation.data, action = operation.action, operId = operation.id, filter = operation.filter;
1180
+ var now = Date.now();
1181
+ switch (action) {
1182
+ case 'create': {
1183
+ this.preProcessDataCreated(entity, data);
1184
+ var result_2 = 0;
1185
+ var createInner = function (operation2) {
1186
+ try {
1187
+ result_2 += _this.updateAbjointRow(entity, operation2, context, option);
1188
+ }
1189
+ catch (e) {
1190
+ throw e;
1191
+ }
1192
+ };
1193
+ if (data instanceof Array) {
1194
+ var multipleCreate = this.supportMultipleCreate();
1195
+ if (multipleCreate) {
1196
+ createInner(operation);
1197
+ }
1198
+ else {
1199
+ try {
1200
+ for (var data_2 = tslib_1.__values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
1201
+ var d = data_2_1.value;
1202
+ var createSingleOper = {
1203
+ id: 'any',
1204
+ action: 'create',
1205
+ data: d,
1206
+ };
1207
+ createInner(createSingleOper);
1208
+ }
1209
+ }
1210
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
1211
+ finally {
1212
+ try {
1213
+ if (data_2_1 && !data_2_1.done && (_a = data_2.return)) _a.call(data_2);
1214
+ }
1215
+ finally { if (e_4) throw e_4.error; }
1216
+ }
1217
+ }
1218
+ }
1219
+ else {
1220
+ createInner(operation);
1221
+ }
1222
+ return result_2;
1223
+ }
1224
+ default: {
1225
+ if (action === 'remove') {
1226
+ }
1227
+ else {
1228
+ var updateAttrCount = Object.keys(data).length;
1229
+ if (updateAttrCount > 0) {
1230
+ // 优化一下,如果不更新任何属性,则不实际执行
1231
+ Object.assign(data, {
1232
+ $$updateAt$$: now,
1233
+ });
1234
+ this.preProcessDataUpdated(data);
1235
+ }
1236
+ else {
1237
+ return 0;
1238
+ }
1239
+ }
1240
+ return this.updateAbjointRow(entity, operation, context, option);
1241
+ }
1242
+ }
1243
+ };
1244
+ CascadeStore.prototype.cascadeUpdate = function (entity, operation, context, option) {
1245
+ var e_5, _a, e_6, _b, e_7, _c;
1246
+ var action = operation.action, data = operation.data, filter = operation.filter, id = operation.id;
1247
+ var opData;
1248
+ var wholeBeforeFns = [];
1249
+ var wholeAfterFns = [];
1250
+ var result = {};
1251
+ if (['create', 'create-l'].includes(action) && data instanceof Array) {
1252
+ opData = [];
1253
+ try {
1254
+ for (var data_3 = tslib_1.__values(data), data_3_1 = data_3.next(); !data_3_1.done; data_3_1 = data_3.next()) {
1255
+ var d = data_3_1.value;
1256
+ var _d = this.destructCascadeUpdate(entity, action, d, context, option, this.cascadeUpdate), od = _d.data, beforeFns = _d.beforeFns, afterFns = _d.afterFns;
1257
+ opData.push(od);
1258
+ wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1259
+ wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1260
+ }
1261
+ }
1262
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
1263
+ finally {
1264
+ try {
1265
+ if (data_3_1 && !data_3_1.done && (_a = data_3.return)) _a.call(data_3);
1266
+ }
1267
+ finally { if (e_5) throw e_5.error; }
1268
+ }
1269
+ }
1270
+ else {
1271
+ var _e = this.destructCascadeUpdate(entity, action, data, context, option, this.cascadeUpdate, filter), od = _e.data, beforeFns = _e.beforeFns, afterFns = _e.afterFns;
1272
+ opData = od;
1273
+ wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1274
+ wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1275
+ }
1276
+ var operation2 = Object.assign({}, operation, {
1277
+ data: opData,
1278
+ });
1279
+ try {
1280
+ for (var wholeBeforeFns_1 = tslib_1.__values(wholeBeforeFns), wholeBeforeFns_1_1 = wholeBeforeFns_1.next(); !wholeBeforeFns_1_1.done; wholeBeforeFns_1_1 = wholeBeforeFns_1.next()) {
1281
+ var before_1 = wholeBeforeFns_1_1.value;
1282
+ before_1();
1283
+ }
1284
+ }
1285
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
1286
+ finally {
1287
+ try {
1288
+ if (wholeBeforeFns_1_1 && !wholeBeforeFns_1_1.done && (_b = wholeBeforeFns_1.return)) _b.call(wholeBeforeFns_1);
1289
+ }
1290
+ finally { if (e_6) throw e_6.error; }
1291
+ }
1292
+ var count = this.doUpdateSingleRow(entity, operation2, context, option);
1293
+ try {
1294
+ for (var wholeAfterFns_1 = tslib_1.__values(wholeAfterFns), wholeAfterFns_1_1 = wholeAfterFns_1.next(); !wholeAfterFns_1_1.done; wholeAfterFns_1_1 = wholeAfterFns_1.next()) {
1295
+ var after_1 = wholeAfterFns_1_1.value;
1296
+ after_1();
1297
+ }
1298
+ }
1299
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
1300
+ finally {
1301
+ try {
1302
+ if (wholeAfterFns_1_1 && !wholeAfterFns_1_1.done && (_c = wholeAfterFns_1.return)) _c.call(wholeAfterFns_1);
1303
+ }
1304
+ finally { if (e_7) throw e_7.error; }
1305
+ }
1306
+ return result;
1307
+ };
1308
+ /**
1309
+ *
1310
+ * @param entity
1311
+ * @param operation
1312
+ * @param context
1313
+ * @param option
1314
+ */
1315
+ CascadeStore.prototype.cascadeUpdateAsync = function (entity, operation, context, option) {
1316
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
1317
+ var action, data, filter, id, opData, wholeBeforeFns, wholeAfterFns, result, data_4, data_4_1, d, _a, od, beforeFns, afterFns, _b, od, beforeFns, afterFns, operation2, wholeBeforeFns_2, wholeBeforeFns_2_1, before_2, e_8_1, count, wholeAfterFns_2, wholeAfterFns_2_1, after_2, e_9_1;
1318
+ var e_10, _c, e_8, _d, _e, _f, e_9, _g;
1319
+ return tslib_1.__generator(this, function (_h) {
1320
+ switch (_h.label) {
1321
+ case 0:
1322
+ action = operation.action, data = operation.data, filter = operation.filter, id = operation.id;
1323
+ wholeBeforeFns = [];
1324
+ wholeAfterFns = [];
1325
+ result = {};
1326
+ if (['create', 'create-l'].includes(action) && data instanceof Array) {
1327
+ opData = [];
1328
+ try {
1329
+ for (data_4 = tslib_1.__values(data), data_4_1 = data_4.next(); !data_4_1.done; data_4_1 = data_4.next()) {
1330
+ d = data_4_1.value;
1331
+ _a = this.destructCascadeUpdate(entity, action, d, context, option, this.cascadeUpdateAsync), od = _a.data, beforeFns = _a.beforeFns, afterFns = _a.afterFns;
1332
+ opData.push(od);
1333
+ wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1334
+ wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1335
+ }
1336
+ }
1337
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
1338
+ finally {
1339
+ try {
1340
+ if (data_4_1 && !data_4_1.done && (_c = data_4.return)) _c.call(data_4);
1341
+ }
1342
+ finally { if (e_10) throw e_10.error; }
1343
+ }
1344
+ }
1345
+ else {
1346
+ _b = this.destructCascadeUpdate(entity, action, data, context, option, this.cascadeUpdateAsync, filter), od = _b.data, beforeFns = _b.beforeFns, afterFns = _b.afterFns;
1347
+ opData = od;
1348
+ wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1349
+ wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1350
+ }
1351
+ operation2 = Object.assign({}, operation, {
1352
+ data: opData,
1353
+ });
1354
+ _h.label = 1;
1355
+ case 1:
1356
+ _h.trys.push([1, 6, 7, 8]);
1357
+ wholeBeforeFns_2 = tslib_1.__values(wholeBeforeFns), wholeBeforeFns_2_1 = wholeBeforeFns_2.next();
1358
+ _h.label = 2;
1359
+ case 2:
1360
+ if (!!wholeBeforeFns_2_1.done) return [3 /*break*/, 5];
1361
+ before_2 = wholeBeforeFns_2_1.value;
1362
+ return [4 /*yield*/, before_2()];
1363
+ case 3:
1364
+ _h.sent();
1365
+ _h.label = 4;
1366
+ case 4:
1367
+ wholeBeforeFns_2_1 = wholeBeforeFns_2.next();
1368
+ return [3 /*break*/, 2];
1369
+ case 5: return [3 /*break*/, 8];
1370
+ case 6:
1371
+ e_8_1 = _h.sent();
1372
+ e_8 = { error: e_8_1 };
1373
+ return [3 /*break*/, 8];
1374
+ case 7:
1375
+ try {
1376
+ if (wholeBeforeFns_2_1 && !wholeBeforeFns_2_1.done && (_d = wholeBeforeFns_2.return)) _d.call(wholeBeforeFns_2);
1377
+ }
1378
+ finally { if (e_8) throw e_8.error; }
1379
+ return [7 /*endfinally*/];
1380
+ case 8: return [4 /*yield*/, this.doUpdateSingleRowAsync(entity, operation2, context, option)];
1381
+ case 9:
1382
+ count = _h.sent();
1383
+ this.mergeOperationResult(result, (_e = {},
1384
+ _e[entity] = (_f = {},
1385
+ _f[operation2.action] = count,
1386
+ _f),
1387
+ _e));
1388
+ _h.label = 10;
1389
+ case 10:
1390
+ _h.trys.push([10, 15, 16, 17]);
1391
+ wholeAfterFns_2 = tslib_1.__values(wholeAfterFns), wholeAfterFns_2_1 = wholeAfterFns_2.next();
1392
+ _h.label = 11;
1393
+ case 11:
1394
+ if (!!wholeAfterFns_2_1.done) return [3 /*break*/, 14];
1395
+ after_2 = wholeAfterFns_2_1.value;
1396
+ return [4 /*yield*/, after_2()];
1397
+ case 12:
1398
+ _h.sent();
1399
+ _h.label = 13;
1400
+ case 13:
1401
+ wholeAfterFns_2_1 = wholeAfterFns_2.next();
1402
+ return [3 /*break*/, 11];
1403
+ case 14: return [3 /*break*/, 17];
1404
+ case 15:
1405
+ e_9_1 = _h.sent();
1406
+ e_9 = { error: e_9_1 };
1407
+ return [3 /*break*/, 17];
1408
+ case 16:
1409
+ try {
1410
+ if (wholeAfterFns_2_1 && !wholeAfterFns_2_1.done && (_g = wholeAfterFns_2.return)) _g.call(wholeAfterFns_2);
1411
+ }
1412
+ finally { if (e_9) throw e_9.error; }
1413
+ return [7 /*endfinally*/];
1414
+ case 17: return [2 /*return*/, result];
1415
+ }
1416
+ });
1417
+ });
1418
+ };
1419
+ CascadeStore.prototype.cascadeSelect = function (entity, selection, context, option) {
1420
+ var data = selection.data, filter = selection.filter, indexFrom = selection.indexFrom, count = selection.count, sorter = selection.sorter;
1421
+ var _a = this.destructCascadeSelect(entity, data, context, this.cascadeSelect, this.aggregateSync, option), projection = _a.projection, cascadeSelectionFns = _a.cascadeSelectionFns;
1422
+ var rows = this.selectAbjointRow(entity, {
1423
+ data: projection,
1424
+ filter: filter,
1425
+ indexFrom: indexFrom,
1426
+ count: count,
1427
+ sorter: sorter
1428
+ }, context, option);
1429
+ if (cascadeSelectionFns.length > 0) {
1430
+ var ruException_1 = [];
1431
+ cascadeSelectionFns.forEach(function (ele) {
1432
+ try {
1433
+ ele(rows);
1434
+ }
1435
+ catch (e) {
1436
+ if (e instanceof types_1.OakRowUnexistedException) {
1437
+ var rows_1 = e.getRows();
1438
+ ruException_1.push.apply(ruException_1, tslib_1.__spreadArray([], tslib_1.__read(rows_1), false));
1439
+ }
1440
+ else {
1441
+ throw e;
1442
+ }
1443
+ }
1444
+ });
1445
+ if (ruException_1.length > 0) {
1446
+ throw new types_1.OakRowUnexistedException(ruException_1);
1447
+ }
1448
+ }
1449
+ return rows;
1450
+ };
1451
+ /**
1452
+ * 将一次查询的结果集加入result
1453
+ * todo 如果是supportMtoOJoin,这里还要解构(未充分测试)
1454
+ * @param entity
1455
+ * @param rows
1456
+ * @param context
1457
+ */
1458
+ CascadeStore.prototype.addToResultSelections = function (entity, rows, context) {
1459
+ if (this.supportManyToOneJoin()) {
1460
+ var attrsToPick_1 = [];
1461
+ var _loop_3 = function (attr) {
1462
+ var data = {};
1463
+ var rel = this_3.judgeRelation(entity, attr);
1464
+ if (rel === 2) {
1465
+ this_3.addToResultSelections(attr, rows.map(function (ele) { return ele[attr]; }).filter(function (ele) { return !!ele; }), context);
1466
+ }
1467
+ else if (typeof rel === 'string') {
1468
+ this_3.addToResultSelections(rel, rows.map(function (ele) { return ele[attr]; }).filter(function (ele) { return !!ele; }), context);
1469
+ }
1470
+ else if (rel instanceof Array) {
1471
+ this_3.addToResultSelections(rel[0], rows.map(function (ele) { return ele[attr]; }).reduce(function (prev, current) { return prev.concat(current); }, []), context);
1472
+ }
1473
+ else {
1474
+ attrsToPick_1.push(attr);
1475
+ }
1476
+ };
1477
+ var this_3 = this;
1478
+ for (var attr in rows[0]) {
1479
+ _loop_3(attr);
1480
+ }
1481
+ var originRows = rows.map(function (ele) { return (0, lodash_1.pick)(ele, attrsToPick_1); });
1482
+ this.addSingleRowToResultSelections(entity, originRows, context);
1483
+ }
1484
+ else {
1485
+ this.addSingleRowToResultSelections(entity, rows, context);
1486
+ }
1487
+ };
1488
+ CascadeStore.prototype.addSingleRowToResultSelections = function (entity, rows, context) {
1489
+ var _a;
1490
+ var opRecords = context.opRecords;
1491
+ var lastOperation = opRecords[opRecords.length - 1];
1492
+ if (lastOperation && lastOperation.a === 's') {
1493
+ var entityBranch_1 = lastOperation.d[entity];
1494
+ if (entityBranch_1) {
1495
+ rows.forEach(function (row) {
1496
+ var _a;
1497
+ if (row) {
1498
+ (0, assert_1.default)(row.id);
1499
+ var id = row.id;
1500
+ if (!entityBranch_1[id]) {
1501
+ Object.assign(entityBranch_1, (_a = {},
1502
+ _a[id] = row,
1503
+ _a));
1504
+ }
1505
+ else {
1506
+ Object.assign(entityBranch_1[id], row);
1507
+ }
1508
+ }
1509
+ });
1510
+ return;
1511
+ }
1512
+ }
1513
+ else {
1514
+ lastOperation = {
1515
+ a: 's',
1516
+ d: {},
1517
+ };
1518
+ opRecords.push(lastOperation);
1519
+ }
1520
+ var entityBranch = {};
1521
+ rows.forEach(function (row) {
1522
+ var _a;
1523
+ if (row) {
1524
+ var id = row.id;
1525
+ Object.assign(entityBranch, (_a = {},
1526
+ _a[id] = row,
1527
+ _a));
1528
+ }
1529
+ });
1530
+ Object.assign(lastOperation.d, (_a = {},
1531
+ _a[entity] = entityBranch,
1532
+ _a));
1533
+ };
1534
+ CascadeStore.prototype.cascadeSelectAsync = function (entity, selection, context, option) {
1535
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
1536
+ var data, filter, indexFrom, count, sorter, _a, projection, cascadeSelectionFns, rows, ruException_2;
1537
+ var _this = this;
1538
+ return tslib_1.__generator(this, function (_b) {
1539
+ switch (_b.label) {
1540
+ case 0:
1541
+ data = selection.data, filter = selection.filter, indexFrom = selection.indexFrom, count = selection.count, sorter = selection.sorter;
1542
+ _a = this.destructCascadeSelect(entity, data, context, this.cascadeSelectAsync, this.aggregateAsync, option), projection = _a.projection, cascadeSelectionFns = _a.cascadeSelectionFns;
1543
+ return [4 /*yield*/, this.selectAbjointRowAsync(entity, {
1544
+ data: projection,
1545
+ filter: filter,
1546
+ indexFrom: indexFrom,
1547
+ count: count,
1548
+ sorter: sorter
1549
+ }, context, option)];
1550
+ case 1:
1551
+ rows = _b.sent();
1552
+ if (!option.dontCollect) {
1553
+ this.addToResultSelections(entity, rows, context);
1554
+ }
1555
+ if (!(cascadeSelectionFns.length > 0)) return [3 /*break*/, 3];
1556
+ ruException_2 = [];
1557
+ return [4 /*yield*/, Promise.all(cascadeSelectionFns.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1558
+ var e_11, rows_2;
1559
+ return tslib_1.__generator(this, function (_a) {
1560
+ switch (_a.label) {
1561
+ case 0:
1562
+ _a.trys.push([0, 2, , 3]);
1563
+ return [4 /*yield*/, ele(rows)];
1564
+ case 1:
1565
+ _a.sent();
1566
+ return [3 /*break*/, 3];
1567
+ case 2:
1568
+ e_11 = _a.sent();
1569
+ if (e_11 instanceof types_1.OakRowUnexistedException) {
1570
+ rows_2 = e_11.getRows();
1571
+ ruException_2.push.apply(ruException_2, tslib_1.__spreadArray([], tslib_1.__read(rows_2), false));
1572
+ }
1573
+ else {
1574
+ throw e_11;
1575
+ }
1576
+ return [3 /*break*/, 3];
1577
+ case 3: return [2 /*return*/];
1578
+ }
1579
+ });
1580
+ }); }))];
1581
+ case 2:
1582
+ _b.sent();
1583
+ if (ruException_2.length > 0) {
1584
+ throw new types_1.OakRowUnexistedException(ruException_2);
1585
+ }
1586
+ _b.label = 3;
1587
+ case 3: return [2 /*return*/, rows];
1588
+ }
1589
+ });
1590
+ });
1591
+ };
1592
+ return CascadeStore;
1593
+ }(RowStore_1.RowStore));
1594
+ exports.CascadeStore = CascadeStore;