oak-domain 2.0.2 → 2.0.3

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