oak-domain 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) hide show
  1. package/lib/actions/action.d.ts +16 -16
  2. package/lib/actions/action.js +17 -17
  3. package/lib/actions/relation.d.ts +5 -5
  4. package/lib/actions/relation.js +38 -38
  5. package/lib/base-app-domain/ActionAuth/Schema.d.ts +160 -160
  6. package/lib/base-app-domain/ActionAuth/Schema.js +2 -2
  7. package/lib/base-app-domain/ActionAuth/Storage.d.ts +3 -3
  8. package/lib/base-app-domain/ActionAuth/Storage.js +42 -42
  9. package/lib/base-app-domain/ActionDefDict.d.ts +9 -9
  10. package/lib/base-app-domain/ActionDefDict.js +11 -11
  11. package/lib/base-app-domain/EntityDict.d.ts +24 -24
  12. package/lib/base-app-domain/EntityDict.js +2 -2
  13. package/lib/base-app-domain/I18n/Schema.d.ts +129 -129
  14. package/lib/base-app-domain/I18n/Schema.js +2 -2
  15. package/lib/base-app-domain/I18n/Storage.d.ts +3 -3
  16. package/lib/base-app-domain/I18n/Storage.js +59 -59
  17. package/lib/base-app-domain/Modi/Action.d.ts +10 -10
  18. package/lib/base-app-domain/Modi/Action.js +14 -14
  19. package/lib/base-app-domain/Modi/Schema.d.ts +136 -136
  20. package/lib/base-app-domain/Modi/Schema.js +2 -2
  21. package/lib/base-app-domain/Modi/Storage.d.ts +3 -3
  22. package/lib/base-app-domain/Modi/Storage.js +63 -63
  23. package/lib/base-app-domain/ModiEntity/Schema.d.ts +311 -311
  24. package/lib/base-app-domain/ModiEntity/Schema.js +2 -2
  25. package/lib/base-app-domain/ModiEntity/Storage.d.ts +3 -3
  26. package/lib/base-app-domain/ModiEntity/Storage.js +30 -30
  27. package/lib/base-app-domain/Oper/Schema.d.ts +152 -152
  28. package/lib/base-app-domain/Oper/Schema.js +2 -2
  29. package/lib/base-app-domain/Oper/Storage.d.ts +3 -3
  30. package/lib/base-app-domain/Oper/Storage.js +38 -38
  31. package/lib/base-app-domain/OperEntity/Schema.d.ts +300 -300
  32. package/lib/base-app-domain/OperEntity/Schema.js +2 -2
  33. package/lib/base-app-domain/OperEntity/Storage.d.ts +3 -3
  34. package/lib/base-app-domain/OperEntity/Storage.js +30 -30
  35. package/lib/base-app-domain/Relation/Schema.d.ts +188 -188
  36. package/lib/base-app-domain/Relation/Schema.js +2 -2
  37. package/lib/base-app-domain/Relation/Storage.d.ts +3 -3
  38. package/lib/base-app-domain/Relation/Storage.js +54 -54
  39. package/lib/base-app-domain/Relation.d.ts +10 -10
  40. package/lib/base-app-domain/Relation.js +10 -10
  41. package/lib/base-app-domain/RelationAuth/Schema.d.ts +180 -180
  42. package/lib/base-app-domain/RelationAuth/Schema.js +2 -2
  43. package/lib/base-app-domain/RelationAuth/Storage.d.ts +3 -3
  44. package/lib/base-app-domain/RelationAuth/Storage.js +46 -46
  45. package/lib/base-app-domain/Storage.d.ts +3 -3
  46. package/lib/base-app-domain/Storage.js +27 -27
  47. package/lib/base-app-domain/User/Action.d.ts +10 -10
  48. package/lib/base-app-domain/User/Action.js +12 -12
  49. package/lib/base-app-domain/User/Schema.d.ts +197 -197
  50. package/lib/base-app-domain/User/Schema.js +2 -2
  51. package/lib/base-app-domain/User/Storage.d.ts +3 -3
  52. package/lib/base-app-domain/User/Storage.js +33 -33
  53. package/lib/base-app-domain/UserEntityGrant/Action.d.ts +5 -5
  54. package/lib/base-app-domain/UserEntityGrant/Action.js +5 -5
  55. package/lib/base-app-domain/UserEntityGrant/Schema.d.ts +157 -157
  56. package/lib/base-app-domain/UserEntityGrant/Schema.js +2 -2
  57. package/lib/base-app-domain/UserEntityGrant/Storage.d.ts +3 -3
  58. package/lib/base-app-domain/UserEntityGrant/Storage.js +29 -29
  59. package/lib/base-app-domain/UserRelation/Schema.d.ts +194 -194
  60. package/lib/base-app-domain/UserRelation/Schema.js +2 -2
  61. package/lib/base-app-domain/UserRelation/Storage.d.ts +3 -3
  62. package/lib/base-app-domain/UserRelation/Storage.js +56 -56
  63. package/lib/base-app-domain/_SubQuery.d.ts +112 -112
  64. package/lib/base-app-domain/_SubQuery.js +2 -2
  65. package/lib/base-app-domain/index.d.ts +4 -4
  66. package/lib/base-app-domain/index.js +7 -7
  67. package/lib/checkers/index.d.ts +5 -5
  68. package/lib/checkers/index.js +14 -14
  69. package/lib/compiler/env.d.ts +14 -14
  70. package/lib/compiler/env.js +50 -50
  71. package/lib/compiler/localeBuilder.d.ts +22 -22
  72. package/lib/compiler/localeBuilder.js +169 -169
  73. package/lib/compiler/schemalBuilder.d.ts +11 -11
  74. package/lib/compiler/schemalBuilder.js +3787 -3787
  75. package/lib/compiler/uiBuilder.d.ts +1 -1
  76. package/lib/compiler/uiBuilder.js +3 -3
  77. package/lib/entities/ActionAuth.d.ts +12 -12
  78. package/lib/entities/ActionAuth.js +29 -29
  79. package/lib/entities/I18n.d.ts +9 -9
  80. package/lib/entities/I18n.js +37 -37
  81. package/lib/entities/Modi.d.ts +11 -11
  82. package/lib/entities/Modi.js +49 -49
  83. package/lib/entities/ModiEntity.d.ts +8 -8
  84. package/lib/entities/ModiEntity.js +18 -18
  85. package/lib/entities/Oper.d.ts +11 -11
  86. package/lib/entities/Oper.js +21 -21
  87. package/lib/entities/OperEntity.d.ts +8 -8
  88. package/lib/entities/OperEntity.js +18 -18
  89. package/lib/entities/Relation.d.ts +8 -8
  90. package/lib/entities/Relation.js +35 -35
  91. package/lib/entities/RelationAuth.d.ts +8 -8
  92. package/lib/entities/RelationAuth.js +34 -34
  93. package/lib/entities/User.d.ts +8 -8
  94. package/lib/entities/User.js +31 -31
  95. package/lib/entities/UserEntityGrant.d.ts +8 -8
  96. package/lib/entities/UserEntityGrant.js +18 -18
  97. package/lib/entities/UserRelation.d.ts +10 -10
  98. package/lib/entities/UserRelation.js +38 -38
  99. package/lib/store/AsyncRowStore.d.ts +58 -58
  100. package/lib/store/AsyncRowStore.js +190 -190
  101. package/lib/store/CascadeStore.d.ts +103 -103
  102. package/lib/store/CascadeStore.js +2035 -2035
  103. package/lib/store/RelationAuth.d.ts +103 -103
  104. package/lib/store/RelationAuth.js +1492 -1492
  105. package/lib/store/SyncRowStore.d.ts +29 -29
  106. package/lib/store/SyncRowStore.js +49 -48
  107. package/lib/store/TriggerExecutor.d.ts +31 -31
  108. package/lib/store/TriggerExecutor.js +532 -532
  109. package/lib/store/actionAuth.d.ts +4 -4
  110. package/lib/store/actionAuth.js +40 -40
  111. package/lib/store/actionDef.d.ts +10 -10
  112. package/lib/store/actionDef.js +351 -351
  113. package/lib/store/checker.d.ts +26 -26
  114. package/lib/store/checker.js +623 -623
  115. package/lib/store/filter.d.ts +123 -123
  116. package/lib/store/filter.js +1761 -1761
  117. package/lib/store/modi.d.ts +13 -13
  118. package/lib/store/modi.js +315 -315
  119. package/lib/store/relation.d.ts +12 -12
  120. package/lib/store/relation.js +67 -67
  121. package/lib/timers/oper.d.ts +18 -18
  122. package/lib/timers/oper.js +60 -60
  123. package/lib/timers/vaccum.d.ts +20 -20
  124. package/lib/timers/vaccum.js +176 -176
  125. package/lib/triggers/index.d.ts +5 -5
  126. package/lib/triggers/index.js +8 -8
  127. package/lib/types/Action.d.ts +20 -20
  128. package/lib/types/Action.js +2 -2
  129. package/lib/types/AppLoader.d.ts +11 -11
  130. package/lib/types/AppLoader.js +10 -10
  131. package/lib/types/Aspect.d.ts +13 -13
  132. package/lib/types/Aspect.js +4 -4
  133. package/lib/types/Auth.d.ts +74 -74
  134. package/lib/types/Auth.js +2 -2
  135. package/lib/types/Connector.d.ts +33 -33
  136. package/lib/types/Connector.js +9 -9
  137. package/lib/types/Context.d.ts +7 -7
  138. package/lib/types/Context.js +3 -3
  139. package/lib/types/DataType.d.ts +24 -24
  140. package/lib/types/DataType.js +6 -6
  141. package/lib/types/Demand.d.ts +88 -88
  142. package/lib/types/Demand.js +10 -10
  143. package/lib/types/Endpoint.d.ts +11 -11
  144. package/lib/types/Endpoint.js +3 -3
  145. package/lib/types/Entity.d.ts +193 -193
  146. package/lib/types/Entity.js +14 -14
  147. package/lib/types/EntityDesc.d.ts +8 -8
  148. package/lib/types/EntityDesc.js +2 -2
  149. package/lib/types/Environment.d.ts +57 -57
  150. package/lib/types/Environment.js +2 -2
  151. package/lib/types/Exception.d.ts +115 -115
  152. package/lib/types/Exception.js +389 -389
  153. package/lib/types/Expression.d.ts +163 -163
  154. package/lib/types/Expression.js +427 -427
  155. package/lib/types/Geo.d.ts +18 -18
  156. package/lib/types/Geo.js +2 -2
  157. package/lib/types/Locale.d.ts +25 -25
  158. package/lib/types/Locale.js +2 -2
  159. package/lib/types/Logger.d.ts +6 -6
  160. package/lib/types/Logger.js +3 -3
  161. package/lib/types/Polyfill.d.ts +24 -24
  162. package/lib/types/Polyfill.js +2 -2
  163. package/lib/types/Port.d.ts +18 -18
  164. package/lib/types/Port.js +2 -2
  165. package/lib/types/RowStore.d.ts +18 -18
  166. package/lib/types/RowStore.js +34 -34
  167. package/lib/types/Storage.d.ts +57 -57
  168. package/lib/types/Storage.js +2 -2
  169. package/lib/types/Style.d.ts +11 -11
  170. package/lib/types/Style.js +2 -2
  171. package/lib/types/Timer.d.ts +14 -14
  172. package/lib/types/Timer.js +2 -2
  173. package/lib/types/Trigger.d.ts +108 -108
  174. package/lib/types/Trigger.js +54 -54
  175. package/lib/types/Txn.d.ts +2 -2
  176. package/lib/types/Txn.js +3 -3
  177. package/lib/types/Watcher.d.ts +19 -19
  178. package/lib/types/Watcher.js +4 -4
  179. package/lib/types/index.d.ts +24 -24
  180. package/lib/types/index.js +27 -27
  181. package/lib/types/schema/DataTypes.d.ts +34 -34
  182. package/lib/types/schema/DataTypes.js +3 -3
  183. package/lib/utils/SimpleConnector.d.ts +48 -48
  184. package/lib/utils/SimpleConnector.js +180 -180
  185. package/lib/utils/assert.d.ts +5 -5
  186. package/lib/utils/assert.js +9 -9
  187. package/lib/utils/concurrent.d.ts +15 -15
  188. package/lib/utils/concurrent.js +89 -89
  189. package/lib/utils/date.d.ts +1 -1
  190. package/lib/utils/date.js +18 -18
  191. package/lib/utils/geo.d.ts +4 -4
  192. package/lib/utils/geo.js +24 -24
  193. package/lib/utils/lodash.d.ts +20 -20
  194. package/lib/utils/lodash.js +55 -55
  195. package/lib/utils/mask.d.ts +5 -5
  196. package/lib/utils/mask.js +35 -35
  197. package/lib/utils/money.d.ts +6 -6
  198. package/lib/utils/money.js +40 -40
  199. package/lib/utils/random/random.d.ts +1 -1
  200. package/lib/utils/random/random.js +24 -24
  201. package/lib/utils/random/random.mp.d.ts +1 -1
  202. package/lib/utils/random/random.mp.js +25 -25
  203. package/lib/utils/random/random.web.d.ts +1 -1
  204. package/lib/utils/random/random.web.js +17 -17
  205. package/lib/utils/string.d.ts +28 -28
  206. package/lib/utils/string.js +82 -82
  207. package/lib/utils/url.d.ts +1 -1
  208. package/lib/utils/url.js +12 -12
  209. package/lib/utils/uuid.d.ts +17 -17
  210. package/lib/utils/uuid.js +257 -257
  211. package/lib/utils/validator.d.ts +25 -25
  212. package/lib/utils/validator.js +128 -128
  213. package/lib/utils/version.d.ts +7 -7
  214. package/lib/utils/version.js +21 -21
  215. package/package.json +49 -49
  216. package/src/entities/ActionAuth.ts +41 -41
  217. package/src/entities/I18n.ts +46 -46
  218. package/src/entities/Modi.ts +69 -69
  219. package/src/entities/ModiEntity.ts +26 -26
  220. package/src/entities/Oper.ts +32 -32
  221. package/src/entities/OperEntity.ts +27 -27
  222. package/src/entities/Relation.ts +43 -43
  223. package/src/entities/RelationAuth.ts +43 -43
  224. package/src/entities/User.ts +48 -48
  225. package/src/entities/UserEntityGrant.ts +27 -27
  226. package/src/entities/UserRelation.ts +50 -50
@@ -1,2035 +1,2035 @@
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 AsyncRowStore_1 = require("./AsyncRowStore");
13
- var filter_2 = require("./filter");
14
- var uuid_1 = require("../utils/uuid");
15
- var env_1 = require("../compiler/env");
16
- /**这个用来处理级联的select和update,对不同能力的 */
17
- var CascadeStore = /** @class */ (function (_super) {
18
- tslib_1.__extends(CascadeStore, _super);
19
- function CascadeStore(storageSchema) {
20
- var _this = _super.call(this, storageSchema) || this;
21
- _this.selectionRewriters = [];
22
- _this.operationRewriters = [];
23
- return _this;
24
- }
25
- CascadeStore.prototype.reinforceSelectionAsync = function (entity, selection, context, option) {
26
- return tslib_1.__awaiter(this, void 0, void 0, function () {
27
- var noRelationDestEntities, rewriterPromises;
28
- var _this = this;
29
- return tslib_1.__generator(this, function (_a) {
30
- switch (_a.label) {
31
- case 0:
32
- noRelationDestEntities = [];
33
- this.reinforceSelectionInner(entity, selection, context, noRelationDestEntities);
34
- rewriterPromises = this.selectionRewriters.map(function (ele) { return ele(_this.getSchema(), entity, selection, context); });
35
- if (!(rewriterPromises.length > 0)) return [3 /*break*/, 2];
36
- return [4 /*yield*/, Promise.all(rewriterPromises)];
37
- case 1:
38
- _a.sent();
39
- _a.label = 2;
40
- case 2: return [2 /*return*/];
41
- }
42
- });
43
- });
44
- };
45
- CascadeStore.prototype.reinforceSelectionSync = function (entity, selection, context, option) {
46
- var _this = this;
47
- this.reinforceSelectionInner(entity, selection, context, []);
48
- var rewriterPromises = this.selectionRewriters.map(function (ele) { return ele(_this.getSchema(), entity, selection, context); });
49
- };
50
- CascadeStore.prototype.reinforceSelectionInner = function (entity, selection, context, noRelationDestEntities) {
51
- var _this = this;
52
- var filter = selection.filter, data = selection.data, sorter = selection.sorter;
53
- var checkNode = function (projectionNode, attrs) {
54
- attrs.forEach(function (attr) {
55
- var _a;
56
- if (!projectionNode.hasOwnProperty(attr)) {
57
- Object.assign(projectionNode, (_a = {},
58
- _a[attr] = 1,
59
- _a));
60
- }
61
- });
62
- };
63
- var relevantIds = [];
64
- if (filter) {
65
- var toBeAssignNode_1 = {}; // 用来记录在表达式中涉及到的结点
66
- // filter当中所关联到的属性必须在projection中
67
- var filterNodeDict_1 = {};
68
- var checkFilterNode_1 = function (entity2, filterNode, projectionNode) {
69
- var _a, e_1, _b, _c, _d, _e, _f;
70
- var necessaryAttrs = ['id'];
71
- for (var attr in filterNode) {
72
- if (attr === '#id') {
73
- (0, assert_1.default)(!filterNodeDict_1[filterNode[attr]], "projection\u4E2D\u7ED3\u70B9\u7684id\u6709\u91CD\u590D, ".concat(filterNode[attr]));
74
- Object.assign(filterNodeDict_1, (_a = {},
75
- _a[filterNode[attr]] = projectionNode,
76
- _a));
77
- if (toBeAssignNode_1[filterNode[attr]]) {
78
- checkNode(projectionNode, toBeAssignNode_1[filterNode[attr]]);
79
- }
80
- }
81
- else if (['$and', '$or'].includes(attr)) {
82
- try {
83
- for (var _g = (e_1 = void 0, tslib_1.__values(filterNode[attr])), _h = _g.next(); !_h.done; _h = _g.next()) {
84
- var node = _h.value;
85
- checkFilterNode_1(entity2, node, projectionNode);
86
- }
87
- }
88
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
89
- finally {
90
- try {
91
- if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
92
- }
93
- finally { if (e_1) throw e_1.error; }
94
- }
95
- }
96
- else if (attr === '$not') {
97
- checkFilterNode_1(entity2, filterNode[attr], projectionNode);
98
- }
99
- else if (attr === '$text') {
100
- // 全文检索首先要有fulltext索引,其次要把fulltext的相关属性加到projection里
101
- var indexes = _this.getSchema()[entity2].indexes;
102
- var fulltextIndex = indexes.find(function (ele) { return ele.config && ele.config.type === 'fulltext'; });
103
- var attributes = fulltextIndex.attributes;
104
- necessaryAttrs.push.apply(necessaryAttrs, tslib_1.__spreadArray([], tslib_1.__read((attributes.map(function (ele) { return ele.name; }))), false));
105
- }
106
- else {
107
- if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
108
- var exprResult = (0, types_1.getAttrRefInExpression)(filterNode[attr]);
109
- for (var nodeName in exprResult) {
110
- if (nodeName === '#current') {
111
- checkNode(projectionNode, exprResult[nodeName]);
112
- }
113
- else if (filterNodeDict_1[nodeName]) {
114
- checkNode(filterNodeDict_1[nodeName], exprResult[nodeName]);
115
- }
116
- else {
117
- if (toBeAssignNode_1[nodeName]) {
118
- (_c = toBeAssignNode_1[nodeName]).push.apply(_c, tslib_1.__spreadArray([], tslib_1.__read(exprResult[nodeName]), false));
119
- }
120
- else {
121
- Object.assign(toBeAssignNode_1, (_d = {},
122
- _d[nodeName] = exprResult[nodeName],
123
- _d));
124
- }
125
- }
126
- }
127
- }
128
- else {
129
- var rel = _this.judgeRelation(entity2, attr);
130
- if (rel === 1) {
131
- necessaryAttrs.push(attr);
132
- }
133
- else if (rel === 2) {
134
- // entity/entityId反指
135
- necessaryAttrs.push('entity', 'entityId');
136
- if (!projectionNode[attr]) {
137
- Object.assign(projectionNode, (_e = {},
138
- _e[attr] = {
139
- id: 1,
140
- },
141
- _e));
142
- }
143
- checkFilterNode_1(attr, filterNode[attr], projectionNode[attr]);
144
- }
145
- else if (typeof rel === 'string') {
146
- necessaryAttrs.push("".concat(attr, "Id"));
147
- if (!projectionNode[attr]) {
148
- Object.assign(projectionNode, (_f = {},
149
- _f[attr] = {
150
- id: 1,
151
- },
152
- _f));
153
- }
154
- checkFilterNode_1(rel, filterNode[attr], projectionNode[attr]);
155
- }
156
- else if (rel instanceof Array) {
157
- // 现在filter中还不支持一对多的语义,先放着吧
158
- }
159
- }
160
- }
161
- checkNode(projectionNode, necessaryAttrs);
162
- }
163
- };
164
- checkFilterNode_1(entity, filter, data);
165
- relevantIds = (0, filter_2.getRelevantIds)(filter);
166
- }
167
- // sorter感觉现在取不取影响不大,前端的list直接获取返回的ids了,先不管之
168
- if (sorter) {
169
- }
170
- var toBeAssignNode2 = {}; // 用来记录在表达式中涉及到的结点
171
- var projectionNodeDict = {};
172
- var checkProjectionNode = function (entity2, projectionNode) {
173
- var _a, _b, _c;
174
- var necessaryAttrs = ['id', '$$createAt$$', '$$updateAt$$']; // 有的页面依赖于其它页面取数据,有时两个页面的filter的差异会导致有一个加createAt,有一个不加,此时可能产生前台取数据不完整的异常。先统一加上
175
- for (var attr in projectionNode) {
176
- if (attr === '#id') {
177
- (0, assert_1.default)(!projectionNodeDict[projectionNode[attr]], "projection\u4E2D\u7ED3\u70B9\u7684id\u6709\u91CD\u590D, ".concat(projectionNode[attr]));
178
- Object.assign(projectionNodeDict, (_a = {},
179
- _a[projectionNode[attr]] = projectionNode,
180
- _a));
181
- if (toBeAssignNode2[projectionNode[attr]]) {
182
- checkNode(projectionNode, toBeAssignNode2[projectionNode[attr]]);
183
- }
184
- }
185
- else {
186
- if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
187
- var exprResult = (0, types_1.getAttrRefInExpression)(projectionNode[attr]);
188
- for (var nodeName in exprResult) {
189
- if (nodeName === '#current') {
190
- checkNode(projectionNode, exprResult[nodeName]);
191
- }
192
- else if (projectionNodeDict[nodeName]) {
193
- checkNode(projectionNodeDict[nodeName], exprResult[nodeName]);
194
- }
195
- else {
196
- if (toBeAssignNode2[nodeName]) {
197
- (_b = toBeAssignNode2[nodeName]).push.apply(_b, tslib_1.__spreadArray([], tslib_1.__read(exprResult[nodeName]), false));
198
- }
199
- else {
200
- Object.assign(toBeAssignNode2, (_c = {},
201
- _c[nodeName] = exprResult[nodeName],
202
- _c));
203
- }
204
- }
205
- }
206
- }
207
- else {
208
- var rel = (0, relation_1.judgeRelation)(_this.getSchema(), entity2, attr);
209
- if (rel === 1) {
210
- necessaryAttrs.push(attr);
211
- }
212
- else if (rel === 2) {
213
- // entity/entityId反指
214
- necessaryAttrs.push('entity', 'entityId');
215
- checkProjectionNode(attr, projectionNode[attr]);
216
- }
217
- else if (typeof rel === 'string') {
218
- necessaryAttrs.push("".concat(attr, "Id"));
219
- checkProjectionNode(rel, projectionNode[attr]);
220
- }
221
- else if (rel instanceof Array && !attr.endsWith('$$aggr')) {
222
- var data_1 = projectionNode[attr].data;
223
- if (rel[1]) {
224
- checkNode(data_1, [rel[1]]);
225
- }
226
- else {
227
- checkNode(data_1, ['entity', 'entityId']);
228
- }
229
- _this.reinforceSelectionInner(rel[0], projectionNode[attr], context, noRelationDestEntities);
230
- }
231
- }
232
- }
233
- checkNode(projectionNode, necessaryAttrs);
234
- }
235
- // 如果对象中指向一对多的Modi,此时加上指向Modi的projection
236
- if (_this.getSchema()[entity2].toModi) {
237
- Object.assign(projectionNode, {
238
- modi$entity: {
239
- $entity: 'modi',
240
- data: {
241
- id: 1,
242
- targetEntity: 1,
243
- entity: 1,
244
- entityId: 1,
245
- action: 1,
246
- iState: 1,
247
- data: 1,
248
- filter: 1,
249
- },
250
- filter: {
251
- iState: 'active',
252
- },
253
- }
254
- });
255
- }
256
- // 如果对象上有relation关系,在此将本用户相关的relation和actionAuth全部取出
257
- // 还要将actionAuth上没有relation关系但destEntity为本对象的行也全部取出,这些是指向userId的可能路径
258
- // 放在这里有点怪异,暂先这样
259
- if (context instanceof AsyncRowStore_1.AsyncContext) {
260
- var userId = context.getCurrentUserId(true);
261
- if (userId && !env_1.SYSTEM_RESERVE_ENTITIES.includes(entity2)) {
262
- if (_this.getSchema()[entity2].relation && !projectionNode.userRelation$entity) {
263
- Object.assign(projectionNode, {
264
- userRelation$entity: {
265
- $entity: 'userRelation',
266
- data: {
267
- id: 1,
268
- entity: 1,
269
- entityId: 1,
270
- userId: 1,
271
- relationId: 1,
272
- relation: {
273
- id: 1,
274
- name: 1,
275
- display: 1,
276
- }
277
- },
278
- filter: {
279
- userId: userId,
280
- },
281
- },
282
- });
283
- }
284
- noRelationDestEntities.push(entity2);
285
- }
286
- }
287
- };
288
- checkProjectionNode(entity, data);
289
- if (!sorter && relevantIds.length === 0) {
290
- // 如果没有sorter,就给予一个按createAt逆序的sorter
291
- Object.assign(selection, {
292
- sorter: [
293
- {
294
- $attr: {
295
- $$createAt$$: 1,
296
- },
297
- $direction: 'desc',
298
- }
299
- ]
300
- });
301
- Object.assign(data, {
302
- $$createAt$$: 1,
303
- });
304
- }
305
- };
306
- CascadeStore.prototype.reinforceOperation = function (entity, operation, context) {
307
- return tslib_1.__awaiter(this, void 0, void 0, function () {
308
- var _this = this;
309
- return tslib_1.__generator(this, function (_a) {
310
- switch (_a.label) {
311
- case 0: return [4 /*yield*/, Promise.all(this.operationRewriters.map(function (ele) { return ele(_this.getSchema(), entity, operation, context); }))];
312
- case 1:
313
- _a.sent();
314
- return [2 /*return*/];
315
- }
316
- });
317
- });
318
- };
319
- CascadeStore.prototype.registerOperationRewriter = function (rewriter) {
320
- this.operationRewriters.push(rewriter);
321
- };
322
- CascadeStore.prototype.registerSelectionRewriter = function (rewriter) {
323
- this.selectionRewriters.push(rewriter);
324
- };
325
- CascadeStore.prototype.destructCascadeSelect = function (entity, projection2, context, cascadeSelectFn, aggregateFn, option) {
326
- var _this = this;
327
- var projection = {};
328
- var cascadeSelectionFns = [];
329
- var supportMtoJoin = this.supportManyToOneJoin();
330
- var toModi = this.getSchema()[entity].toModi;
331
- (0, assert_1.default)(typeof projection2 === 'object');
332
- var _loop_1 = function (attr) {
333
- var _a, _b, _c, _d;
334
- var relation = (0, relation_1.judgeRelation)(this_1.storageSchema, entity, attr);
335
- if (relation === 1 || relation == 0) {
336
- Object.assign(projection, (_a = {},
337
- _a[attr] = projection2[attr],
338
- _a));
339
- }
340
- else if (relation === 2) {
341
- // 基于entity/entityId的多对一
342
- Object.assign(projection, {
343
- entity: 1,
344
- entityId: 1,
345
- });
346
- (0, assert_1.default)(typeof projection2[attr] === 'object');
347
- if (supportMtoJoin) {
348
- cascadeSelectionFns.push(function (result) {
349
- if (!toModi) {
350
- result.forEach(function (ele) {
351
- if (ele.entity === attr) {
352
- (0, assert_1.default)(ele.entityId);
353
- if (!ele[attr]) {
354
- throw new types_1.OakRowUnexistedException([{
355
- entity: attr,
356
- selection: {
357
- data: projection2[attr],
358
- filter: {
359
- id: ele.entityId,
360
- }
361
- }
362
- }]);
363
- }
364
- }
365
- });
366
- }
367
- });
368
- var _e = this_1.destructCascadeSelect(attr, projection2[attr], context, cascadeSelectFn, aggregateFn, option), subProjection = _e.projection, subCascadeSelectionFns = _e.cascadeSelectionFns;
369
- Object.assign(projection, (_b = {},
370
- _b[attr] = subProjection,
371
- _b));
372
- subCascadeSelectionFns.forEach(function (ele) { return cascadeSelectionFns.push(function (result) {
373
- return ele(result.map(function (ele2) { return ele2[attr]; }).filter(function (ele2) { return !!ele2; }));
374
- }); });
375
- }
376
- else {
377
- cascadeSelectionFns.push(function (result) {
378
- var entityIds = (0, lodash_1.uniq)(result.filter(function (ele) { return ele.entity === attr; }).map(function (ele) {
379
- (0, assert_1.default)(ele.entityId !== null);
380
- return ele.entityId;
381
- }));
382
- var dealWithSubRows = function (subRows) {
383
- (0, assert_1.default)(subRows.length <= entityIds.length);
384
- if (subRows.length < entityIds.length && !toModi) {
385
- // 后台不允许数据不一致
386
- if (context instanceof AsyncRowStore_1.AsyncContext || !option.ignoreForeignKeyMiss) {
387
- throw new types_1.OakRowUnexistedException([{
388
- entity: attr,
389
- selection: {
390
- data: projection2[attr],
391
- filter: {
392
- id: {
393
- $in: entityIds
394
- },
395
- },
396
- },
397
- }]);
398
- }
399
- }
400
- result.forEach(function (ele) {
401
- var _a, _b;
402
- if (ele.entity === attr) {
403
- var subRow = subRows.find(function (ele2) { return ele2.id === ele.entityId; });
404
- if (subRow) {
405
- Object.assign(ele, (_a = {},
406
- _a[attr] = subRow,
407
- _a));
408
- }
409
- else {
410
- Object.assign(ele, (_b = {},
411
- _b[attr] = null,
412
- _b));
413
- }
414
- }
415
- });
416
- };
417
- if (entityIds.length > 0) {
418
- var subRows = cascadeSelectFn.call(_this, attr, {
419
- data: projection2[attr],
420
- filter: {
421
- id: {
422
- $in: entityIds
423
- },
424
- },
425
- }, context, option);
426
- if (subRows instanceof Promise) {
427
- return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
428
- }
429
- else {
430
- dealWithSubRows(subRows);
431
- }
432
- }
433
- });
434
- }
435
- }
436
- else if (typeof relation === 'string') {
437
- Object.assign(projection, (_c = {},
438
- _c["".concat(attr, "Id")] = 1,
439
- _c));
440
- (0, assert_1.default)(typeof projection2[attr] === 'object');
441
- if (supportMtoJoin) {
442
- if (!toModi) {
443
- // 如果不是modi,要保证外键没有空指针
444
- cascadeSelectionFns.push(function (result) {
445
- if (!toModi) {
446
- result.forEach(function (ele) {
447
- if (ele["".concat(attr, "Id")] && !ele[attr]) {
448
- throw new types_1.OakRowUnexistedException([{
449
- entity: relation,
450
- selection: {
451
- data: projection2[attr],
452
- filter: {
453
- id: ele["".concat(attr, "Id")],
454
- }
455
- }
456
- }]);
457
- }
458
- });
459
- }
460
- });
461
- }
462
- var _f = this_1.destructCascadeSelect(relation, projection2[attr], context, cascadeSelectFn, aggregateFn, option), subProjection = _f.projection, subCascadeSelectionFns = _f.cascadeSelectionFns;
463
- Object.assign(projection, (_d = {},
464
- _d[attr] = subProjection,
465
- _d));
466
- subCascadeSelectionFns.forEach(function (ele) { return cascadeSelectionFns.push(function (result) {
467
- return ele(result.map(function (ele2) { return ele2[attr]; }).filter(function (ele2) { return !!ele2; }));
468
- }); });
469
- }
470
- else {
471
- cascadeSelectionFns.push(function (result) {
472
- var ids = (0, lodash_1.uniq)(result.filter(function (ele) { return !!(ele["".concat(attr, "Id")]); }).map(function (ele) { return ele["".concat(attr, "Id")]; }));
473
- var dealWithSubRows = function (subRows) {
474
- (0, assert_1.default)(subRows.length <= ids.length);
475
- if (subRows.length < ids.length && !toModi) {
476
- if (context instanceof AsyncRowStore_1.AsyncContext || !option.ignoreForeignKeyMiss) {
477
- throw new types_1.OakRowUnexistedException([{
478
- entity: relation,
479
- selection: {
480
- data: projection2[attr],
481
- filter: {
482
- id: {
483
- $in: ids
484
- },
485
- },
486
- }
487
- }]);
488
- }
489
- }
490
- result.forEach(function (ele) {
491
- var _a, _b, _c;
492
- if (ele["".concat(attr, "Id")]) {
493
- var subRow = subRows.find(function (ele2) { return ele2.id === ele["".concat(attr, "Id")]; });
494
- if (subRow) {
495
- Object.assign(ele, (_a = {},
496
- _a[attr] = subRow,
497
- _a));
498
- }
499
- else {
500
- Object.assign(ele, (_b = {},
501
- _b[attr] = null,
502
- _b));
503
- }
504
- }
505
- else {
506
- Object.assign(ele, (_c = {},
507
- _c[attr] = null,
508
- _c));
509
- }
510
- });
511
- };
512
- if (ids.length > 0) {
513
- var subRows = cascadeSelectFn.call(_this, relation, {
514
- data: projection2[attr],
515
- filter: {
516
- id: {
517
- $in: ids
518
- },
519
- },
520
- }, context, option);
521
- if (subRows instanceof Promise) {
522
- return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
523
- }
524
- dealWithSubRows(subRows);
525
- }
526
- });
527
- }
528
- }
529
- else {
530
- (0, assert_1.default)(relation instanceof Array);
531
- var _g = projection2[attr], subProjection_1 = _g.data, subFilter_1 = _g.filter, indexFrom_1 = _g.indexFrom, count_1 = _g.count, subSorter_1 = _g.sorter;
532
- var _h = tslib_1.__read(relation, 2), entity2_1 = _h[0], foreignKey_1 = _h[1];
533
- var isAggr = attr.endsWith('$$aggr');
534
- if (foreignKey_1) {
535
- // 基于属性的一对多
536
- if (isAggr) {
537
- // 是聚合运算,只有后台才需要执行
538
- (context instanceof AsyncRowStore_1.AsyncContext) && cascadeSelectionFns.push(function (result) {
539
- var aggrResults = result.map(function (row) {
540
- var _a, _b;
541
- var aggrResult = aggregateFn.call(_this, entity2_1, {
542
- data: subProjection_1,
543
- filter: (0, filter_1.combineFilters)(entity2_1, _this.getSchema(), [(_a = {},
544
- _a[foreignKey_1] = row.id,
545
- _a), subFilter_1]),
546
- sorter: subSorter_1,
547
- indexFrom: indexFrom_1,
548
- count: count_1
549
- }, context, option);
550
- if (aggrResult instanceof Promise) {
551
- return aggrResult.then(function (aggrResultResult) {
552
- var _a;
553
- return Object.assign(row, (_a = {},
554
- _a[attr] = aggrResultResult,
555
- _a));
556
- });
557
- }
558
- else {
559
- Object.assign(row, (_b = {},
560
- _b[attr] = aggrResult,
561
- _b));
562
- }
563
- });
564
- if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
565
- return Promise.all(aggrResults).then(function () { return undefined; });
566
- }
567
- });
568
- }
569
- else {
570
- // 是一对多查询
571
- cascadeSelectionFns.push(function (result) {
572
- var _a;
573
- var ids = result.map(function (ele) { return ele.id; });
574
- var dealWithSubRows = function (subRows) {
575
- var _a;
576
- // 这里如果result只有一行,则把返回结果直接置上,不对比外键值
577
- // 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
578
- if (result.length == 1) {
579
- Object.assign(result[0], (_a = {},
580
- _a[attr] = subRows,
581
- _a));
582
- }
583
- else {
584
- result.forEach(function (ele) {
585
- var _a;
586
- var subRowss = subRows.filter(function (ele2) { return ele2[foreignKey_1] === ele.id; });
587
- (0, assert_1.default)(subRowss);
588
- Object.assign(ele, (_a = {},
589
- _a[attr] = subRowss,
590
- _a));
591
- });
592
- }
593
- };
594
- if (ids.length > 0) {
595
- var subRows = cascadeSelectFn.call(_this, entity2_1, {
596
- data: subProjection_1,
597
- filter: (0, filter_1.combineFilters)(entity2_1, _this.getSchema(), [(_a = {},
598
- _a[foreignKey_1] = {
599
- $in: ids,
600
- },
601
- _a), subFilter_1]),
602
- sorter: subSorter_1,
603
- indexFrom: indexFrom_1,
604
- count: count_1
605
- }, context, option);
606
- if (subRows instanceof Promise) {
607
- return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
608
- }
609
- dealWithSubRows(subRows);
610
- }
611
- });
612
- }
613
- }
614
- else {
615
- // 基于entity的多对一
616
- if (isAggr) {
617
- // 是聚合运算,只有后台才需要执行
618
- (context instanceof AsyncRowStore_1.AsyncContext) && cascadeSelectionFns.push(function (result) {
619
- var aggrResults = result.map(function (row) {
620
- var _a;
621
- var aggrResult = aggregateFn.call(_this, entity2_1, {
622
- data: subProjection_1,
623
- filter: (0, filter_1.combineFilters)(entity2_1, _this.getSchema(), [{
624
- entity: entity,
625
- entityId: row.id,
626
- }, subFilter_1]),
627
- sorter: subSorter_1,
628
- indexFrom: indexFrom_1,
629
- count: count_1
630
- }, context, option);
631
- if (aggrResult instanceof Promise) {
632
- return aggrResult.then(function (aggrResultResult) {
633
- var _a;
634
- return Object.assign(row, (_a = {},
635
- _a[attr] = aggrResultResult,
636
- _a));
637
- });
638
- }
639
- else {
640
- Object.assign(row, (_a = {},
641
- _a[attr] = aggrResult,
642
- _a));
643
- }
644
- });
645
- if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
646
- return Promise.all(aggrResults).then(function () { return undefined; });
647
- }
648
- });
649
- }
650
- else {
651
- // 是一对多查询
652
- cascadeSelectionFns.push(function (result) {
653
- var ids = result.map(function (ele) { return ele.id; });
654
- var dealWithSubRows = function (subRows) {
655
- var _a;
656
- // 这里如果result只有一行,则把返回结果直接置上,不对比外键值
657
- // 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
658
- if (result.length === 1) {
659
- Object.assign(result[0], (_a = {},
660
- _a[attr] = subRows,
661
- _a));
662
- }
663
- else {
664
- result.forEach(function (ele) {
665
- var _a;
666
- var subRowss = subRows.filter(function (ele2) { return ele2.entityId === ele.id; });
667
- (0, assert_1.default)(subRowss);
668
- Object.assign(ele, (_a = {},
669
- _a[attr] = subRowss,
670
- _a));
671
- });
672
- }
673
- };
674
- if (ids.length > 0) {
675
- var subRows = cascadeSelectFn.call(_this, entity2_1, {
676
- data: subProjection_1,
677
- filter: (0, filter_1.combineFilters)(entity2_1, _this.getSchema(), [{
678
- entity: entity,
679
- entityId: {
680
- $in: ids,
681
- }
682
- }, subFilter_1]),
683
- sorter: subSorter_1,
684
- indexFrom: indexFrom_1,
685
- count: count_1
686
- }, context, option);
687
- if (subRows instanceof Promise) {
688
- return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
689
- }
690
- dealWithSubRows(subRows);
691
- }
692
- });
693
- }
694
- }
695
- }
696
- };
697
- var this_1 = this;
698
- for (var attr in projection2) {
699
- _loop_1(attr);
700
- }
701
- return {
702
- projection: projection,
703
- cascadeSelectionFns: cascadeSelectionFns,
704
- };
705
- };
706
- /**
707
- * 级联更新
708
- * A --> B
709
- 多对一:A CREATE/B CREATE,B data的主键赋到A的data上
710
- A CREATE/B UPDATE,B filter的主键来自A的data
711
- A UPDATE/B CREATE,B data的主键赋到A的data上
712
- A UPDATE/B UPDATE,B filter的主键来自A的row
713
- A UPDATE/B REMOVE,B filter的主键来自A的row
714
- A REMOVE/B UPDATE,B filter的主键来自A的row
715
- A REMOVE/B REMOVE,B filter的主键来自A的row
716
-
717
- 一对多:A CREATE/B CREATE,A data上的主键赋到B的data上
718
- A CREATE/B UPDATE,A data上的主键赋到B的data上
719
- A UPDATE/B CREATE,A filter上的主键赋到B的data上(一定是带主键的filter)
720
- A UPDATE/B UPDATE,A filter上的主键赋到B的filter上(一定是带主键的filter)
721
- A UPDATE/B REMOVE,A filter上的主键赋到B的filter上(一定是带主键的filter)
722
- A REMOVE/B UPDATE,A filter上的主键赋到B的filter上(且B关于A的外键清空)
723
- A REMOVE/B REMOVE,A filter上的主键赋到B的filter上
724
- *
725
- * 延时更新,
726
- * A(业务级别的申请对象) ---> B(业务级别需要更新的对象)
727
- * 两者必须通过entity/entityId关联
728
- * 此时需要把对B的更新记录成一条新插入的Modi对象,并将A上的entity/entityId指向该对象(新生成的Modi对象的id与此operation的id保持一致)
729
- * @param entity
730
- * @param action
731
- * @param data
732
- * @param context
733
- * @param option
734
- * @param result
735
- * @param filter
736
- * @returns
737
- */
738
- CascadeStore.prototype.destructCascadeUpdate = function (entity, action, data, context, option, cascadeUpdate, filter) {
739
- var _this = this;
740
- var modiAttr = this.getSchema()[entity].toModi;
741
- var option2 = Object.assign({}, option);
742
- var opData = {};
743
- var beforeFns = [];
744
- var afterFns = [];
745
- if (modiAttr && action !== 'remove' && !option.dontCreateModi) {
746
- // create/update具有modi对象的对象,对其子对象的update行为全部是create modi对象(缓存动作)
747
- // delete此对象,所有的modi子对象应该通过触发器作废,这个目前先通过系统的trigger来实现
748
- (0, assert_1.default)(!option2.modiParentId && !option2.modiParentEntity);
749
- if (action === 'create') {
750
- option2.modiParentId = data.id;
751
- }
752
- else {
753
- (0, assert_1.default)((filter === null || filter === void 0 ? void 0 : filter.id) && typeof filter.id === 'string');
754
- option2.modiParentId = filter.id;
755
- }
756
- option2.modiParentEntity = entity;
757
- }
758
- var _loop_2 = function (attr) {
759
- var _a, _b, _c, _d, e_2, _e;
760
- var relation = (0, relation_1.judgeRelation)(this_2.storageSchema, entity, attr);
761
- if (relation === 1) {
762
- Object.assign(opData, (_a = {},
763
- _a[attr] = data[attr],
764
- _a));
765
- }
766
- else if (relation === 2) {
767
- // 基于entity/entityId的many-to-one
768
- var operationMto_1 = data[attr];
769
- var actionMto = operationMto_1.action, dataMto = operationMto_1.data, filterMto = operationMto_1.filter;
770
- if (actionMto === 'create') {
771
- Object.assign(opData, {
772
- entityId: dataMto.id,
773
- entity: attr,
774
- });
775
- }
776
- else if (action === 'create') {
777
- var fkId = data.entityId, entity_1 = data.entity;
778
- (0, assert_1.default)(typeof fkId === 'string' || entity_1 === attr);
779
- if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
780
- // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
781
- (0, assert_1.default)(filterMto.id === fkId);
782
- }
783
- else {
784
- // A中data的entityId作为B中filter的主键
785
- Object.assign(operationMto_1, {
786
- filter: (0, filter_1.combineFilters)(attr, this_2.getSchema(), [{
787
- id: fkId,
788
- }, filterMto]),
789
- });
790
- }
791
- }
792
- else {
793
- // 剩下三种情况都是B中的filter的id来自A中row的entityId
794
- (0, assert_1.default)(!data.hasOwnProperty('entityId') && !data.hasOwnProperty('entity'));
795
- if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
796
- // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
797
- (0, assert_1.default)(typeof filterMto.id === 'string');
798
- }
799
- else if (filter.entity === attr && filter.entityId) {
800
- Object.assign(operationMto_1, {
801
- filter: (0, filter_1.combineFilters)(attr, this_2.getSchema(), [{
802
- id: filter.entityId,
803
- }, filterMto]),
804
- });
805
- }
806
- else if (filter[attr]) {
807
- Object.assign(operationMto_1, {
808
- filter: (0, filter_1.combineFilters)(attr, this_2.getSchema(), [filter[attr], filterMto]),
809
- });
810
- }
811
- else {
812
- // A中data的entityId作为B中filter的主键
813
- Object.assign(operationMto_1, {
814
- filter: (0, filter_1.combineFilters)(attr, this_2.getSchema(), [(_b = {},
815
- _b["".concat(entity, "$entity")] = {
816
- filter: filter,
817
- },
818
- _b), filterMto]),
819
- });
820
- }
821
- }
822
- beforeFns.push(function () { return cascadeUpdate.call(_this, attr, operationMto_1, context, option2); });
823
- }
824
- else if (typeof relation === 'string') {
825
- // 基于attr的外键的many-to-one
826
- var operationMto_2 = data[attr];
827
- var actionMto = operationMto_2.action, dataMto = operationMto_2.data, filterMto = operationMto_2.filter;
828
- if (actionMto === 'create') {
829
- Object.assign(opData, (_c = {},
830
- _c["".concat(attr, "Id")] = dataMto.id,
831
- _c));
832
- }
833
- else if (action === 'create') {
834
- var _f = data, _g = "".concat(attr, "Id"), fkId = _f[_g];
835
- (0, assert_1.default)(typeof fkId === 'string');
836
- if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
837
- // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
838
- (0, assert_1.default)(filterMto.id === fkId);
839
- }
840
- else {
841
- // A中data的entityId作为B中filter的主键
842
- Object.assign(operationMto_2, {
843
- filter: (0, filter_1.combineFilters)(relation, this_2.getSchema(), [filterMto, {
844
- id: fkId,
845
- }]),
846
- });
847
- }
848
- }
849
- else {
850
- (0, assert_1.default)(!data.hasOwnProperty("".concat(attr, "Id")));
851
- if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
852
- // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
853
- (0, assert_1.default)(typeof filterMto.id === 'string');
854
- }
855
- else if (filter["".concat(attr, "Id")]) {
856
- Object.assign(operationMto_2, {
857
- filter: (0, filter_1.combineFilters)(relation, this_2.getSchema(), [filterMto, {
858
- id: filter["".concat(attr, "Id")],
859
- }]),
860
- });
861
- }
862
- else if (filter[attr]) {
863
- Object.assign(operationMto_2, {
864
- filter: (0, filter_1.combineFilters)(relation, this_2.getSchema(), [filterMto, filter[attr]]),
865
- });
866
- }
867
- else {
868
- // A中data的attrId作为B中filter的主键
869
- Object.assign(operationMto_2, {
870
- filter: (0, filter_1.combineFilters)(relation, this_2.getSchema(), [filterMto, (_d = {},
871
- _d["".concat(entity, "$").concat(attr)] = filter,
872
- _d)]),
873
- });
874
- }
875
- }
876
- beforeFns.push(function () { return cascadeUpdate.call(_this, relation, operationMto_2, context, option2); });
877
- }
878
- else {
879
- (0, assert_1.default)(relation instanceof Array);
880
- var _h = tslib_1.__read(relation, 2), entityOtm_1 = _h[0], foreignKey_2 = _h[1];
881
- var otmOperations = data[attr];
882
- var dealWithOneToMany = function (otm) {
883
- var _a, _b, _c, _d, _e, _f, _g;
884
- var actionOtm = otm.action, dataOtm = otm.data, filterOtm = otm.filter;
885
- if (!foreignKey_2) {
886
- // 基于entity/entityId的one-to-many
887
- if (action === 'create') {
888
- var id_1 = data.id;
889
- if (dataOtm instanceof Array) {
890
- dataOtm.forEach(function (ele) { return Object.assign(ele, {
891
- entity: entity,
892
- entityId: id_1,
893
- }); });
894
- }
895
- else {
896
- Object.assign(dataOtm, {
897
- entity: entity,
898
- entityId: id_1,
899
- });
900
- }
901
- }
902
- else if (actionOtm === 'create') {
903
- // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
904
- // todo 这个假设对watcher等后台行为可能不成立,等遇到create/create一对多的case再完善
905
- var id_2 = filter.id;
906
- (0, assert_1.default)(typeof id_2 === 'string');
907
- if (dataOtm instanceof Array) {
908
- dataOtm.forEach(function (ele) { return Object.assign(ele, {
909
- entity: entity,
910
- entityId: id_2,
911
- }); });
912
- }
913
- else {
914
- Object.assign(dataOtm, {
915
- entity: entity,
916
- entityId: id_2,
917
- });
918
- }
919
- }
920
- else {
921
- // 这里优化一下,如果filter上有id,直接更新成根据entityId来过滤
922
- if (filter) {
923
- if (filter.id && Object.keys(filter).length === 1) {
924
- Object.assign(otm, {
925
- filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [{
926
- entity: entity,
927
- entityId: filter.id,
928
- }, filterOtm]),
929
- });
930
- }
931
- else {
932
- Object.assign(otm, {
933
- filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [(_a = {},
934
- _a[entity] = filter,
935
- _a), filterOtm]),
936
- });
937
- }
938
- }
939
- else {
940
- Object.assign(otm, {
941
- filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [{
942
- entity: entity,
943
- entityId: {
944
- $exists: true,
945
- }
946
- }, filterOtm])
947
- });
948
- }
949
- if (action === 'remove' && actionOtm === 'update') {
950
- Object.assign(dataOtm, {
951
- entity: null,
952
- entityId: null,
953
- });
954
- }
955
- }
956
- }
957
- else {
958
- // 基于foreignKey的one-to-many
959
- if (action === 'create') {
960
- var id_3 = data.id;
961
- if (dataOtm instanceof Array) {
962
- dataOtm.forEach(function (ele) {
963
- var _a;
964
- return Object.assign(ele, (_a = {},
965
- _a[foreignKey_2] = id_3,
966
- _a));
967
- });
968
- }
969
- else {
970
- Object.assign(dataOtm, (_b = {},
971
- _b[foreignKey_2] = id_3,
972
- _b));
973
- }
974
- }
975
- else if (actionOtm === 'create') {
976
- // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
977
- // todo 这个假设在后台可能不成立,等遇到了再说
978
- var id_4 = filter.id;
979
- (0, assert_1.default)(typeof id_4 === 'string');
980
- if (dataOtm instanceof Array) {
981
- dataOtm.forEach(function (ele) {
982
- var _a;
983
- return Object.assign(ele, (_a = {},
984
- _a[foreignKey_2] = id_4,
985
- _a));
986
- });
987
- }
988
- else {
989
- Object.assign(dataOtm, (_c = {},
990
- _c[foreignKey_2] = id_4,
991
- _c));
992
- }
993
- }
994
- else {
995
- // 这里优化一下,如果filter上有id,直接更新成根据entityId来过滤
996
- if (filter) {
997
- if (filter.id && Object.keys(filter).length === 1) {
998
- Object.assign(otm, {
999
- filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [(_d = {},
1000
- _d[foreignKey_2] = filter.id,
1001
- _d), filterOtm]),
1002
- });
1003
- }
1004
- else {
1005
- Object.assign(otm, {
1006
- filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [(_e = {},
1007
- _e[foreignKey_2.slice(0, foreignKey_2.length - 2)] = filter,
1008
- _e), filterOtm]),
1009
- });
1010
- }
1011
- }
1012
- else {
1013
- Object.assign(otm, {
1014
- filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [(_f = {},
1015
- _f[foreignKey_2] = {
1016
- $exists: true,
1017
- },
1018
- _f), filterOtm]),
1019
- });
1020
- }
1021
- if (action === 'remove' && actionOtm === 'update') {
1022
- Object.assign(dataOtm, (_g = {},
1023
- _g[foreignKey_2] = null,
1024
- _g));
1025
- }
1026
- }
1027
- }
1028
- // 一对多的依赖应该后建,否则中间会出现空指针,导致checker等出错
1029
- afterFns.push(function () { return cascadeUpdate.call(_this, entityOtm_1, otm, context, option2); });
1030
- };
1031
- if (otmOperations instanceof Array) {
1032
- try {
1033
- for (var otmOperations_1 = (e_2 = void 0, tslib_1.__values(otmOperations)), otmOperations_1_1 = otmOperations_1.next(); !otmOperations_1_1.done; otmOperations_1_1 = otmOperations_1.next()) {
1034
- var oper = otmOperations_1_1.value;
1035
- dealWithOneToMany(oper);
1036
- }
1037
- }
1038
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1039
- finally {
1040
- try {
1041
- if (otmOperations_1_1 && !otmOperations_1_1.done && (_e = otmOperations_1.return)) _e.call(otmOperations_1);
1042
- }
1043
- finally { if (e_2) throw e_2.error; }
1044
- }
1045
- }
1046
- else {
1047
- dealWithOneToMany(otmOperations);
1048
- }
1049
- }
1050
- };
1051
- var this_2 = this;
1052
- for (var attr in data) {
1053
- _loop_2(attr);
1054
- }
1055
- return {
1056
- data: opData,
1057
- beforeFns: beforeFns,
1058
- afterFns: afterFns,
1059
- };
1060
- };
1061
- // 对插入的数据,没有初始值的属性置null
1062
- CascadeStore.prototype.preProcessDataCreated = function (entity, data) {
1063
- var now = Date.now();
1064
- var attributes = this.getSchema()[entity].attributes;
1065
- var processSingle = function (data2) {
1066
- var _a, _b;
1067
- for (var key in attributes) {
1068
- if (data2[key] === undefined) {
1069
- Object.assign(data2, (_a = {},
1070
- _a[key] = null,
1071
- _a));
1072
- }
1073
- }
1074
- Object.assign(data2, (_b = {},
1075
- _b[Entity_1.CreateAtAttribute] = now,
1076
- _b[Entity_1.UpdateAtAttribute] = now,
1077
- _b[Entity_1.DeleteAtAttribute] = null,
1078
- _b));
1079
- };
1080
- if (data instanceof Array) {
1081
- data.forEach(function (ele) { return processSingle(ele); });
1082
- }
1083
- else {
1084
- processSingle(data);
1085
- }
1086
- };
1087
- // 对更新的数据,去掉所有的undefined属性
1088
- CascadeStore.prototype.preProcessDataUpdated = function (data) {
1089
- var undefinedKeys = Object.keys(data).filter(function (ele) { return data[ele] === undefined; });
1090
- undefinedKeys.forEach(function (ele) { return (0, lodash_1.unset)(data, ele); });
1091
- };
1092
- CascadeStore.prototype.judgeRelation = function (entity, attr) {
1093
- return (0, relation_1.judgeRelation)(this.storageSchema, entity, attr);
1094
- };
1095
- /**
1096
- * 和具体的update过程无关的例程放在这里,包括对later动作的处理、对oper的记录以及对record的收集等
1097
- * @param entity
1098
- * @param operation
1099
- * @param context
1100
- * @param option
1101
- */
1102
- CascadeStore.prototype.doUpdateSingleRowAsync = function (entity, operation, context, option) {
1103
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1104
- var data, action, operId, filter, now, _a, modiCreate, closeRootMode, result_1, createInner, multipleCreate, data_2, data_2_1, d, createSingleOper, e_3_1, operatorId, createOper, _b, closeRootMode, ids_1, selection, rows, modiUpsert, upsertModis, _c, originData, originId, _d, closeRootMode, createOper, updateAttrCount, result;
1105
- var e_3, _e, _f, _g, _h, _j, _k, _l;
1106
- var _this = this;
1107
- return tslib_1.__generator(this, function (_m) {
1108
- switch (_m.label) {
1109
- case 0:
1110
- data = operation.data, action = operation.action, operId = operation.id, filter = operation.filter;
1111
- now = Date.now();
1112
- _a = action;
1113
- switch (_a) {
1114
- case 'create': return [3 /*break*/, 1];
1115
- }
1116
- return [3 /*break*/, 22];
1117
- case 1:
1118
- if (!(option.modiParentEntity && !['modi', 'modiEntity', 'oper', 'operEntity'].includes(entity))) return [3 /*break*/, 3];
1119
- // 变成对modi的插入
1120
- (0, assert_1.default)(option.modiParentId);
1121
- modiCreate = {
1122
- id: 'dummy',
1123
- action: 'create',
1124
- data: {
1125
- id: operId,
1126
- targetEntity: entity,
1127
- action: action,
1128
- entity: option.modiParentEntity,
1129
- entityId: option.modiParentId,
1130
- filter: {
1131
- id: data.id, //这里记录这个filter是为了后面update的时候直接在其上面update,参见本函数后半段关于modiUpsert相关的优化
1132
- },
1133
- data: data,
1134
- iState: 'active',
1135
- },
1136
- };
1137
- closeRootMode = context.openRootMode();
1138
- return [4 /*yield*/, this.cascadeUpdateAsync('modi', modiCreate, context, option)];
1139
- case 2:
1140
- _m.sent();
1141
- closeRootMode();
1142
- return [2 /*return*/, 1];
1143
- case 3:
1144
- this.preProcessDataCreated(entity, data);
1145
- result_1 = 0;
1146
- createInner = function (operation2) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1147
- var _a, e_4;
1148
- return tslib_1.__generator(this, function (_b) {
1149
- switch (_b.label) {
1150
- case 0:
1151
- _b.trys.push([0, 2, , 3]);
1152
- _a = result_1;
1153
- return [4 /*yield*/, this.updateAbjointRowAsync(entity, operation2, context, option)];
1154
- case 1:
1155
- result_1 = _a + _b.sent();
1156
- return [3 /*break*/, 3];
1157
- case 2:
1158
- e_4 = _b.sent();
1159
- /* 这段代码是处理插入时有重复的行,现在看有问题,等实际需求出现再写
1160
- if (e instanceof OakCongruentRowExists) {
1161
- if (option.allowExists) {
1162
- // 如果允许存在,对已存在行进行update,剩下的行继续insert
1163
- const congruentRow = e.getData() as ED[T]['OpSchema'];
1164
- if (data instanceof Array) {
1165
- const rest = data.filter(
1166
- ele => ele.id !== congruentRow.id
1167
- );
1168
- if (rest.length === data.length) {
1169
- throw e;
1170
- }
1171
- const result2 = await this.updateAbjointRow(
1172
- entity,
1173
- Object.assign({}, operation, {
1174
- data: rest,
1175
- }),
1176
- context,
1177
- option
1178
- );
1179
-
1180
- const row = data.find(
1181
- ele => ele.id === congruentRow.id
1182
- );
1183
- const updateData = omit(row, ['id', '$$createAt$$']);
1184
- const result3 = await this.updateAbjointRow(
1185
- entity,
1186
- {
1187
- id: await generateNewId(),
1188
- action: 'update',
1189
- data: updateData,
1190
- filter: {
1191
- id: congruentRow.id,
1192
- } as any,
1193
- },
1194
- context,
1195
- option
1196
- );
1197
-
1198
- return result2 + result3;
1199
- }
1200
- else {
1201
- if (data.id !== congruentRow.id) {
1202
- throw e;
1203
- }
1204
- const updateData = omit(data, ['id', '$$createAt$$']);
1205
- const result2 = await this.updateAbjointRow(
1206
- entity,
1207
- {
1208
- id: await generateNewId(),
1209
- action: 'update',
1210
- data: updateData,
1211
- filter: {
1212
- id: congruentRow.id,
1213
- } as any,
1214
- },
1215
- context,
1216
- option
1217
- );
1218
- return result2;
1219
- }
1220
- }
1221
- } */
1222
- throw e_4;
1223
- case 3: return [2 /*return*/];
1224
- }
1225
- });
1226
- }); };
1227
- if (!(data instanceof Array)) return [3 /*break*/, 13];
1228
- multipleCreate = this.supportMultipleCreate();
1229
- if (!multipleCreate) return [3 /*break*/, 5];
1230
- return [4 /*yield*/, createInner(operation)];
1231
- case 4:
1232
- _m.sent();
1233
- return [3 /*break*/, 12];
1234
- case 5:
1235
- _m.trys.push([5, 10, 11, 12]);
1236
- data_2 = tslib_1.__values(data), data_2_1 = data_2.next();
1237
- _m.label = 6;
1238
- case 6:
1239
- if (!!data_2_1.done) return [3 /*break*/, 9];
1240
- d = data_2_1.value;
1241
- createSingleOper = {
1242
- id: 'any',
1243
- action: 'create',
1244
- data: d,
1245
- };
1246
- return [4 /*yield*/, createInner(createSingleOper)];
1247
- case 7:
1248
- _m.sent();
1249
- _m.label = 8;
1250
- case 8:
1251
- data_2_1 = data_2.next();
1252
- return [3 /*break*/, 6];
1253
- case 9: return [3 /*break*/, 12];
1254
- case 10:
1255
- e_3_1 = _m.sent();
1256
- e_3 = { error: e_3_1 };
1257
- return [3 /*break*/, 12];
1258
- case 11:
1259
- try {
1260
- if (data_2_1 && !data_2_1.done && (_e = data_2.return)) _e.call(data_2);
1261
- }
1262
- finally { if (e_3) throw e_3.error; }
1263
- return [7 /*endfinally*/];
1264
- case 12: return [3 /*break*/, 15];
1265
- case 13: return [4 /*yield*/, createInner(operation)];
1266
- case 14:
1267
- _m.sent();
1268
- _m.label = 15;
1269
- case 15:
1270
- if (!option.dontCollect) {
1271
- context.opRecords.push({
1272
- a: 'c',
1273
- e: entity,
1274
- d: data,
1275
- });
1276
- }
1277
- if (!(!option.dontCreateOper && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity))) return [3 /*break*/, 21];
1278
- // 按照框架要求生成Oper和OperEntity这两个内置的对象
1279
- (0, assert_1.default)(operId);
1280
- operatorId = context.getCurrentUserId(true);
1281
- if (!operatorId) return [3 /*break*/, 21];
1282
- _f = {
1283
- id: 'dummy',
1284
- action: 'create'
1285
- };
1286
- _g = {
1287
- id: operId,
1288
- action: action,
1289
- data: data,
1290
- operatorId: operatorId,
1291
- targetEntity: entity
1292
- };
1293
- if (!(data instanceof Array)) return [3 /*break*/, 17];
1294
- _h = {
1295
- id: 'dummy',
1296
- action: 'create'
1297
- };
1298
- return [4 /*yield*/, Promise.all(data.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1299
- var _a;
1300
- return tslib_1.__generator(this, function (_b) {
1301
- switch (_b.label) {
1302
- case 0:
1303
- _a = {};
1304
- return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1305
- case 1: return [2 /*return*/, (_a.id = _b.sent(),
1306
- _a.entityId = ele.id,
1307
- _a.entity = entity,
1308
- _a)];
1309
- }
1310
- });
1311
- }); }))];
1312
- case 16:
1313
- _b = (_h.data = _m.sent(),
1314
- _h);
1315
- return [3 /*break*/, 19];
1316
- case 17:
1317
- _j = {
1318
- id: 'dummy',
1319
- action: 'create'
1320
- };
1321
- _k = {};
1322
- return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1323
- case 18:
1324
- _b = [(_j.data = (_k.id = _m.sent(),
1325
- _k.entityId = data.id,
1326
- _k.entity = entity,
1327
- _k),
1328
- _j)];
1329
- _m.label = 19;
1330
- case 19:
1331
- createOper = (_f.data = (_g.operEntity$oper = _b,
1332
- _g),
1333
- _f);
1334
- closeRootMode = context.openRootMode();
1335
- return [4 /*yield*/, this.cascadeUpdateAsync('oper', createOper, context, {
1336
- dontCollect: true,
1337
- dontCreateOper: true,
1338
- })];
1339
- case 20:
1340
- _m.sent();
1341
- closeRootMode();
1342
- _m.label = 21;
1343
- case 21: return [2 /*return*/, result_1];
1344
- case 22:
1345
- ids_1 = (0, filter_2.getRelevantIds)(filter);
1346
- if (!(ids_1.length === 0)) return [3 /*break*/, 24];
1347
- selection = {
1348
- data: {
1349
- id: 1,
1350
- },
1351
- filter: operation.filter,
1352
- indexFrom: operation.indexFrom,
1353
- count: operation.count,
1354
- };
1355
- return [4 /*yield*/, this.selectAbjointRowAsync(entity, selection, context, {
1356
- dontCollect: true,
1357
- })];
1358
- case 23:
1359
- rows = _m.sent();
1360
- ids_1.push.apply(ids_1, tslib_1.__spreadArray([], tslib_1.__read((rows.map(function (ele) { return ele.id; }))), false));
1361
- _m.label = 24;
1362
- case 24:
1363
- if (data) {
1364
- this.preProcessDataUpdated(data);
1365
- }
1366
- if (!(option.modiParentEntity && !['modi', 'modiEntity'].includes(entity))) return [3 /*break*/, 30];
1367
- modiUpsert = void 0;
1368
- if (!(action !== 'remove')) return [3 /*break*/, 26];
1369
- return [4 /*yield*/, this.selectAbjointRowAsync('modi', {
1370
- data: {
1371
- id: 1,
1372
- data: 1,
1373
- },
1374
- filter: {
1375
- targetEntity: entity,
1376
- action: {
1377
- $in: ['create', 'update'],
1378
- },
1379
- entity: option.modiParentEntity,
1380
- entityId: option.modiParentId,
1381
- iState: 'active',
1382
- filter: ids_1.length > 0 ? {
1383
- id: {
1384
- $in: ids_1,
1385
- },
1386
- } : filter,
1387
- },
1388
- sorter: [
1389
- {
1390
- $attr: {
1391
- $$createAt$$: 1,
1392
- },
1393
- $direction: 'desc',
1394
- }
1395
- ],
1396
- indexFrom: 0,
1397
- count: 1,
1398
- }, context, option)];
1399
- case 25:
1400
- upsertModis = _m.sent();
1401
- if (upsertModis.length > 0) {
1402
- _c = upsertModis[0], originData = _c.data, originId = _c.id;
1403
- modiUpsert = {
1404
- id: 'dummy',
1405
- action: 'update',
1406
- data: {
1407
- data: Object.assign({}, originData, data),
1408
- },
1409
- filter: {
1410
- id: originId,
1411
- }
1412
- };
1413
- }
1414
- _m.label = 26;
1415
- case 26:
1416
- if (!!modiUpsert) return [3 /*break*/, 28];
1417
- modiUpsert = {
1418
- id: 'dummy',
1419
- action: 'create',
1420
- data: {
1421
- id: operId,
1422
- targetEntity: entity,
1423
- entity: option.modiParentEntity,
1424
- entityId: option.modiParentId,
1425
- action: action,
1426
- data: data,
1427
- iState: 'active',
1428
- filter: filter,
1429
- },
1430
- };
1431
- if (!(ids_1.length > 0)) return [3 /*break*/, 28];
1432
- _d = modiUpsert.data;
1433
- _l = {
1434
- id: 'dummy',
1435
- action: 'create'
1436
- };
1437
- return [4 /*yield*/, Promise.all(ids_1.map(function (id) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1438
- var _a;
1439
- return tslib_1.__generator(this, function (_b) {
1440
- switch (_b.label) {
1441
- case 0:
1442
- _a = {};
1443
- return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1444
- case 1: return [2 /*return*/, (_a.id = _b.sent(),
1445
- _a.entity = entity,
1446
- _a.entityId = id,
1447
- _a)];
1448
- }
1449
- });
1450
- }); }))];
1451
- case 27:
1452
- _d.modiEntity$modi = (_l.data = _m.sent(),
1453
- _l);
1454
- _m.label = 28;
1455
- case 28:
1456
- closeRootMode = context.openRootMode();
1457
- return [4 /*yield*/, this.cascadeUpdateAsync('modi', modiUpsert, context, option)];
1458
- case 29:
1459
- _m.sent();
1460
- closeRootMode();
1461
- return [2 /*return*/, 1];
1462
- case 30:
1463
- createOper = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1464
- var createOper_1, closeRootMode;
1465
- var _a, _b, _c;
1466
- var _this = this;
1467
- return tslib_1.__generator(this, function (_d) {
1468
- switch (_d.label) {
1469
- case 0:
1470
- if (!(!(option === null || option === void 0 ? void 0 : option.dontCreateOper) && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity) && ids_1.length > 0)) return [3 /*break*/, 3];
1471
- // 按照框架要求生成Oper和OperEntity这两个内置的对象
1472
- (0, assert_1.default)(operId);
1473
- _a = {
1474
- id: 'dummy',
1475
- action: 'create'
1476
- };
1477
- _b = {
1478
- id: operId,
1479
- action: action,
1480
- data: data,
1481
- targetEntity: entity
1482
- };
1483
- _c = {
1484
- id: 'dummy',
1485
- action: 'create'
1486
- };
1487
- return [4 /*yield*/, Promise.all(ids_1.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1488
- var _a;
1489
- return tslib_1.__generator(this, function (_b) {
1490
- switch (_b.label) {
1491
- case 0:
1492
- _a = {};
1493
- return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1494
- case 1: return [2 /*return*/, (_a.id = _b.sent(),
1495
- _a.entityId = ele,
1496
- _a.entity = entity,
1497
- _a)];
1498
- }
1499
- });
1500
- }); }))];
1501
- case 1:
1502
- createOper_1 = (_a.data = (_b.operEntity$oper = (_c.data = _d.sent(),
1503
- _c),
1504
- _b),
1505
- _a);
1506
- closeRootMode = context.openRootMode();
1507
- return [4 /*yield*/, this.cascadeUpdateAsync('oper', createOper_1, context, {
1508
- dontCollect: true,
1509
- dontCreateOper: true,
1510
- })];
1511
- case 2:
1512
- _d.sent();
1513
- closeRootMode();
1514
- _d.label = 3;
1515
- case 3: return [2 /*return*/];
1516
- }
1517
- });
1518
- }); };
1519
- if (!(action === 'remove')) return [3 /*break*/, 31];
1520
- if (!option.dontCollect) {
1521
- context.opRecords.push({
1522
- a: 'r',
1523
- e: entity,
1524
- f: {
1525
- id: {
1526
- $in: ids_1,
1527
- }
1528
- },
1529
- });
1530
- }
1531
- return [3 /*break*/, 35];
1532
- case 31:
1533
- updateAttrCount = Object.keys(data).length;
1534
- if (!(updateAttrCount > 0)) return [3 /*break*/, 32];
1535
- // 优化一下,如果不更新任何属性,则不实际执行
1536
- Object.assign(data, {
1537
- $$updateAt$$: now,
1538
- });
1539
- if (!option.dontCollect) {
1540
- context.opRecords.push({
1541
- a: 'u',
1542
- e: entity,
1543
- d: data,
1544
- f: {
1545
- id: {
1546
- $in: ids_1,
1547
- }
1548
- },
1549
- });
1550
- }
1551
- return [3 /*break*/, 35];
1552
- case 32:
1553
- if (!(action !== 'update')) return [3 /*break*/, 34];
1554
- // 如果不是update动作而是用户自定义的动作,这里还是要记录oper
1555
- return [4 /*yield*/, createOper()];
1556
- case 33:
1557
- // 如果不是update动作而是用户自定义的动作,这里还是要记录oper
1558
- _m.sent();
1559
- return [2 /*return*/, 0];
1560
- case 34: return [2 /*return*/, 0];
1561
- case 35: return [4 /*yield*/, this.updateAbjointRowAsync(entity, operation, context, option)];
1562
- case 36:
1563
- result = _m.sent();
1564
- return [4 /*yield*/, createOper()];
1565
- case 37:
1566
- _m.sent();
1567
- return [2 /*return*/, result];
1568
- }
1569
- });
1570
- });
1571
- };
1572
- CascadeStore.prototype.doUpdateSingleRow = function (entity, operation, context, option) {
1573
- var e_5, _a;
1574
- var _this = this;
1575
- var data = operation.data, action = operation.action, operId = operation.id, filter = operation.filter;
1576
- var now = Date.now();
1577
- switch (action) {
1578
- case 'create': {
1579
- this.preProcessDataCreated(entity, data);
1580
- var result_2 = 0;
1581
- var createInner = function (operation2) {
1582
- try {
1583
- result_2 += _this.updateAbjointRow(entity, operation2, context, option);
1584
- }
1585
- catch (e) {
1586
- throw e;
1587
- }
1588
- };
1589
- if (data instanceof Array) {
1590
- var multipleCreate = this.supportMultipleCreate();
1591
- if (multipleCreate) {
1592
- createInner(operation);
1593
- }
1594
- else {
1595
- try {
1596
- for (var data_3 = tslib_1.__values(data), data_3_1 = data_3.next(); !data_3_1.done; data_3_1 = data_3.next()) {
1597
- var d = data_3_1.value;
1598
- var createSingleOper = {
1599
- id: 'any',
1600
- action: 'create',
1601
- data: d,
1602
- };
1603
- createInner(createSingleOper);
1604
- }
1605
- }
1606
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
1607
- finally {
1608
- try {
1609
- if (data_3_1 && !data_3_1.done && (_a = data_3.return)) _a.call(data_3);
1610
- }
1611
- finally { if (e_5) throw e_5.error; }
1612
- }
1613
- }
1614
- }
1615
- else {
1616
- createInner(operation);
1617
- }
1618
- return result_2;
1619
- }
1620
- default: {
1621
- if (action === 'remove') {
1622
- }
1623
- else {
1624
- var updateAttrCount = Object.keys(data).length;
1625
- if (updateAttrCount > 0) {
1626
- // 优化一下,如果不更新任何属性,则不实际执行
1627
- Object.assign(data, {
1628
- $$updateAt$$: now,
1629
- });
1630
- this.preProcessDataUpdated(data);
1631
- }
1632
- else {
1633
- return 0;
1634
- }
1635
- }
1636
- return this.updateAbjointRow(entity, operation, context, option);
1637
- }
1638
- }
1639
- };
1640
- CascadeStore.prototype.cascadeUpdate = function (entity, operation, context, option) {
1641
- var e_6, _a, e_7, _b, e_8, _c;
1642
- var action = operation.action, data = operation.data, filter = operation.filter, id = operation.id;
1643
- var opData;
1644
- var wholeBeforeFns = [];
1645
- var wholeAfterFns = [];
1646
- var result = {};
1647
- if (['create', 'create-l'].includes(action) && data instanceof Array) {
1648
- opData = [];
1649
- try {
1650
- for (var data_4 = tslib_1.__values(data), data_4_1 = data_4.next(); !data_4_1.done; data_4_1 = data_4.next()) {
1651
- var d = data_4_1.value;
1652
- var _d = this.destructCascadeUpdate(entity, action, d, context, option, this.cascadeUpdate), od = _d.data, beforeFns = _d.beforeFns, afterFns = _d.afterFns;
1653
- opData.push(od);
1654
- wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1655
- wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1656
- }
1657
- }
1658
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
1659
- finally {
1660
- try {
1661
- if (data_4_1 && !data_4_1.done && (_a = data_4.return)) _a.call(data_4);
1662
- }
1663
- finally { if (e_6) throw e_6.error; }
1664
- }
1665
- }
1666
- else {
1667
- var _e = this.destructCascadeUpdate(entity, action, data, context, option, this.cascadeUpdate, filter), od = _e.data, beforeFns = _e.beforeFns, afterFns = _e.afterFns;
1668
- opData = od;
1669
- wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1670
- wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1671
- }
1672
- var operation2 = Object.assign({}, operation, {
1673
- data: opData,
1674
- });
1675
- try {
1676
- for (var wholeBeforeFns_1 = tslib_1.__values(wholeBeforeFns), wholeBeforeFns_1_1 = wholeBeforeFns_1.next(); !wholeBeforeFns_1_1.done; wholeBeforeFns_1_1 = wholeBeforeFns_1.next()) {
1677
- var before_1 = wholeBeforeFns_1_1.value;
1678
- before_1();
1679
- }
1680
- }
1681
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
1682
- finally {
1683
- try {
1684
- if (wholeBeforeFns_1_1 && !wholeBeforeFns_1_1.done && (_b = wholeBeforeFns_1.return)) _b.call(wholeBeforeFns_1);
1685
- }
1686
- finally { if (e_7) throw e_7.error; }
1687
- }
1688
- var count = this.doUpdateSingleRow(entity, operation2, context, option);
1689
- try {
1690
- for (var wholeAfterFns_1 = tslib_1.__values(wholeAfterFns), wholeAfterFns_1_1 = wholeAfterFns_1.next(); !wholeAfterFns_1_1.done; wholeAfterFns_1_1 = wholeAfterFns_1.next()) {
1691
- var after_1 = wholeAfterFns_1_1.value;
1692
- after_1();
1693
- }
1694
- }
1695
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
1696
- finally {
1697
- try {
1698
- if (wholeAfterFns_1_1 && !wholeAfterFns_1_1.done && (_c = wholeAfterFns_1.return)) _c.call(wholeAfterFns_1);
1699
- }
1700
- finally { if (e_8) throw e_8.error; }
1701
- }
1702
- return result;
1703
- };
1704
- /**
1705
- *
1706
- * @param entity
1707
- * @param operation
1708
- * @param context
1709
- * @param option
1710
- */
1711
- CascadeStore.prototype.cascadeUpdateAsync = function (entity, operation, context, option) {
1712
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1713
- var action, data, filter, id, opData, wholeBeforeFns, wholeAfterFns, result, data_5, data_5_1, d, _a, od, beforeFns, afterFns, _b, od, beforeFns, afterFns, operation2, wholeBeforeFns_2, wholeBeforeFns_2_1, before_2, e_9_1, count, wholeAfterFns_2, wholeAfterFns_2_1, after_2, e_10_1;
1714
- var e_11, _c, e_9, _d, _e, _f, e_10, _g;
1715
- return tslib_1.__generator(this, function (_h) {
1716
- switch (_h.label) {
1717
- case 0:
1718
- action = operation.action, data = operation.data, filter = operation.filter, id = operation.id;
1719
- wholeBeforeFns = [];
1720
- wholeAfterFns = [];
1721
- result = {};
1722
- if (['create', 'create-l'].includes(action) && data instanceof Array) {
1723
- opData = [];
1724
- try {
1725
- for (data_5 = tslib_1.__values(data), data_5_1 = data_5.next(); !data_5_1.done; data_5_1 = data_5.next()) {
1726
- d = data_5_1.value;
1727
- _a = this.destructCascadeUpdate(entity, action, d, context, option, this.cascadeUpdateAsync), od = _a.data, beforeFns = _a.beforeFns, afterFns = _a.afterFns;
1728
- opData.push(od);
1729
- wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1730
- wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1731
- }
1732
- }
1733
- catch (e_11_1) { e_11 = { error: e_11_1 }; }
1734
- finally {
1735
- try {
1736
- if (data_5_1 && !data_5_1.done && (_c = data_5.return)) _c.call(data_5);
1737
- }
1738
- finally { if (e_11) throw e_11.error; }
1739
- }
1740
- }
1741
- else {
1742
- _b = this.destructCascadeUpdate(entity, action, data, context, option, this.cascadeUpdateAsync, filter), od = _b.data, beforeFns = _b.beforeFns, afterFns = _b.afterFns;
1743
- opData = od;
1744
- wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1745
- wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1746
- }
1747
- operation2 = Object.assign({}, operation, {
1748
- data: opData,
1749
- });
1750
- _h.label = 1;
1751
- case 1:
1752
- _h.trys.push([1, 6, 7, 8]);
1753
- wholeBeforeFns_2 = tslib_1.__values(wholeBeforeFns), wholeBeforeFns_2_1 = wholeBeforeFns_2.next();
1754
- _h.label = 2;
1755
- case 2:
1756
- if (!!wholeBeforeFns_2_1.done) return [3 /*break*/, 5];
1757
- before_2 = wholeBeforeFns_2_1.value;
1758
- return [4 /*yield*/, before_2()];
1759
- case 3:
1760
- _h.sent();
1761
- _h.label = 4;
1762
- case 4:
1763
- wholeBeforeFns_2_1 = wholeBeforeFns_2.next();
1764
- return [3 /*break*/, 2];
1765
- case 5: return [3 /*break*/, 8];
1766
- case 6:
1767
- e_9_1 = _h.sent();
1768
- e_9 = { error: e_9_1 };
1769
- return [3 /*break*/, 8];
1770
- case 7:
1771
- try {
1772
- if (wholeBeforeFns_2_1 && !wholeBeforeFns_2_1.done && (_d = wholeBeforeFns_2.return)) _d.call(wholeBeforeFns_2);
1773
- }
1774
- finally { if (e_9) throw e_9.error; }
1775
- return [7 /*endfinally*/];
1776
- case 8: return [4 /*yield*/, this.doUpdateSingleRowAsync(entity, operation2, context, option)];
1777
- case 9:
1778
- count = _h.sent();
1779
- this.mergeOperationResult(result, (_e = {},
1780
- _e[entity] = (_f = {},
1781
- _f[operation2.action] = count,
1782
- _f),
1783
- _e));
1784
- _h.label = 10;
1785
- case 10:
1786
- _h.trys.push([10, 15, 16, 17]);
1787
- wholeAfterFns_2 = tslib_1.__values(wholeAfterFns), wholeAfterFns_2_1 = wholeAfterFns_2.next();
1788
- _h.label = 11;
1789
- case 11:
1790
- if (!!wholeAfterFns_2_1.done) return [3 /*break*/, 14];
1791
- after_2 = wholeAfterFns_2_1.value;
1792
- return [4 /*yield*/, after_2()];
1793
- case 12:
1794
- _h.sent();
1795
- _h.label = 13;
1796
- case 13:
1797
- wholeAfterFns_2_1 = wholeAfterFns_2.next();
1798
- return [3 /*break*/, 11];
1799
- case 14: return [3 /*break*/, 17];
1800
- case 15:
1801
- e_10_1 = _h.sent();
1802
- e_10 = { error: e_10_1 };
1803
- return [3 /*break*/, 17];
1804
- case 16:
1805
- try {
1806
- if (wholeAfterFns_2_1 && !wholeAfterFns_2_1.done && (_g = wholeAfterFns_2.return)) _g.call(wholeAfterFns_2);
1807
- }
1808
- finally { if (e_10) throw e_10.error; }
1809
- return [7 /*endfinally*/];
1810
- case 17: return [2 /*return*/, result];
1811
- }
1812
- });
1813
- });
1814
- };
1815
- CascadeStore.prototype.cascadeSelect = function (entity, selection, context, option) {
1816
- var data = selection.data, filter = selection.filter, indexFrom = selection.indexFrom, count = selection.count, sorter = selection.sorter;
1817
- var _a = this.destructCascadeSelect(entity, data, context, this.cascadeSelect, this.aggregateSync, option), projection = _a.projection, cascadeSelectionFns = _a.cascadeSelectionFns;
1818
- var rows = this.selectAbjointRow(entity, {
1819
- data: projection,
1820
- filter: filter,
1821
- indexFrom: indexFrom,
1822
- count: count,
1823
- sorter: sorter
1824
- }, context, option);
1825
- if (cascadeSelectionFns.length > 0) {
1826
- var ruException_1 = [];
1827
- cascadeSelectionFns.forEach(function (ele) {
1828
- try {
1829
- ele(rows);
1830
- }
1831
- catch (e) {
1832
- if (e instanceof types_1.OakRowUnexistedException) {
1833
- var rows_1 = e.getRows();
1834
- ruException_1.push.apply(ruException_1, tslib_1.__spreadArray([], tslib_1.__read(rows_1), false));
1835
- }
1836
- else {
1837
- throw e;
1838
- }
1839
- }
1840
- });
1841
- if (ruException_1.length > 0) {
1842
- throw new types_1.OakRowUnexistedException(ruException_1);
1843
- }
1844
- }
1845
- return rows;
1846
- };
1847
- /**
1848
- * 将一次查询的结果集加入result
1849
- * todo 如果是supportMtoOJoin,这里还要解构(未充分测试)
1850
- * @param entity
1851
- * @param rows
1852
- * @param context
1853
- */
1854
- CascadeStore.prototype.addToResultSelections = function (entity, rows, context) {
1855
- var e_12, _a;
1856
- if (this.supportManyToOneJoin()) {
1857
- // 这里的外键连接有可能为空,需要使用所有的行的attr的并集来测试
1858
- var attrs = (0, lodash_1.uniq)(rows.map(function (ele) { return Object.keys(ele); }).flat());
1859
- var attrsToPick_1 = [];
1860
- var _loop_3 = function (attr) {
1861
- var data = {};
1862
- var rel = this_3.judgeRelation(entity, attr);
1863
- if (rel === 2) {
1864
- this_3.addToResultSelections(attr, rows.map(function (ele) { return ele[attr]; }).filter(function (ele) { return !!ele; }), context);
1865
- }
1866
- else if (typeof rel === 'string') {
1867
- this_3.addToResultSelections(rel, rows.map(function (ele) { return ele[attr]; }).filter(function (ele) { return !!ele; }), context);
1868
- }
1869
- else if (rel instanceof Array) {
1870
- this_3.addToResultSelections(rel[0], rows.map(function (ele) { return ele[attr]; }).reduce(function (prev, current) { return prev.concat(current); }, []), context);
1871
- }
1872
- else {
1873
- attrsToPick_1.push(attr);
1874
- }
1875
- };
1876
- var this_3 = this;
1877
- try {
1878
- for (var attrs_1 = tslib_1.__values(attrs), attrs_1_1 = attrs_1.next(); !attrs_1_1.done; attrs_1_1 = attrs_1.next()) {
1879
- var attr = attrs_1_1.value;
1880
- _loop_3(attr);
1881
- }
1882
- }
1883
- catch (e_12_1) { e_12 = { error: e_12_1 }; }
1884
- finally {
1885
- try {
1886
- if (attrs_1_1 && !attrs_1_1.done && (_a = attrs_1.return)) _a.call(attrs_1);
1887
- }
1888
- finally { if (e_12) throw e_12.error; }
1889
- }
1890
- var originRows = rows.map(function (ele) { return (0, lodash_1.pick)(ele, attrsToPick_1); });
1891
- this.addSingleRowToResultSelections(entity, originRows, context);
1892
- }
1893
- else {
1894
- this.addSingleRowToResultSelections(entity, rows, context);
1895
- }
1896
- };
1897
- CascadeStore.prototype.addSingleRowToResultSelections = function (entity, rows, context) {
1898
- var _a;
1899
- var opRecords = context.opRecords;
1900
- var lastOperation = opRecords[opRecords.length - 1];
1901
- if (lastOperation && lastOperation.a === 's') {
1902
- var entityBranch_1 = lastOperation.d[entity];
1903
- if (entityBranch_1) {
1904
- rows.forEach(function (row) {
1905
- var _a;
1906
- if (row) {
1907
- (0, assert_1.default)(row.id);
1908
- var id = row.id;
1909
- if (!entityBranch_1[id]) {
1910
- Object.assign(entityBranch_1, (_a = {},
1911
- _a[id] = (0, lodash_1.cloneDeep)(row),
1912
- _a));
1913
- }
1914
- else {
1915
- Object.assign(entityBranch_1[id], (0, lodash_1.cloneDeep)(row));
1916
- }
1917
- }
1918
- });
1919
- return;
1920
- }
1921
- }
1922
- else {
1923
- lastOperation = {
1924
- a: 's',
1925
- d: {},
1926
- };
1927
- opRecords.push(lastOperation);
1928
- }
1929
- var entityBranch = {};
1930
- rows.forEach(function (row) {
1931
- var _a;
1932
- if (row) {
1933
- var id = row.id;
1934
- Object.assign(entityBranch, (_a = {},
1935
- _a[id] = (0, lodash_1.cloneDeep)(row),
1936
- _a));
1937
- }
1938
- });
1939
- Object.assign(lastOperation.d, (_a = {},
1940
- _a[entity] = entityBranch,
1941
- _a));
1942
- };
1943
- CascadeStore.prototype.cascadeSelectAsync = function (entity, selection, context, option) {
1944
- return tslib_1.__awaiter(this, void 0, void 0, function () {
1945
- var data, filter, indexFrom, count, sorter, _a, projection, cascadeSelectionFns, rows, ruException_2;
1946
- var _this = this;
1947
- return tslib_1.__generator(this, function (_b) {
1948
- switch (_b.label) {
1949
- case 0:
1950
- data = selection.data, filter = selection.filter, indexFrom = selection.indexFrom, count = selection.count, sorter = selection.sorter;
1951
- _a = this.destructCascadeSelect(entity, data, context, this.cascadeSelectAsync, this.aggregateAsync, option), projection = _a.projection, cascadeSelectionFns = _a.cascadeSelectionFns;
1952
- return [4 /*yield*/, this.selectAbjointRowAsync(entity, {
1953
- data: projection,
1954
- filter: filter,
1955
- indexFrom: indexFrom,
1956
- count: count,
1957
- sorter: sorter
1958
- }, context, option)];
1959
- case 1:
1960
- rows = _b.sent();
1961
- if (!option.dontCollect) {
1962
- this.addToResultSelections(entity, rows, context);
1963
- }
1964
- if (!(cascadeSelectionFns.length > 0)) return [3 /*break*/, 3];
1965
- ruException_2 = [];
1966
- return [4 /*yield*/, Promise.all(cascadeSelectionFns.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1967
- var e_13, rows_2;
1968
- return tslib_1.__generator(this, function (_a) {
1969
- switch (_a.label) {
1970
- case 0:
1971
- _a.trys.push([0, 2, , 3]);
1972
- return [4 /*yield*/, ele(rows)];
1973
- case 1:
1974
- _a.sent();
1975
- return [3 /*break*/, 3];
1976
- case 2:
1977
- e_13 = _a.sent();
1978
- if (e_13 instanceof types_1.OakRowUnexistedException) {
1979
- rows_2 = e_13.getRows();
1980
- ruException_2.push.apply(ruException_2, tslib_1.__spreadArray([], tslib_1.__read(rows_2), false));
1981
- }
1982
- else {
1983
- throw e_13;
1984
- }
1985
- return [3 /*break*/, 3];
1986
- case 3: return [2 /*return*/];
1987
- }
1988
- });
1989
- }); }))];
1990
- case 2:
1991
- _b.sent();
1992
- if (ruException_2.length > 0) {
1993
- throw new types_1.OakRowUnexistedException(ruException_2);
1994
- }
1995
- _b.label = 3;
1996
- case 3: return [2 /*return*/, rows];
1997
- }
1998
- });
1999
- });
2000
- };
2001
- CascadeStore.prototype.selectAsync = function (entity, selection, context, option) {
2002
- return tslib_1.__awaiter(this, void 0, void 0, function () {
2003
- return tslib_1.__generator(this, function (_a) {
2004
- switch (_a.label) {
2005
- case 0: return [4 /*yield*/, this.reinforceSelectionAsync(entity, selection, context, option)];
2006
- case 1:
2007
- _a.sent();
2008
- return [2 /*return*/, this.cascadeSelectAsync(entity, selection, context, option)];
2009
- }
2010
- });
2011
- });
2012
- };
2013
- CascadeStore.prototype.selectSync = function (entity, selection, context, option) {
2014
- this.reinforceSelectionSync(entity, selection, context, option);
2015
- return this.cascadeSelect(entity, selection, context, option);
2016
- };
2017
- CascadeStore.prototype.operateSync = function (entity, operation, context, option) {
2018
- //this.reinforceOperation(entity, operation); // 感觉前台可以无视?
2019
- return this.cascadeUpdate(entity, operation, context, option);
2020
- };
2021
- CascadeStore.prototype.operateAsync = function (entity, operation, context, option) {
2022
- return tslib_1.__awaiter(this, void 0, void 0, function () {
2023
- return tslib_1.__generator(this, function (_a) {
2024
- switch (_a.label) {
2025
- case 0: return [4 /*yield*/, this.reinforceOperation(entity, operation, context)];
2026
- case 1:
2027
- _a.sent();
2028
- return [2 /*return*/, this.cascadeUpdateAsync(entity, operation, context, option)];
2029
- }
2030
- });
2031
- });
2032
- };
2033
- return CascadeStore;
2034
- }(RowStore_1.RowStore));
2035
- 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 AsyncRowStore_1 = require("./AsyncRowStore");
13
+ var filter_2 = require("./filter");
14
+ var uuid_1 = require("../utils/uuid");
15
+ var env_1 = require("../compiler/env");
16
+ /**这个用来处理级联的select和update,对不同能力的 */
17
+ var CascadeStore = /** @class */ (function (_super) {
18
+ tslib_1.__extends(CascadeStore, _super);
19
+ function CascadeStore(storageSchema) {
20
+ var _this = _super.call(this, storageSchema) || this;
21
+ _this.selectionRewriters = [];
22
+ _this.operationRewriters = [];
23
+ return _this;
24
+ }
25
+ CascadeStore.prototype.reinforceSelectionAsync = function (entity, selection, context, option) {
26
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
27
+ var noRelationDestEntities, rewriterPromises;
28
+ var _this = this;
29
+ return tslib_1.__generator(this, function (_a) {
30
+ switch (_a.label) {
31
+ case 0:
32
+ noRelationDestEntities = [];
33
+ this.reinforceSelectionInner(entity, selection, context, noRelationDestEntities);
34
+ rewriterPromises = this.selectionRewriters.map(function (ele) { return ele(_this.getSchema(), entity, selection, context); });
35
+ if (!(rewriterPromises.length > 0)) return [3 /*break*/, 2];
36
+ return [4 /*yield*/, Promise.all(rewriterPromises)];
37
+ case 1:
38
+ _a.sent();
39
+ _a.label = 2;
40
+ case 2: return [2 /*return*/];
41
+ }
42
+ });
43
+ });
44
+ };
45
+ CascadeStore.prototype.reinforceSelectionSync = function (entity, selection, context, option) {
46
+ var _this = this;
47
+ this.reinforceSelectionInner(entity, selection, context, []);
48
+ var rewriterPromises = this.selectionRewriters.map(function (ele) { return ele(_this.getSchema(), entity, selection, context); });
49
+ };
50
+ CascadeStore.prototype.reinforceSelectionInner = function (entity, selection, context, noRelationDestEntities) {
51
+ var _this = this;
52
+ var filter = selection.filter, data = selection.data, sorter = selection.sorter;
53
+ var checkNode = function (projectionNode, attrs) {
54
+ attrs.forEach(function (attr) {
55
+ var _a;
56
+ if (!projectionNode.hasOwnProperty(attr)) {
57
+ Object.assign(projectionNode, (_a = {},
58
+ _a[attr] = 1,
59
+ _a));
60
+ }
61
+ });
62
+ };
63
+ var relevantIds = [];
64
+ if (filter) {
65
+ var toBeAssignNode_1 = {}; // 用来记录在表达式中涉及到的结点
66
+ // filter当中所关联到的属性必须在projection中
67
+ var filterNodeDict_1 = {};
68
+ var checkFilterNode_1 = function (entity2, filterNode, projectionNode) {
69
+ var _a, e_1, _b, _c, _d, _e, _f;
70
+ var necessaryAttrs = ['id'];
71
+ for (var attr in filterNode) {
72
+ if (attr === '#id') {
73
+ (0, assert_1.default)(!filterNodeDict_1[filterNode[attr]], "projection\u4E2D\u7ED3\u70B9\u7684id\u6709\u91CD\u590D, ".concat(filterNode[attr]));
74
+ Object.assign(filterNodeDict_1, (_a = {},
75
+ _a[filterNode[attr]] = projectionNode,
76
+ _a));
77
+ if (toBeAssignNode_1[filterNode[attr]]) {
78
+ checkNode(projectionNode, toBeAssignNode_1[filterNode[attr]]);
79
+ }
80
+ }
81
+ else if (['$and', '$or'].includes(attr)) {
82
+ try {
83
+ for (var _g = (e_1 = void 0, tslib_1.__values(filterNode[attr])), _h = _g.next(); !_h.done; _h = _g.next()) {
84
+ var node = _h.value;
85
+ checkFilterNode_1(entity2, node, projectionNode);
86
+ }
87
+ }
88
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
89
+ finally {
90
+ try {
91
+ if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
92
+ }
93
+ finally { if (e_1) throw e_1.error; }
94
+ }
95
+ }
96
+ else if (attr === '$not') {
97
+ checkFilterNode_1(entity2, filterNode[attr], projectionNode);
98
+ }
99
+ else if (attr === '$text') {
100
+ // 全文检索首先要有fulltext索引,其次要把fulltext的相关属性加到projection里
101
+ var indexes = _this.getSchema()[entity2].indexes;
102
+ var fulltextIndex = indexes.find(function (ele) { return ele.config && ele.config.type === 'fulltext'; });
103
+ var attributes = fulltextIndex.attributes;
104
+ necessaryAttrs.push.apply(necessaryAttrs, tslib_1.__spreadArray([], tslib_1.__read((attributes.map(function (ele) { return ele.name; }))), false));
105
+ }
106
+ else {
107
+ if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
108
+ var exprResult = (0, types_1.getAttrRefInExpression)(filterNode[attr]);
109
+ for (var nodeName in exprResult) {
110
+ if (nodeName === '#current') {
111
+ checkNode(projectionNode, exprResult[nodeName]);
112
+ }
113
+ else if (filterNodeDict_1[nodeName]) {
114
+ checkNode(filterNodeDict_1[nodeName], exprResult[nodeName]);
115
+ }
116
+ else {
117
+ if (toBeAssignNode_1[nodeName]) {
118
+ (_c = toBeAssignNode_1[nodeName]).push.apply(_c, tslib_1.__spreadArray([], tslib_1.__read(exprResult[nodeName]), false));
119
+ }
120
+ else {
121
+ Object.assign(toBeAssignNode_1, (_d = {},
122
+ _d[nodeName] = exprResult[nodeName],
123
+ _d));
124
+ }
125
+ }
126
+ }
127
+ }
128
+ else {
129
+ var rel = _this.judgeRelation(entity2, attr);
130
+ if (rel === 1) {
131
+ necessaryAttrs.push(attr);
132
+ }
133
+ else if (rel === 2) {
134
+ // entity/entityId反指
135
+ necessaryAttrs.push('entity', 'entityId');
136
+ if (!projectionNode[attr]) {
137
+ Object.assign(projectionNode, (_e = {},
138
+ _e[attr] = {
139
+ id: 1,
140
+ },
141
+ _e));
142
+ }
143
+ checkFilterNode_1(attr, filterNode[attr], projectionNode[attr]);
144
+ }
145
+ else if (typeof rel === 'string') {
146
+ necessaryAttrs.push("".concat(attr, "Id"));
147
+ if (!projectionNode[attr]) {
148
+ Object.assign(projectionNode, (_f = {},
149
+ _f[attr] = {
150
+ id: 1,
151
+ },
152
+ _f));
153
+ }
154
+ checkFilterNode_1(rel, filterNode[attr], projectionNode[attr]);
155
+ }
156
+ else if (rel instanceof Array) {
157
+ // 现在filter中还不支持一对多的语义,先放着吧
158
+ }
159
+ }
160
+ }
161
+ checkNode(projectionNode, necessaryAttrs);
162
+ }
163
+ };
164
+ checkFilterNode_1(entity, filter, data);
165
+ relevantIds = (0, filter_2.getRelevantIds)(filter);
166
+ }
167
+ // sorter感觉现在取不取影响不大,前端的list直接获取返回的ids了,先不管之
168
+ if (sorter) {
169
+ }
170
+ var toBeAssignNode2 = {}; // 用来记录在表达式中涉及到的结点
171
+ var projectionNodeDict = {};
172
+ var checkProjectionNode = function (entity2, projectionNode) {
173
+ var _a, _b, _c;
174
+ var necessaryAttrs = ['id', '$$createAt$$', '$$updateAt$$']; // 有的页面依赖于其它页面取数据,有时两个页面的filter的差异会导致有一个加createAt,有一个不加,此时可能产生前台取数据不完整的异常。先统一加上
175
+ for (var attr in projectionNode) {
176
+ if (attr === '#id') {
177
+ (0, assert_1.default)(!projectionNodeDict[projectionNode[attr]], "projection\u4E2D\u7ED3\u70B9\u7684id\u6709\u91CD\u590D, ".concat(projectionNode[attr]));
178
+ Object.assign(projectionNodeDict, (_a = {},
179
+ _a[projectionNode[attr]] = projectionNode,
180
+ _a));
181
+ if (toBeAssignNode2[projectionNode[attr]]) {
182
+ checkNode(projectionNode, toBeAssignNode2[projectionNode[attr]]);
183
+ }
184
+ }
185
+ else {
186
+ if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
187
+ var exprResult = (0, types_1.getAttrRefInExpression)(projectionNode[attr]);
188
+ for (var nodeName in exprResult) {
189
+ if (nodeName === '#current') {
190
+ checkNode(projectionNode, exprResult[nodeName]);
191
+ }
192
+ else if (projectionNodeDict[nodeName]) {
193
+ checkNode(projectionNodeDict[nodeName], exprResult[nodeName]);
194
+ }
195
+ else {
196
+ if (toBeAssignNode2[nodeName]) {
197
+ (_b = toBeAssignNode2[nodeName]).push.apply(_b, tslib_1.__spreadArray([], tslib_1.__read(exprResult[nodeName]), false));
198
+ }
199
+ else {
200
+ Object.assign(toBeAssignNode2, (_c = {},
201
+ _c[nodeName] = exprResult[nodeName],
202
+ _c));
203
+ }
204
+ }
205
+ }
206
+ }
207
+ else {
208
+ var rel = (0, relation_1.judgeRelation)(_this.getSchema(), entity2, attr);
209
+ if (rel === 1) {
210
+ necessaryAttrs.push(attr);
211
+ }
212
+ else if (rel === 2) {
213
+ // entity/entityId反指
214
+ necessaryAttrs.push('entity', 'entityId');
215
+ checkProjectionNode(attr, projectionNode[attr]);
216
+ }
217
+ else if (typeof rel === 'string') {
218
+ necessaryAttrs.push("".concat(attr, "Id"));
219
+ checkProjectionNode(rel, projectionNode[attr]);
220
+ }
221
+ else if (rel instanceof Array && !attr.endsWith('$$aggr')) {
222
+ var data_1 = projectionNode[attr].data;
223
+ if (rel[1]) {
224
+ checkNode(data_1, [rel[1]]);
225
+ }
226
+ else {
227
+ checkNode(data_1, ['entity', 'entityId']);
228
+ }
229
+ _this.reinforceSelectionInner(rel[0], projectionNode[attr], context, noRelationDestEntities);
230
+ }
231
+ }
232
+ }
233
+ checkNode(projectionNode, necessaryAttrs);
234
+ }
235
+ // 如果对象中指向一对多的Modi,此时加上指向Modi的projection
236
+ if (_this.getSchema()[entity2].toModi) {
237
+ Object.assign(projectionNode, {
238
+ modi$entity: {
239
+ $entity: 'modi',
240
+ data: {
241
+ id: 1,
242
+ targetEntity: 1,
243
+ entity: 1,
244
+ entityId: 1,
245
+ action: 1,
246
+ iState: 1,
247
+ data: 1,
248
+ filter: 1,
249
+ },
250
+ filter: {
251
+ iState: 'active',
252
+ },
253
+ }
254
+ });
255
+ }
256
+ // 如果对象上有relation关系,在此将本用户相关的relation和actionAuth全部取出
257
+ // 还要将actionAuth上没有relation关系但destEntity为本对象的行也全部取出,这些是指向userId的可能路径
258
+ // 放在这里有点怪异,暂先这样
259
+ if (context instanceof AsyncRowStore_1.AsyncContext) {
260
+ var userId = context.getCurrentUserId(true);
261
+ if (userId && !env_1.SYSTEM_RESERVE_ENTITIES.includes(entity2)) {
262
+ if (_this.getSchema()[entity2].relation && !projectionNode.userRelation$entity) {
263
+ Object.assign(projectionNode, {
264
+ userRelation$entity: {
265
+ $entity: 'userRelation',
266
+ data: {
267
+ id: 1,
268
+ entity: 1,
269
+ entityId: 1,
270
+ userId: 1,
271
+ relationId: 1,
272
+ relation: {
273
+ id: 1,
274
+ name: 1,
275
+ display: 1,
276
+ }
277
+ },
278
+ filter: {
279
+ userId: userId,
280
+ },
281
+ },
282
+ });
283
+ }
284
+ noRelationDestEntities.push(entity2);
285
+ }
286
+ }
287
+ };
288
+ checkProjectionNode(entity, data);
289
+ if (!sorter && relevantIds.length === 0) {
290
+ // 如果没有sorter,就给予一个按createAt逆序的sorter
291
+ Object.assign(selection, {
292
+ sorter: [
293
+ {
294
+ $attr: {
295
+ $$createAt$$: 1,
296
+ },
297
+ $direction: 'desc',
298
+ }
299
+ ]
300
+ });
301
+ Object.assign(data, {
302
+ $$createAt$$: 1,
303
+ });
304
+ }
305
+ };
306
+ CascadeStore.prototype.reinforceOperation = function (entity, operation, context) {
307
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
308
+ var _this = this;
309
+ return tslib_1.__generator(this, function (_a) {
310
+ switch (_a.label) {
311
+ case 0: return [4 /*yield*/, Promise.all(this.operationRewriters.map(function (ele) { return ele(_this.getSchema(), entity, operation, context); }))];
312
+ case 1:
313
+ _a.sent();
314
+ return [2 /*return*/];
315
+ }
316
+ });
317
+ });
318
+ };
319
+ CascadeStore.prototype.registerOperationRewriter = function (rewriter) {
320
+ this.operationRewriters.push(rewriter);
321
+ };
322
+ CascadeStore.prototype.registerSelectionRewriter = function (rewriter) {
323
+ this.selectionRewriters.push(rewriter);
324
+ };
325
+ CascadeStore.prototype.destructCascadeSelect = function (entity, projection2, context, cascadeSelectFn, aggregateFn, option) {
326
+ var _this = this;
327
+ var projection = {};
328
+ var cascadeSelectionFns = [];
329
+ var supportMtoJoin = this.supportManyToOneJoin();
330
+ var toModi = this.getSchema()[entity].toModi;
331
+ (0, assert_1.default)(typeof projection2 === 'object');
332
+ var _loop_1 = function (attr) {
333
+ var _a, _b, _c, _d;
334
+ var relation = (0, relation_1.judgeRelation)(this_1.storageSchema, entity, attr);
335
+ if (relation === 1 || relation == 0) {
336
+ Object.assign(projection, (_a = {},
337
+ _a[attr] = projection2[attr],
338
+ _a));
339
+ }
340
+ else if (relation === 2) {
341
+ // 基于entity/entityId的多对一
342
+ Object.assign(projection, {
343
+ entity: 1,
344
+ entityId: 1,
345
+ });
346
+ (0, assert_1.default)(typeof projection2[attr] === 'object');
347
+ if (supportMtoJoin) {
348
+ cascadeSelectionFns.push(function (result) {
349
+ if (!toModi) {
350
+ result.forEach(function (ele) {
351
+ if (ele.entity === attr) {
352
+ (0, assert_1.default)(ele.entityId);
353
+ if (!ele[attr]) {
354
+ throw new types_1.OakRowUnexistedException([{
355
+ entity: attr,
356
+ selection: {
357
+ data: projection2[attr],
358
+ filter: {
359
+ id: ele.entityId,
360
+ }
361
+ }
362
+ }]);
363
+ }
364
+ }
365
+ });
366
+ }
367
+ });
368
+ var _e = this_1.destructCascadeSelect(attr, projection2[attr], context, cascadeSelectFn, aggregateFn, option), subProjection = _e.projection, subCascadeSelectionFns = _e.cascadeSelectionFns;
369
+ Object.assign(projection, (_b = {},
370
+ _b[attr] = subProjection,
371
+ _b));
372
+ subCascadeSelectionFns.forEach(function (ele) { return cascadeSelectionFns.push(function (result) {
373
+ return ele(result.map(function (ele2) { return ele2[attr]; }).filter(function (ele2) { return !!ele2; }));
374
+ }); });
375
+ }
376
+ else {
377
+ cascadeSelectionFns.push(function (result) {
378
+ var entityIds = (0, lodash_1.uniq)(result.filter(function (ele) { return ele.entity === attr; }).map(function (ele) {
379
+ (0, assert_1.default)(ele.entityId !== null);
380
+ return ele.entityId;
381
+ }));
382
+ var dealWithSubRows = function (subRows) {
383
+ (0, assert_1.default)(subRows.length <= entityIds.length);
384
+ if (subRows.length < entityIds.length && !toModi) {
385
+ // 后台不允许数据不一致
386
+ if (context instanceof AsyncRowStore_1.AsyncContext || !option.ignoreForeignKeyMiss) {
387
+ throw new types_1.OakRowUnexistedException([{
388
+ entity: attr,
389
+ selection: {
390
+ data: projection2[attr],
391
+ filter: {
392
+ id: {
393
+ $in: entityIds
394
+ },
395
+ },
396
+ },
397
+ }]);
398
+ }
399
+ }
400
+ result.forEach(function (ele) {
401
+ var _a, _b;
402
+ if (ele.entity === attr) {
403
+ var subRow = subRows.find(function (ele2) { return ele2.id === ele.entityId; });
404
+ if (subRow) {
405
+ Object.assign(ele, (_a = {},
406
+ _a[attr] = subRow,
407
+ _a));
408
+ }
409
+ else {
410
+ Object.assign(ele, (_b = {},
411
+ _b[attr] = null,
412
+ _b));
413
+ }
414
+ }
415
+ });
416
+ };
417
+ if (entityIds.length > 0) {
418
+ var subRows = cascadeSelectFn.call(_this, attr, {
419
+ data: projection2[attr],
420
+ filter: {
421
+ id: {
422
+ $in: entityIds
423
+ },
424
+ },
425
+ }, context, option);
426
+ if (subRows instanceof Promise) {
427
+ return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
428
+ }
429
+ else {
430
+ dealWithSubRows(subRows);
431
+ }
432
+ }
433
+ });
434
+ }
435
+ }
436
+ else if (typeof relation === 'string') {
437
+ Object.assign(projection, (_c = {},
438
+ _c["".concat(attr, "Id")] = 1,
439
+ _c));
440
+ (0, assert_1.default)(typeof projection2[attr] === 'object');
441
+ if (supportMtoJoin) {
442
+ if (!toModi) {
443
+ // 如果不是modi,要保证外键没有空指针
444
+ cascadeSelectionFns.push(function (result) {
445
+ if (!toModi) {
446
+ result.forEach(function (ele) {
447
+ if (ele["".concat(attr, "Id")] && !ele[attr]) {
448
+ throw new types_1.OakRowUnexistedException([{
449
+ entity: relation,
450
+ selection: {
451
+ data: projection2[attr],
452
+ filter: {
453
+ id: ele["".concat(attr, "Id")],
454
+ }
455
+ }
456
+ }]);
457
+ }
458
+ });
459
+ }
460
+ });
461
+ }
462
+ var _f = this_1.destructCascadeSelect(relation, projection2[attr], context, cascadeSelectFn, aggregateFn, option), subProjection = _f.projection, subCascadeSelectionFns = _f.cascadeSelectionFns;
463
+ Object.assign(projection, (_d = {},
464
+ _d[attr] = subProjection,
465
+ _d));
466
+ subCascadeSelectionFns.forEach(function (ele) { return cascadeSelectionFns.push(function (result) {
467
+ return ele(result.map(function (ele2) { return ele2[attr]; }).filter(function (ele2) { return !!ele2; }));
468
+ }); });
469
+ }
470
+ else {
471
+ cascadeSelectionFns.push(function (result) {
472
+ var ids = (0, lodash_1.uniq)(result.filter(function (ele) { return !!(ele["".concat(attr, "Id")]); }).map(function (ele) { return ele["".concat(attr, "Id")]; }));
473
+ var dealWithSubRows = function (subRows) {
474
+ (0, assert_1.default)(subRows.length <= ids.length);
475
+ if (subRows.length < ids.length && !toModi) {
476
+ if (context instanceof AsyncRowStore_1.AsyncContext || !option.ignoreForeignKeyMiss) {
477
+ throw new types_1.OakRowUnexistedException([{
478
+ entity: relation,
479
+ selection: {
480
+ data: projection2[attr],
481
+ filter: {
482
+ id: {
483
+ $in: ids
484
+ },
485
+ },
486
+ }
487
+ }]);
488
+ }
489
+ }
490
+ result.forEach(function (ele) {
491
+ var _a, _b, _c;
492
+ if (ele["".concat(attr, "Id")]) {
493
+ var subRow = subRows.find(function (ele2) { return ele2.id === ele["".concat(attr, "Id")]; });
494
+ if (subRow) {
495
+ Object.assign(ele, (_a = {},
496
+ _a[attr] = subRow,
497
+ _a));
498
+ }
499
+ else {
500
+ Object.assign(ele, (_b = {},
501
+ _b[attr] = null,
502
+ _b));
503
+ }
504
+ }
505
+ else {
506
+ Object.assign(ele, (_c = {},
507
+ _c[attr] = null,
508
+ _c));
509
+ }
510
+ });
511
+ };
512
+ if (ids.length > 0) {
513
+ var subRows = cascadeSelectFn.call(_this, relation, {
514
+ data: projection2[attr],
515
+ filter: {
516
+ id: {
517
+ $in: ids
518
+ },
519
+ },
520
+ }, context, option);
521
+ if (subRows instanceof Promise) {
522
+ return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
523
+ }
524
+ dealWithSubRows(subRows);
525
+ }
526
+ });
527
+ }
528
+ }
529
+ else {
530
+ (0, assert_1.default)(relation instanceof Array);
531
+ var _g = projection2[attr], subProjection_1 = _g.data, subFilter_1 = _g.filter, indexFrom_1 = _g.indexFrom, count_1 = _g.count, subSorter_1 = _g.sorter;
532
+ var _h = tslib_1.__read(relation, 2), entity2_1 = _h[0], foreignKey_1 = _h[1];
533
+ var isAggr = attr.endsWith('$$aggr');
534
+ if (foreignKey_1) {
535
+ // 基于属性的一对多
536
+ if (isAggr) {
537
+ // 是聚合运算,只有后台才需要执行
538
+ (context instanceof AsyncRowStore_1.AsyncContext) && cascadeSelectionFns.push(function (result) {
539
+ var aggrResults = result.map(function (row) {
540
+ var _a, _b;
541
+ var aggrResult = aggregateFn.call(_this, entity2_1, {
542
+ data: subProjection_1,
543
+ filter: (0, filter_1.combineFilters)(entity2_1, _this.getSchema(), [(_a = {},
544
+ _a[foreignKey_1] = row.id,
545
+ _a), subFilter_1]),
546
+ sorter: subSorter_1,
547
+ indexFrom: indexFrom_1,
548
+ count: count_1
549
+ }, context, option);
550
+ if (aggrResult instanceof Promise) {
551
+ return aggrResult.then(function (aggrResultResult) {
552
+ var _a;
553
+ return Object.assign(row, (_a = {},
554
+ _a[attr] = aggrResultResult,
555
+ _a));
556
+ });
557
+ }
558
+ else {
559
+ Object.assign(row, (_b = {},
560
+ _b[attr] = aggrResult,
561
+ _b));
562
+ }
563
+ });
564
+ if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
565
+ return Promise.all(aggrResults).then(function () { return undefined; });
566
+ }
567
+ });
568
+ }
569
+ else {
570
+ // 是一对多查询
571
+ cascadeSelectionFns.push(function (result) {
572
+ var _a;
573
+ var ids = result.map(function (ele) { return ele.id; });
574
+ var dealWithSubRows = function (subRows) {
575
+ var _a;
576
+ // 这里如果result只有一行,则把返回结果直接置上,不对比外键值
577
+ // 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
578
+ if (result.length == 1) {
579
+ Object.assign(result[0], (_a = {},
580
+ _a[attr] = subRows,
581
+ _a));
582
+ }
583
+ else {
584
+ result.forEach(function (ele) {
585
+ var _a;
586
+ var subRowss = subRows.filter(function (ele2) { return ele2[foreignKey_1] === ele.id; });
587
+ (0, assert_1.default)(subRowss);
588
+ Object.assign(ele, (_a = {},
589
+ _a[attr] = subRowss,
590
+ _a));
591
+ });
592
+ }
593
+ };
594
+ if (ids.length > 0) {
595
+ var subRows = cascadeSelectFn.call(_this, entity2_1, {
596
+ data: subProjection_1,
597
+ filter: (0, filter_1.combineFilters)(entity2_1, _this.getSchema(), [(_a = {},
598
+ _a[foreignKey_1] = {
599
+ $in: ids,
600
+ },
601
+ _a), subFilter_1]),
602
+ sorter: subSorter_1,
603
+ indexFrom: indexFrom_1,
604
+ count: count_1
605
+ }, context, option);
606
+ if (subRows instanceof Promise) {
607
+ return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
608
+ }
609
+ dealWithSubRows(subRows);
610
+ }
611
+ });
612
+ }
613
+ }
614
+ else {
615
+ // 基于entity的多对一
616
+ if (isAggr) {
617
+ // 是聚合运算,只有后台才需要执行
618
+ (context instanceof AsyncRowStore_1.AsyncContext) && cascadeSelectionFns.push(function (result) {
619
+ var aggrResults = result.map(function (row) {
620
+ var _a;
621
+ var aggrResult = aggregateFn.call(_this, entity2_1, {
622
+ data: subProjection_1,
623
+ filter: (0, filter_1.combineFilters)(entity2_1, _this.getSchema(), [{
624
+ entity: entity,
625
+ entityId: row.id,
626
+ }, subFilter_1]),
627
+ sorter: subSorter_1,
628
+ indexFrom: indexFrom_1,
629
+ count: count_1
630
+ }, context, option);
631
+ if (aggrResult instanceof Promise) {
632
+ return aggrResult.then(function (aggrResultResult) {
633
+ var _a;
634
+ return Object.assign(row, (_a = {},
635
+ _a[attr] = aggrResultResult,
636
+ _a));
637
+ });
638
+ }
639
+ else {
640
+ Object.assign(row, (_a = {},
641
+ _a[attr] = aggrResult,
642
+ _a));
643
+ }
644
+ });
645
+ if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
646
+ return Promise.all(aggrResults).then(function () { return undefined; });
647
+ }
648
+ });
649
+ }
650
+ else {
651
+ // 是一对多查询
652
+ cascadeSelectionFns.push(function (result) {
653
+ var ids = result.map(function (ele) { return ele.id; });
654
+ var dealWithSubRows = function (subRows) {
655
+ var _a;
656
+ // 这里如果result只有一行,则把返回结果直接置上,不对比外键值
657
+ // 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
658
+ if (result.length === 1) {
659
+ Object.assign(result[0], (_a = {},
660
+ _a[attr] = subRows,
661
+ _a));
662
+ }
663
+ else {
664
+ result.forEach(function (ele) {
665
+ var _a;
666
+ var subRowss = subRows.filter(function (ele2) { return ele2.entityId === ele.id; });
667
+ (0, assert_1.default)(subRowss);
668
+ Object.assign(ele, (_a = {},
669
+ _a[attr] = subRowss,
670
+ _a));
671
+ });
672
+ }
673
+ };
674
+ if (ids.length > 0) {
675
+ var subRows = cascadeSelectFn.call(_this, entity2_1, {
676
+ data: subProjection_1,
677
+ filter: (0, filter_1.combineFilters)(entity2_1, _this.getSchema(), [{
678
+ entity: entity,
679
+ entityId: {
680
+ $in: ids,
681
+ }
682
+ }, subFilter_1]),
683
+ sorter: subSorter_1,
684
+ indexFrom: indexFrom_1,
685
+ count: count_1
686
+ }, context, option);
687
+ if (subRows instanceof Promise) {
688
+ return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
689
+ }
690
+ dealWithSubRows(subRows);
691
+ }
692
+ });
693
+ }
694
+ }
695
+ }
696
+ };
697
+ var this_1 = this;
698
+ for (var attr in projection2) {
699
+ _loop_1(attr);
700
+ }
701
+ return {
702
+ projection: projection,
703
+ cascadeSelectionFns: cascadeSelectionFns,
704
+ };
705
+ };
706
+ /**
707
+ * 级联更新
708
+ * A --> B
709
+ 多对一:A CREATE/B CREATE,B data的主键赋到A的data上
710
+ A CREATE/B UPDATE,B filter的主键来自A的data
711
+ A UPDATE/B CREATE,B data的主键赋到A的data上
712
+ A UPDATE/B UPDATE,B filter的主键来自A的row
713
+ A UPDATE/B REMOVE,B filter的主键来自A的row
714
+ A REMOVE/B UPDATE,B filter的主键来自A的row
715
+ A REMOVE/B REMOVE,B filter的主键来自A的row
716
+
717
+ 一对多:A CREATE/B CREATE,A data上的主键赋到B的data上
718
+ A CREATE/B UPDATE,A data上的主键赋到B的data上
719
+ A UPDATE/B CREATE,A filter上的主键赋到B的data上(一定是带主键的filter)
720
+ A UPDATE/B UPDATE,A filter上的主键赋到B的filter上(一定是带主键的filter)
721
+ A UPDATE/B REMOVE,A filter上的主键赋到B的filter上(一定是带主键的filter)
722
+ A REMOVE/B UPDATE,A filter上的主键赋到B的filter上(且B关于A的外键清空)
723
+ A REMOVE/B REMOVE,A filter上的主键赋到B的filter上
724
+ *
725
+ * 延时更新,
726
+ * A(业务级别的申请对象) ---> B(业务级别需要更新的对象)
727
+ * 两者必须通过entity/entityId关联
728
+ * 此时需要把对B的更新记录成一条新插入的Modi对象,并将A上的entity/entityId指向该对象(新生成的Modi对象的id与此operation的id保持一致)
729
+ * @param entity
730
+ * @param action
731
+ * @param data
732
+ * @param context
733
+ * @param option
734
+ * @param result
735
+ * @param filter
736
+ * @returns
737
+ */
738
+ CascadeStore.prototype.destructCascadeUpdate = function (entity, action, data, context, option, cascadeUpdate, filter) {
739
+ var _this = this;
740
+ var modiAttr = this.getSchema()[entity].toModi;
741
+ var option2 = Object.assign({}, option);
742
+ var opData = {};
743
+ var beforeFns = [];
744
+ var afterFns = [];
745
+ if (modiAttr && action !== 'remove' && !option.dontCreateModi) {
746
+ // create/update具有modi对象的对象,对其子对象的update行为全部是create modi对象(缓存动作)
747
+ // delete此对象,所有的modi子对象应该通过触发器作废,这个目前先通过系统的trigger来实现
748
+ (0, assert_1.default)(!option2.modiParentId && !option2.modiParentEntity);
749
+ if (action === 'create') {
750
+ option2.modiParentId = data.id;
751
+ }
752
+ else {
753
+ (0, assert_1.default)((filter === null || filter === void 0 ? void 0 : filter.id) && typeof filter.id === 'string');
754
+ option2.modiParentId = filter.id;
755
+ }
756
+ option2.modiParentEntity = entity;
757
+ }
758
+ var _loop_2 = function (attr) {
759
+ var _a, _b, _c, _d, e_2, _e;
760
+ var relation = (0, relation_1.judgeRelation)(this_2.storageSchema, entity, attr);
761
+ if (relation === 1) {
762
+ Object.assign(opData, (_a = {},
763
+ _a[attr] = data[attr],
764
+ _a));
765
+ }
766
+ else if (relation === 2) {
767
+ // 基于entity/entityId的many-to-one
768
+ var operationMto_1 = data[attr];
769
+ var actionMto = operationMto_1.action, dataMto = operationMto_1.data, filterMto = operationMto_1.filter;
770
+ if (actionMto === 'create') {
771
+ Object.assign(opData, {
772
+ entityId: dataMto.id,
773
+ entity: attr,
774
+ });
775
+ }
776
+ else if (action === 'create') {
777
+ var fkId = data.entityId, entity_1 = data.entity;
778
+ (0, assert_1.default)(typeof fkId === 'string' || entity_1 === attr);
779
+ if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
780
+ // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
781
+ (0, assert_1.default)(filterMto.id === fkId);
782
+ }
783
+ else {
784
+ // A中data的entityId作为B中filter的主键
785
+ Object.assign(operationMto_1, {
786
+ filter: (0, filter_1.combineFilters)(attr, this_2.getSchema(), [{
787
+ id: fkId,
788
+ }, filterMto]),
789
+ });
790
+ }
791
+ }
792
+ else {
793
+ // 剩下三种情况都是B中的filter的id来自A中row的entityId
794
+ (0, assert_1.default)(!data.hasOwnProperty('entityId') && !data.hasOwnProperty('entity'));
795
+ if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
796
+ // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
797
+ (0, assert_1.default)(typeof filterMto.id === 'string');
798
+ }
799
+ else if (filter.entity === attr && filter.entityId) {
800
+ Object.assign(operationMto_1, {
801
+ filter: (0, filter_1.combineFilters)(attr, this_2.getSchema(), [{
802
+ id: filter.entityId,
803
+ }, filterMto]),
804
+ });
805
+ }
806
+ else if (filter[attr]) {
807
+ Object.assign(operationMto_1, {
808
+ filter: (0, filter_1.combineFilters)(attr, this_2.getSchema(), [filter[attr], filterMto]),
809
+ });
810
+ }
811
+ else {
812
+ // A中data的entityId作为B中filter的主键
813
+ Object.assign(operationMto_1, {
814
+ filter: (0, filter_1.combineFilters)(attr, this_2.getSchema(), [(_b = {},
815
+ _b["".concat(entity, "$entity")] = {
816
+ filter: filter,
817
+ },
818
+ _b), filterMto]),
819
+ });
820
+ }
821
+ }
822
+ beforeFns.push(function () { return cascadeUpdate.call(_this, attr, operationMto_1, context, option2); });
823
+ }
824
+ else if (typeof relation === 'string') {
825
+ // 基于attr的外键的many-to-one
826
+ var operationMto_2 = data[attr];
827
+ var actionMto = operationMto_2.action, dataMto = operationMto_2.data, filterMto = operationMto_2.filter;
828
+ if (actionMto === 'create') {
829
+ Object.assign(opData, (_c = {},
830
+ _c["".concat(attr, "Id")] = dataMto.id,
831
+ _c));
832
+ }
833
+ else if (action === 'create') {
834
+ var _f = data, _g = "".concat(attr, "Id"), fkId = _f[_g];
835
+ (0, assert_1.default)(typeof fkId === 'string');
836
+ if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
837
+ // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
838
+ (0, assert_1.default)(filterMto.id === fkId);
839
+ }
840
+ else {
841
+ // A中data的entityId作为B中filter的主键
842
+ Object.assign(operationMto_2, {
843
+ filter: (0, filter_1.combineFilters)(relation, this_2.getSchema(), [filterMto, {
844
+ id: fkId,
845
+ }]),
846
+ });
847
+ }
848
+ }
849
+ else {
850
+ (0, assert_1.default)(!data.hasOwnProperty("".concat(attr, "Id")));
851
+ if (filterMto === null || filterMto === void 0 ? void 0 : filterMto.id) {
852
+ // 若已有id则不用处理,否则会干扰modi的后续判断(会根据filter来判断对象id,如果判断不出来去查实际的对象,但实际的对象其实还未创建好)
853
+ (0, assert_1.default)(typeof filterMto.id === 'string');
854
+ }
855
+ else if (filter["".concat(attr, "Id")]) {
856
+ Object.assign(operationMto_2, {
857
+ filter: (0, filter_1.combineFilters)(relation, this_2.getSchema(), [filterMto, {
858
+ id: filter["".concat(attr, "Id")],
859
+ }]),
860
+ });
861
+ }
862
+ else if (filter[attr]) {
863
+ Object.assign(operationMto_2, {
864
+ filter: (0, filter_1.combineFilters)(relation, this_2.getSchema(), [filterMto, filter[attr]]),
865
+ });
866
+ }
867
+ else {
868
+ // A中data的attrId作为B中filter的主键
869
+ Object.assign(operationMto_2, {
870
+ filter: (0, filter_1.combineFilters)(relation, this_2.getSchema(), [filterMto, (_d = {},
871
+ _d["".concat(entity, "$").concat(attr)] = filter,
872
+ _d)]),
873
+ });
874
+ }
875
+ }
876
+ beforeFns.push(function () { return cascadeUpdate.call(_this, relation, operationMto_2, context, option2); });
877
+ }
878
+ else {
879
+ (0, assert_1.default)(relation instanceof Array);
880
+ var _h = tslib_1.__read(relation, 2), entityOtm_1 = _h[0], foreignKey_2 = _h[1];
881
+ var otmOperations = data[attr];
882
+ var dealWithOneToMany = function (otm) {
883
+ var _a, _b, _c, _d, _e, _f, _g;
884
+ var actionOtm = otm.action, dataOtm = otm.data, filterOtm = otm.filter;
885
+ if (!foreignKey_2) {
886
+ // 基于entity/entityId的one-to-many
887
+ if (action === 'create') {
888
+ var id_1 = data.id;
889
+ if (dataOtm instanceof Array) {
890
+ dataOtm.forEach(function (ele) { return Object.assign(ele, {
891
+ entity: entity,
892
+ entityId: id_1,
893
+ }); });
894
+ }
895
+ else {
896
+ Object.assign(dataOtm, {
897
+ entity: entity,
898
+ entityId: id_1,
899
+ });
900
+ }
901
+ }
902
+ else if (actionOtm === 'create') {
903
+ // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
904
+ // todo 这个假设对watcher等后台行为可能不成立,等遇到create/create一对多的case再完善
905
+ var id_2 = filter.id;
906
+ (0, assert_1.default)(typeof id_2 === 'string');
907
+ if (dataOtm instanceof Array) {
908
+ dataOtm.forEach(function (ele) { return Object.assign(ele, {
909
+ entity: entity,
910
+ entityId: id_2,
911
+ }); });
912
+ }
913
+ else {
914
+ Object.assign(dataOtm, {
915
+ entity: entity,
916
+ entityId: id_2,
917
+ });
918
+ }
919
+ }
920
+ else {
921
+ // 这里优化一下,如果filter上有id,直接更新成根据entityId来过滤
922
+ if (filter) {
923
+ if (filter.id && Object.keys(filter).length === 1) {
924
+ Object.assign(otm, {
925
+ filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [{
926
+ entity: entity,
927
+ entityId: filter.id,
928
+ }, filterOtm]),
929
+ });
930
+ }
931
+ else {
932
+ Object.assign(otm, {
933
+ filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [(_a = {},
934
+ _a[entity] = filter,
935
+ _a), filterOtm]),
936
+ });
937
+ }
938
+ }
939
+ else {
940
+ Object.assign(otm, {
941
+ filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [{
942
+ entity: entity,
943
+ entityId: {
944
+ $exists: true,
945
+ }
946
+ }, filterOtm])
947
+ });
948
+ }
949
+ if (action === 'remove' && actionOtm === 'update') {
950
+ Object.assign(dataOtm, {
951
+ entity: null,
952
+ entityId: null,
953
+ });
954
+ }
955
+ }
956
+ }
957
+ else {
958
+ // 基于foreignKey的one-to-many
959
+ if (action === 'create') {
960
+ var id_3 = data.id;
961
+ if (dataOtm instanceof Array) {
962
+ dataOtm.forEach(function (ele) {
963
+ var _a;
964
+ return Object.assign(ele, (_a = {},
965
+ _a[foreignKey_2] = id_3,
966
+ _a));
967
+ });
968
+ }
969
+ else {
970
+ Object.assign(dataOtm, (_b = {},
971
+ _b[foreignKey_2] = id_3,
972
+ _b));
973
+ }
974
+ }
975
+ else if (actionOtm === 'create') {
976
+ // 这里先假设A(必是update)的filter上一定有id,否则用户界面上应该设计不出来这样的操作
977
+ // todo 这个假设在后台可能不成立,等遇到了再说
978
+ var id_4 = filter.id;
979
+ (0, assert_1.default)(typeof id_4 === 'string');
980
+ if (dataOtm instanceof Array) {
981
+ dataOtm.forEach(function (ele) {
982
+ var _a;
983
+ return Object.assign(ele, (_a = {},
984
+ _a[foreignKey_2] = id_4,
985
+ _a));
986
+ });
987
+ }
988
+ else {
989
+ Object.assign(dataOtm, (_c = {},
990
+ _c[foreignKey_2] = id_4,
991
+ _c));
992
+ }
993
+ }
994
+ else {
995
+ // 这里优化一下,如果filter上有id,直接更新成根据entityId来过滤
996
+ if (filter) {
997
+ if (filter.id && Object.keys(filter).length === 1) {
998
+ Object.assign(otm, {
999
+ filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [(_d = {},
1000
+ _d[foreignKey_2] = filter.id,
1001
+ _d), filterOtm]),
1002
+ });
1003
+ }
1004
+ else {
1005
+ Object.assign(otm, {
1006
+ filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [(_e = {},
1007
+ _e[foreignKey_2.slice(0, foreignKey_2.length - 2)] = filter,
1008
+ _e), filterOtm]),
1009
+ });
1010
+ }
1011
+ }
1012
+ else {
1013
+ Object.assign(otm, {
1014
+ filter: (0, filter_1.combineFilters)(entityOtm_1, _this.getSchema(), [(_f = {},
1015
+ _f[foreignKey_2] = {
1016
+ $exists: true,
1017
+ },
1018
+ _f), filterOtm]),
1019
+ });
1020
+ }
1021
+ if (action === 'remove' && actionOtm === 'update') {
1022
+ Object.assign(dataOtm, (_g = {},
1023
+ _g[foreignKey_2] = null,
1024
+ _g));
1025
+ }
1026
+ }
1027
+ }
1028
+ // 一对多的依赖应该后建,否则中间会出现空指针,导致checker等出错
1029
+ afterFns.push(function () { return cascadeUpdate.call(_this, entityOtm_1, otm, context, option2); });
1030
+ };
1031
+ if (otmOperations instanceof Array) {
1032
+ try {
1033
+ for (var otmOperations_1 = (e_2 = void 0, tslib_1.__values(otmOperations)), otmOperations_1_1 = otmOperations_1.next(); !otmOperations_1_1.done; otmOperations_1_1 = otmOperations_1.next()) {
1034
+ var oper = otmOperations_1_1.value;
1035
+ dealWithOneToMany(oper);
1036
+ }
1037
+ }
1038
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1039
+ finally {
1040
+ try {
1041
+ if (otmOperations_1_1 && !otmOperations_1_1.done && (_e = otmOperations_1.return)) _e.call(otmOperations_1);
1042
+ }
1043
+ finally { if (e_2) throw e_2.error; }
1044
+ }
1045
+ }
1046
+ else {
1047
+ dealWithOneToMany(otmOperations);
1048
+ }
1049
+ }
1050
+ };
1051
+ var this_2 = this;
1052
+ for (var attr in data) {
1053
+ _loop_2(attr);
1054
+ }
1055
+ return {
1056
+ data: opData,
1057
+ beforeFns: beforeFns,
1058
+ afterFns: afterFns,
1059
+ };
1060
+ };
1061
+ // 对插入的数据,没有初始值的属性置null
1062
+ CascadeStore.prototype.preProcessDataCreated = function (entity, data) {
1063
+ var now = Date.now();
1064
+ var attributes = this.getSchema()[entity].attributes;
1065
+ var processSingle = function (data2) {
1066
+ var _a, _b;
1067
+ for (var key in attributes) {
1068
+ if (data2[key] === undefined) {
1069
+ Object.assign(data2, (_a = {},
1070
+ _a[key] = null,
1071
+ _a));
1072
+ }
1073
+ }
1074
+ Object.assign(data2, (_b = {},
1075
+ _b[Entity_1.CreateAtAttribute] = now,
1076
+ _b[Entity_1.UpdateAtAttribute] = now,
1077
+ _b[Entity_1.DeleteAtAttribute] = null,
1078
+ _b));
1079
+ };
1080
+ if (data instanceof Array) {
1081
+ data.forEach(function (ele) { return processSingle(ele); });
1082
+ }
1083
+ else {
1084
+ processSingle(data);
1085
+ }
1086
+ };
1087
+ // 对更新的数据,去掉所有的undefined属性
1088
+ CascadeStore.prototype.preProcessDataUpdated = function (data) {
1089
+ var undefinedKeys = Object.keys(data).filter(function (ele) { return data[ele] === undefined; });
1090
+ undefinedKeys.forEach(function (ele) { return (0, lodash_1.unset)(data, ele); });
1091
+ };
1092
+ CascadeStore.prototype.judgeRelation = function (entity, attr) {
1093
+ return (0, relation_1.judgeRelation)(this.storageSchema, entity, attr);
1094
+ };
1095
+ /**
1096
+ * 和具体的update过程无关的例程放在这里,包括对later动作的处理、对oper的记录以及对record的收集等
1097
+ * @param entity
1098
+ * @param operation
1099
+ * @param context
1100
+ * @param option
1101
+ */
1102
+ CascadeStore.prototype.doUpdateSingleRowAsync = function (entity, operation, context, option) {
1103
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
1104
+ var data, action, operId, filter, now, _a, modiCreate, closeRootMode, result_1, createInner, multipleCreate, data_2, data_2_1, d, createSingleOper, e_3_1, operatorId, createOper, _b, closeRootMode, ids_1, selection, rows, modiUpsert, upsertModis, _c, originData, originId, _d, closeRootMode, createOper, updateAttrCount, result;
1105
+ var e_3, _e, _f, _g, _h, _j, _k, _l;
1106
+ var _this = this;
1107
+ return tslib_1.__generator(this, function (_m) {
1108
+ switch (_m.label) {
1109
+ case 0:
1110
+ data = operation.data, action = operation.action, operId = operation.id, filter = operation.filter;
1111
+ now = Date.now();
1112
+ _a = action;
1113
+ switch (_a) {
1114
+ case 'create': return [3 /*break*/, 1];
1115
+ }
1116
+ return [3 /*break*/, 22];
1117
+ case 1:
1118
+ if (!(option.modiParentEntity && !['modi', 'modiEntity', 'oper', 'operEntity'].includes(entity))) return [3 /*break*/, 3];
1119
+ // 变成对modi的插入
1120
+ (0, assert_1.default)(option.modiParentId);
1121
+ modiCreate = {
1122
+ id: 'dummy',
1123
+ action: 'create',
1124
+ data: {
1125
+ id: operId,
1126
+ targetEntity: entity,
1127
+ action: action,
1128
+ entity: option.modiParentEntity,
1129
+ entityId: option.modiParentId,
1130
+ filter: {
1131
+ id: data.id, //这里记录这个filter是为了后面update的时候直接在其上面update,参见本函数后半段关于modiUpsert相关的优化
1132
+ },
1133
+ data: data,
1134
+ iState: 'active',
1135
+ },
1136
+ };
1137
+ closeRootMode = context.openRootMode();
1138
+ return [4 /*yield*/, this.cascadeUpdateAsync('modi', modiCreate, context, option)];
1139
+ case 2:
1140
+ _m.sent();
1141
+ closeRootMode();
1142
+ return [2 /*return*/, 1];
1143
+ case 3:
1144
+ this.preProcessDataCreated(entity, data);
1145
+ result_1 = 0;
1146
+ createInner = function (operation2) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1147
+ var _a, e_4;
1148
+ return tslib_1.__generator(this, function (_b) {
1149
+ switch (_b.label) {
1150
+ case 0:
1151
+ _b.trys.push([0, 2, , 3]);
1152
+ _a = result_1;
1153
+ return [4 /*yield*/, this.updateAbjointRowAsync(entity, operation2, context, option)];
1154
+ case 1:
1155
+ result_1 = _a + _b.sent();
1156
+ return [3 /*break*/, 3];
1157
+ case 2:
1158
+ e_4 = _b.sent();
1159
+ /* 这段代码是处理插入时有重复的行,现在看有问题,等实际需求出现再写
1160
+ if (e instanceof OakCongruentRowExists) {
1161
+ if (option.allowExists) {
1162
+ // 如果允许存在,对已存在行进行update,剩下的行继续insert
1163
+ const congruentRow = e.getData() as ED[T]['OpSchema'];
1164
+ if (data instanceof Array) {
1165
+ const rest = data.filter(
1166
+ ele => ele.id !== congruentRow.id
1167
+ );
1168
+ if (rest.length === data.length) {
1169
+ throw e;
1170
+ }
1171
+ const result2 = await this.updateAbjointRow(
1172
+ entity,
1173
+ Object.assign({}, operation, {
1174
+ data: rest,
1175
+ }),
1176
+ context,
1177
+ option
1178
+ );
1179
+
1180
+ const row = data.find(
1181
+ ele => ele.id === congruentRow.id
1182
+ );
1183
+ const updateData = omit(row, ['id', '$$createAt$$']);
1184
+ const result3 = await this.updateAbjointRow(
1185
+ entity,
1186
+ {
1187
+ id: await generateNewId(),
1188
+ action: 'update',
1189
+ data: updateData,
1190
+ filter: {
1191
+ id: congruentRow.id,
1192
+ } as any,
1193
+ },
1194
+ context,
1195
+ option
1196
+ );
1197
+
1198
+ return result2 + result3;
1199
+ }
1200
+ else {
1201
+ if (data.id !== congruentRow.id) {
1202
+ throw e;
1203
+ }
1204
+ const updateData = omit(data, ['id', '$$createAt$$']);
1205
+ const result2 = await this.updateAbjointRow(
1206
+ entity,
1207
+ {
1208
+ id: await generateNewId(),
1209
+ action: 'update',
1210
+ data: updateData,
1211
+ filter: {
1212
+ id: congruentRow.id,
1213
+ } as any,
1214
+ },
1215
+ context,
1216
+ option
1217
+ );
1218
+ return result2;
1219
+ }
1220
+ }
1221
+ } */
1222
+ throw e_4;
1223
+ case 3: return [2 /*return*/];
1224
+ }
1225
+ });
1226
+ }); };
1227
+ if (!(data instanceof Array)) return [3 /*break*/, 13];
1228
+ multipleCreate = this.supportMultipleCreate();
1229
+ if (!multipleCreate) return [3 /*break*/, 5];
1230
+ return [4 /*yield*/, createInner(operation)];
1231
+ case 4:
1232
+ _m.sent();
1233
+ return [3 /*break*/, 12];
1234
+ case 5:
1235
+ _m.trys.push([5, 10, 11, 12]);
1236
+ data_2 = tslib_1.__values(data), data_2_1 = data_2.next();
1237
+ _m.label = 6;
1238
+ case 6:
1239
+ if (!!data_2_1.done) return [3 /*break*/, 9];
1240
+ d = data_2_1.value;
1241
+ createSingleOper = {
1242
+ id: 'any',
1243
+ action: 'create',
1244
+ data: d,
1245
+ };
1246
+ return [4 /*yield*/, createInner(createSingleOper)];
1247
+ case 7:
1248
+ _m.sent();
1249
+ _m.label = 8;
1250
+ case 8:
1251
+ data_2_1 = data_2.next();
1252
+ return [3 /*break*/, 6];
1253
+ case 9: return [3 /*break*/, 12];
1254
+ case 10:
1255
+ e_3_1 = _m.sent();
1256
+ e_3 = { error: e_3_1 };
1257
+ return [3 /*break*/, 12];
1258
+ case 11:
1259
+ try {
1260
+ if (data_2_1 && !data_2_1.done && (_e = data_2.return)) _e.call(data_2);
1261
+ }
1262
+ finally { if (e_3) throw e_3.error; }
1263
+ return [7 /*endfinally*/];
1264
+ case 12: return [3 /*break*/, 15];
1265
+ case 13: return [4 /*yield*/, createInner(operation)];
1266
+ case 14:
1267
+ _m.sent();
1268
+ _m.label = 15;
1269
+ case 15:
1270
+ if (!option.dontCollect) {
1271
+ context.opRecords.push({
1272
+ a: 'c',
1273
+ e: entity,
1274
+ d: data,
1275
+ });
1276
+ }
1277
+ if (!(!option.dontCreateOper && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity))) return [3 /*break*/, 21];
1278
+ // 按照框架要求生成Oper和OperEntity这两个内置的对象
1279
+ (0, assert_1.default)(operId);
1280
+ operatorId = context.getCurrentUserId(true);
1281
+ if (!operatorId) return [3 /*break*/, 21];
1282
+ _f = {
1283
+ id: 'dummy',
1284
+ action: 'create'
1285
+ };
1286
+ _g = {
1287
+ id: operId,
1288
+ action: action,
1289
+ data: data,
1290
+ operatorId: operatorId,
1291
+ targetEntity: entity
1292
+ };
1293
+ if (!(data instanceof Array)) return [3 /*break*/, 17];
1294
+ _h = {
1295
+ id: 'dummy',
1296
+ action: 'create'
1297
+ };
1298
+ return [4 /*yield*/, Promise.all(data.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1299
+ var _a;
1300
+ return tslib_1.__generator(this, function (_b) {
1301
+ switch (_b.label) {
1302
+ case 0:
1303
+ _a = {};
1304
+ return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1305
+ case 1: return [2 /*return*/, (_a.id = _b.sent(),
1306
+ _a.entityId = ele.id,
1307
+ _a.entity = entity,
1308
+ _a)];
1309
+ }
1310
+ });
1311
+ }); }))];
1312
+ case 16:
1313
+ _b = (_h.data = _m.sent(),
1314
+ _h);
1315
+ return [3 /*break*/, 19];
1316
+ case 17:
1317
+ _j = {
1318
+ id: 'dummy',
1319
+ action: 'create'
1320
+ };
1321
+ _k = {};
1322
+ return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1323
+ case 18:
1324
+ _b = [(_j.data = (_k.id = _m.sent(),
1325
+ _k.entityId = data.id,
1326
+ _k.entity = entity,
1327
+ _k),
1328
+ _j)];
1329
+ _m.label = 19;
1330
+ case 19:
1331
+ createOper = (_f.data = (_g.operEntity$oper = _b,
1332
+ _g),
1333
+ _f);
1334
+ closeRootMode = context.openRootMode();
1335
+ return [4 /*yield*/, this.cascadeUpdateAsync('oper', createOper, context, {
1336
+ dontCollect: true,
1337
+ dontCreateOper: true,
1338
+ })];
1339
+ case 20:
1340
+ _m.sent();
1341
+ closeRootMode();
1342
+ _m.label = 21;
1343
+ case 21: return [2 /*return*/, result_1];
1344
+ case 22:
1345
+ ids_1 = (0, filter_2.getRelevantIds)(filter);
1346
+ if (!(ids_1.length === 0)) return [3 /*break*/, 24];
1347
+ selection = {
1348
+ data: {
1349
+ id: 1,
1350
+ },
1351
+ filter: operation.filter,
1352
+ indexFrom: operation.indexFrom,
1353
+ count: operation.count,
1354
+ };
1355
+ return [4 /*yield*/, this.selectAbjointRowAsync(entity, selection, context, {
1356
+ dontCollect: true,
1357
+ })];
1358
+ case 23:
1359
+ rows = _m.sent();
1360
+ ids_1.push.apply(ids_1, tslib_1.__spreadArray([], tslib_1.__read((rows.map(function (ele) { return ele.id; }))), false));
1361
+ _m.label = 24;
1362
+ case 24:
1363
+ if (data) {
1364
+ this.preProcessDataUpdated(data);
1365
+ }
1366
+ if (!(option.modiParentEntity && !['modi', 'modiEntity'].includes(entity))) return [3 /*break*/, 30];
1367
+ modiUpsert = void 0;
1368
+ if (!(action !== 'remove')) return [3 /*break*/, 26];
1369
+ return [4 /*yield*/, this.selectAbjointRowAsync('modi', {
1370
+ data: {
1371
+ id: 1,
1372
+ data: 1,
1373
+ },
1374
+ filter: {
1375
+ targetEntity: entity,
1376
+ action: {
1377
+ $in: ['create', 'update'],
1378
+ },
1379
+ entity: option.modiParentEntity,
1380
+ entityId: option.modiParentId,
1381
+ iState: 'active',
1382
+ filter: ids_1.length > 0 ? {
1383
+ id: {
1384
+ $in: ids_1,
1385
+ },
1386
+ } : filter,
1387
+ },
1388
+ sorter: [
1389
+ {
1390
+ $attr: {
1391
+ $$createAt$$: 1,
1392
+ },
1393
+ $direction: 'desc',
1394
+ }
1395
+ ],
1396
+ indexFrom: 0,
1397
+ count: 1,
1398
+ }, context, option)];
1399
+ case 25:
1400
+ upsertModis = _m.sent();
1401
+ if (upsertModis.length > 0) {
1402
+ _c = upsertModis[0], originData = _c.data, originId = _c.id;
1403
+ modiUpsert = {
1404
+ id: 'dummy',
1405
+ action: 'update',
1406
+ data: {
1407
+ data: Object.assign({}, originData, data),
1408
+ },
1409
+ filter: {
1410
+ id: originId,
1411
+ }
1412
+ };
1413
+ }
1414
+ _m.label = 26;
1415
+ case 26:
1416
+ if (!!modiUpsert) return [3 /*break*/, 28];
1417
+ modiUpsert = {
1418
+ id: 'dummy',
1419
+ action: 'create',
1420
+ data: {
1421
+ id: operId,
1422
+ targetEntity: entity,
1423
+ entity: option.modiParentEntity,
1424
+ entityId: option.modiParentId,
1425
+ action: action,
1426
+ data: data,
1427
+ iState: 'active',
1428
+ filter: filter,
1429
+ },
1430
+ };
1431
+ if (!(ids_1.length > 0)) return [3 /*break*/, 28];
1432
+ _d = modiUpsert.data;
1433
+ _l = {
1434
+ id: 'dummy',
1435
+ action: 'create'
1436
+ };
1437
+ return [4 /*yield*/, Promise.all(ids_1.map(function (id) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1438
+ var _a;
1439
+ return tslib_1.__generator(this, function (_b) {
1440
+ switch (_b.label) {
1441
+ case 0:
1442
+ _a = {};
1443
+ return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1444
+ case 1: return [2 /*return*/, (_a.id = _b.sent(),
1445
+ _a.entity = entity,
1446
+ _a.entityId = id,
1447
+ _a)];
1448
+ }
1449
+ });
1450
+ }); }))];
1451
+ case 27:
1452
+ _d.modiEntity$modi = (_l.data = _m.sent(),
1453
+ _l);
1454
+ _m.label = 28;
1455
+ case 28:
1456
+ closeRootMode = context.openRootMode();
1457
+ return [4 /*yield*/, this.cascadeUpdateAsync('modi', modiUpsert, context, option)];
1458
+ case 29:
1459
+ _m.sent();
1460
+ closeRootMode();
1461
+ return [2 /*return*/, 1];
1462
+ case 30:
1463
+ createOper = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1464
+ var createOper_1, closeRootMode;
1465
+ var _a, _b, _c;
1466
+ var _this = this;
1467
+ return tslib_1.__generator(this, function (_d) {
1468
+ switch (_d.label) {
1469
+ case 0:
1470
+ if (!(!(option === null || option === void 0 ? void 0 : option.dontCreateOper) && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity) && ids_1.length > 0)) return [3 /*break*/, 3];
1471
+ // 按照框架要求生成Oper和OperEntity这两个内置的对象
1472
+ (0, assert_1.default)(operId);
1473
+ _a = {
1474
+ id: 'dummy',
1475
+ action: 'create'
1476
+ };
1477
+ _b = {
1478
+ id: operId,
1479
+ action: action,
1480
+ data: data,
1481
+ targetEntity: entity
1482
+ };
1483
+ _c = {
1484
+ id: 'dummy',
1485
+ action: 'create'
1486
+ };
1487
+ return [4 /*yield*/, Promise.all(ids_1.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1488
+ var _a;
1489
+ return tslib_1.__generator(this, function (_b) {
1490
+ switch (_b.label) {
1491
+ case 0:
1492
+ _a = {};
1493
+ return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
1494
+ case 1: return [2 /*return*/, (_a.id = _b.sent(),
1495
+ _a.entityId = ele,
1496
+ _a.entity = entity,
1497
+ _a)];
1498
+ }
1499
+ });
1500
+ }); }))];
1501
+ case 1:
1502
+ createOper_1 = (_a.data = (_b.operEntity$oper = (_c.data = _d.sent(),
1503
+ _c),
1504
+ _b),
1505
+ _a);
1506
+ closeRootMode = context.openRootMode();
1507
+ return [4 /*yield*/, this.cascadeUpdateAsync('oper', createOper_1, context, {
1508
+ dontCollect: true,
1509
+ dontCreateOper: true,
1510
+ })];
1511
+ case 2:
1512
+ _d.sent();
1513
+ closeRootMode();
1514
+ _d.label = 3;
1515
+ case 3: return [2 /*return*/];
1516
+ }
1517
+ });
1518
+ }); };
1519
+ if (!(action === 'remove')) return [3 /*break*/, 31];
1520
+ if (!option.dontCollect) {
1521
+ context.opRecords.push({
1522
+ a: 'r',
1523
+ e: entity,
1524
+ f: {
1525
+ id: {
1526
+ $in: ids_1,
1527
+ }
1528
+ },
1529
+ });
1530
+ }
1531
+ return [3 /*break*/, 35];
1532
+ case 31:
1533
+ updateAttrCount = Object.keys(data).length;
1534
+ if (!(updateAttrCount > 0)) return [3 /*break*/, 32];
1535
+ // 优化一下,如果不更新任何属性,则不实际执行
1536
+ Object.assign(data, {
1537
+ $$updateAt$$: now,
1538
+ });
1539
+ if (!option.dontCollect) {
1540
+ context.opRecords.push({
1541
+ a: 'u',
1542
+ e: entity,
1543
+ d: data,
1544
+ f: {
1545
+ id: {
1546
+ $in: ids_1,
1547
+ }
1548
+ },
1549
+ });
1550
+ }
1551
+ return [3 /*break*/, 35];
1552
+ case 32:
1553
+ if (!(action !== 'update')) return [3 /*break*/, 34];
1554
+ // 如果不是update动作而是用户自定义的动作,这里还是要记录oper
1555
+ return [4 /*yield*/, createOper()];
1556
+ case 33:
1557
+ // 如果不是update动作而是用户自定义的动作,这里还是要记录oper
1558
+ _m.sent();
1559
+ return [2 /*return*/, 0];
1560
+ case 34: return [2 /*return*/, 0];
1561
+ case 35: return [4 /*yield*/, this.updateAbjointRowAsync(entity, operation, context, option)];
1562
+ case 36:
1563
+ result = _m.sent();
1564
+ return [4 /*yield*/, createOper()];
1565
+ case 37:
1566
+ _m.sent();
1567
+ return [2 /*return*/, result];
1568
+ }
1569
+ });
1570
+ });
1571
+ };
1572
+ CascadeStore.prototype.doUpdateSingleRow = function (entity, operation, context, option) {
1573
+ var e_5, _a;
1574
+ var _this = this;
1575
+ var data = operation.data, action = operation.action, operId = operation.id, filter = operation.filter;
1576
+ var now = Date.now();
1577
+ switch (action) {
1578
+ case 'create': {
1579
+ this.preProcessDataCreated(entity, data);
1580
+ var result_2 = 0;
1581
+ var createInner = function (operation2) {
1582
+ try {
1583
+ result_2 += _this.updateAbjointRow(entity, operation2, context, option);
1584
+ }
1585
+ catch (e) {
1586
+ throw e;
1587
+ }
1588
+ };
1589
+ if (data instanceof Array) {
1590
+ var multipleCreate = this.supportMultipleCreate();
1591
+ if (multipleCreate) {
1592
+ createInner(operation);
1593
+ }
1594
+ else {
1595
+ try {
1596
+ for (var data_3 = tslib_1.__values(data), data_3_1 = data_3.next(); !data_3_1.done; data_3_1 = data_3.next()) {
1597
+ var d = data_3_1.value;
1598
+ var createSingleOper = {
1599
+ id: 'any',
1600
+ action: 'create',
1601
+ data: d,
1602
+ };
1603
+ createInner(createSingleOper);
1604
+ }
1605
+ }
1606
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
1607
+ finally {
1608
+ try {
1609
+ if (data_3_1 && !data_3_1.done && (_a = data_3.return)) _a.call(data_3);
1610
+ }
1611
+ finally { if (e_5) throw e_5.error; }
1612
+ }
1613
+ }
1614
+ }
1615
+ else {
1616
+ createInner(operation);
1617
+ }
1618
+ return result_2;
1619
+ }
1620
+ default: {
1621
+ if (action === 'remove') {
1622
+ }
1623
+ else {
1624
+ var updateAttrCount = Object.keys(data).length;
1625
+ if (updateAttrCount > 0) {
1626
+ // 优化一下,如果不更新任何属性,则不实际执行
1627
+ Object.assign(data, {
1628
+ $$updateAt$$: now,
1629
+ });
1630
+ this.preProcessDataUpdated(data);
1631
+ }
1632
+ else {
1633
+ return 0;
1634
+ }
1635
+ }
1636
+ return this.updateAbjointRow(entity, operation, context, option);
1637
+ }
1638
+ }
1639
+ };
1640
+ CascadeStore.prototype.cascadeUpdate = function (entity, operation, context, option) {
1641
+ var e_6, _a, e_7, _b, e_8, _c;
1642
+ var action = operation.action, data = operation.data, filter = operation.filter, id = operation.id;
1643
+ var opData;
1644
+ var wholeBeforeFns = [];
1645
+ var wholeAfterFns = [];
1646
+ var result = {};
1647
+ if (['create', 'create-l'].includes(action) && data instanceof Array) {
1648
+ opData = [];
1649
+ try {
1650
+ for (var data_4 = tslib_1.__values(data), data_4_1 = data_4.next(); !data_4_1.done; data_4_1 = data_4.next()) {
1651
+ var d = data_4_1.value;
1652
+ var _d = this.destructCascadeUpdate(entity, action, d, context, option, this.cascadeUpdate), od = _d.data, beforeFns = _d.beforeFns, afterFns = _d.afterFns;
1653
+ opData.push(od);
1654
+ wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1655
+ wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1656
+ }
1657
+ }
1658
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
1659
+ finally {
1660
+ try {
1661
+ if (data_4_1 && !data_4_1.done && (_a = data_4.return)) _a.call(data_4);
1662
+ }
1663
+ finally { if (e_6) throw e_6.error; }
1664
+ }
1665
+ }
1666
+ else {
1667
+ var _e = this.destructCascadeUpdate(entity, action, data, context, option, this.cascadeUpdate, filter), od = _e.data, beforeFns = _e.beforeFns, afterFns = _e.afterFns;
1668
+ opData = od;
1669
+ wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1670
+ wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1671
+ }
1672
+ var operation2 = Object.assign({}, operation, {
1673
+ data: opData,
1674
+ });
1675
+ try {
1676
+ for (var wholeBeforeFns_1 = tslib_1.__values(wholeBeforeFns), wholeBeforeFns_1_1 = wholeBeforeFns_1.next(); !wholeBeforeFns_1_1.done; wholeBeforeFns_1_1 = wholeBeforeFns_1.next()) {
1677
+ var before_1 = wholeBeforeFns_1_1.value;
1678
+ before_1();
1679
+ }
1680
+ }
1681
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
1682
+ finally {
1683
+ try {
1684
+ if (wholeBeforeFns_1_1 && !wholeBeforeFns_1_1.done && (_b = wholeBeforeFns_1.return)) _b.call(wholeBeforeFns_1);
1685
+ }
1686
+ finally { if (e_7) throw e_7.error; }
1687
+ }
1688
+ var count = this.doUpdateSingleRow(entity, operation2, context, option);
1689
+ try {
1690
+ for (var wholeAfterFns_1 = tslib_1.__values(wholeAfterFns), wholeAfterFns_1_1 = wholeAfterFns_1.next(); !wholeAfterFns_1_1.done; wholeAfterFns_1_1 = wholeAfterFns_1.next()) {
1691
+ var after_1 = wholeAfterFns_1_1.value;
1692
+ after_1();
1693
+ }
1694
+ }
1695
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
1696
+ finally {
1697
+ try {
1698
+ if (wholeAfterFns_1_1 && !wholeAfterFns_1_1.done && (_c = wholeAfterFns_1.return)) _c.call(wholeAfterFns_1);
1699
+ }
1700
+ finally { if (e_8) throw e_8.error; }
1701
+ }
1702
+ return result;
1703
+ };
1704
+ /**
1705
+ *
1706
+ * @param entity
1707
+ * @param operation
1708
+ * @param context
1709
+ * @param option
1710
+ */
1711
+ CascadeStore.prototype.cascadeUpdateAsync = function (entity, operation, context, option) {
1712
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
1713
+ var action, data, filter, id, opData, wholeBeforeFns, wholeAfterFns, result, data_5, data_5_1, d, _a, od, beforeFns, afterFns, _b, od, beforeFns, afterFns, operation2, wholeBeforeFns_2, wholeBeforeFns_2_1, before_2, e_9_1, count, wholeAfterFns_2, wholeAfterFns_2_1, after_2, e_10_1;
1714
+ var e_11, _c, e_9, _d, _e, _f, e_10, _g;
1715
+ return tslib_1.__generator(this, function (_h) {
1716
+ switch (_h.label) {
1717
+ case 0:
1718
+ action = operation.action, data = operation.data, filter = operation.filter, id = operation.id;
1719
+ wholeBeforeFns = [];
1720
+ wholeAfterFns = [];
1721
+ result = {};
1722
+ if (['create', 'create-l'].includes(action) && data instanceof Array) {
1723
+ opData = [];
1724
+ try {
1725
+ for (data_5 = tslib_1.__values(data), data_5_1 = data_5.next(); !data_5_1.done; data_5_1 = data_5.next()) {
1726
+ d = data_5_1.value;
1727
+ _a = this.destructCascadeUpdate(entity, action, d, context, option, this.cascadeUpdateAsync), od = _a.data, beforeFns = _a.beforeFns, afterFns = _a.afterFns;
1728
+ opData.push(od);
1729
+ wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1730
+ wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1731
+ }
1732
+ }
1733
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
1734
+ finally {
1735
+ try {
1736
+ if (data_5_1 && !data_5_1.done && (_c = data_5.return)) _c.call(data_5);
1737
+ }
1738
+ finally { if (e_11) throw e_11.error; }
1739
+ }
1740
+ }
1741
+ else {
1742
+ _b = this.destructCascadeUpdate(entity, action, data, context, option, this.cascadeUpdateAsync, filter), od = _b.data, beforeFns = _b.beforeFns, afterFns = _b.afterFns;
1743
+ opData = od;
1744
+ wholeBeforeFns.push.apply(wholeBeforeFns, tslib_1.__spreadArray([], tslib_1.__read(beforeFns), false));
1745
+ wholeAfterFns.push.apply(wholeAfterFns, tslib_1.__spreadArray([], tslib_1.__read(afterFns), false));
1746
+ }
1747
+ operation2 = Object.assign({}, operation, {
1748
+ data: opData,
1749
+ });
1750
+ _h.label = 1;
1751
+ case 1:
1752
+ _h.trys.push([1, 6, 7, 8]);
1753
+ wholeBeforeFns_2 = tslib_1.__values(wholeBeforeFns), wholeBeforeFns_2_1 = wholeBeforeFns_2.next();
1754
+ _h.label = 2;
1755
+ case 2:
1756
+ if (!!wholeBeforeFns_2_1.done) return [3 /*break*/, 5];
1757
+ before_2 = wholeBeforeFns_2_1.value;
1758
+ return [4 /*yield*/, before_2()];
1759
+ case 3:
1760
+ _h.sent();
1761
+ _h.label = 4;
1762
+ case 4:
1763
+ wholeBeforeFns_2_1 = wholeBeforeFns_2.next();
1764
+ return [3 /*break*/, 2];
1765
+ case 5: return [3 /*break*/, 8];
1766
+ case 6:
1767
+ e_9_1 = _h.sent();
1768
+ e_9 = { error: e_9_1 };
1769
+ return [3 /*break*/, 8];
1770
+ case 7:
1771
+ try {
1772
+ if (wholeBeforeFns_2_1 && !wholeBeforeFns_2_1.done && (_d = wholeBeforeFns_2.return)) _d.call(wholeBeforeFns_2);
1773
+ }
1774
+ finally { if (e_9) throw e_9.error; }
1775
+ return [7 /*endfinally*/];
1776
+ case 8: return [4 /*yield*/, this.doUpdateSingleRowAsync(entity, operation2, context, option)];
1777
+ case 9:
1778
+ count = _h.sent();
1779
+ this.mergeOperationResult(result, (_e = {},
1780
+ _e[entity] = (_f = {},
1781
+ _f[operation2.action] = count,
1782
+ _f),
1783
+ _e));
1784
+ _h.label = 10;
1785
+ case 10:
1786
+ _h.trys.push([10, 15, 16, 17]);
1787
+ wholeAfterFns_2 = tslib_1.__values(wholeAfterFns), wholeAfterFns_2_1 = wholeAfterFns_2.next();
1788
+ _h.label = 11;
1789
+ case 11:
1790
+ if (!!wholeAfterFns_2_1.done) return [3 /*break*/, 14];
1791
+ after_2 = wholeAfterFns_2_1.value;
1792
+ return [4 /*yield*/, after_2()];
1793
+ case 12:
1794
+ _h.sent();
1795
+ _h.label = 13;
1796
+ case 13:
1797
+ wholeAfterFns_2_1 = wholeAfterFns_2.next();
1798
+ return [3 /*break*/, 11];
1799
+ case 14: return [3 /*break*/, 17];
1800
+ case 15:
1801
+ e_10_1 = _h.sent();
1802
+ e_10 = { error: e_10_1 };
1803
+ return [3 /*break*/, 17];
1804
+ case 16:
1805
+ try {
1806
+ if (wholeAfterFns_2_1 && !wholeAfterFns_2_1.done && (_g = wholeAfterFns_2.return)) _g.call(wholeAfterFns_2);
1807
+ }
1808
+ finally { if (e_10) throw e_10.error; }
1809
+ return [7 /*endfinally*/];
1810
+ case 17: return [2 /*return*/, result];
1811
+ }
1812
+ });
1813
+ });
1814
+ };
1815
+ CascadeStore.prototype.cascadeSelect = function (entity, selection, context, option) {
1816
+ var data = selection.data, filter = selection.filter, indexFrom = selection.indexFrom, count = selection.count, sorter = selection.sorter;
1817
+ var _a = this.destructCascadeSelect(entity, data, context, this.cascadeSelect, this.aggregateSync, option), projection = _a.projection, cascadeSelectionFns = _a.cascadeSelectionFns;
1818
+ var rows = this.selectAbjointRow(entity, {
1819
+ data: projection,
1820
+ filter: filter,
1821
+ indexFrom: indexFrom,
1822
+ count: count,
1823
+ sorter: sorter
1824
+ }, context, option);
1825
+ if (cascadeSelectionFns.length > 0) {
1826
+ var ruException_1 = [];
1827
+ cascadeSelectionFns.forEach(function (ele) {
1828
+ try {
1829
+ ele(rows);
1830
+ }
1831
+ catch (e) {
1832
+ if (e instanceof types_1.OakRowUnexistedException) {
1833
+ var rows_1 = e.getRows();
1834
+ ruException_1.push.apply(ruException_1, tslib_1.__spreadArray([], tslib_1.__read(rows_1), false));
1835
+ }
1836
+ else {
1837
+ throw e;
1838
+ }
1839
+ }
1840
+ });
1841
+ if (ruException_1.length > 0) {
1842
+ throw new types_1.OakRowUnexistedException(ruException_1);
1843
+ }
1844
+ }
1845
+ return rows;
1846
+ };
1847
+ /**
1848
+ * 将一次查询的结果集加入result
1849
+ * todo 如果是supportMtoOJoin,这里还要解构(未充分测试)
1850
+ * @param entity
1851
+ * @param rows
1852
+ * @param context
1853
+ */
1854
+ CascadeStore.prototype.addToResultSelections = function (entity, rows, context) {
1855
+ var e_12, _a;
1856
+ if (this.supportManyToOneJoin()) {
1857
+ // 这里的外键连接有可能为空,需要使用所有的行的attr的并集来测试
1858
+ var attrs = (0, lodash_1.uniq)(rows.map(function (ele) { return Object.keys(ele); }).flat());
1859
+ var attrsToPick_1 = [];
1860
+ var _loop_3 = function (attr) {
1861
+ var data = {};
1862
+ var rel = this_3.judgeRelation(entity, attr);
1863
+ if (rel === 2) {
1864
+ this_3.addToResultSelections(attr, rows.map(function (ele) { return ele[attr]; }).filter(function (ele) { return !!ele; }), context);
1865
+ }
1866
+ else if (typeof rel === 'string') {
1867
+ this_3.addToResultSelections(rel, rows.map(function (ele) { return ele[attr]; }).filter(function (ele) { return !!ele; }), context);
1868
+ }
1869
+ else if (rel instanceof Array) {
1870
+ this_3.addToResultSelections(rel[0], rows.map(function (ele) { return ele[attr]; }).reduce(function (prev, current) { return prev.concat(current); }, []), context);
1871
+ }
1872
+ else {
1873
+ attrsToPick_1.push(attr);
1874
+ }
1875
+ };
1876
+ var this_3 = this;
1877
+ try {
1878
+ for (var attrs_1 = tslib_1.__values(attrs), attrs_1_1 = attrs_1.next(); !attrs_1_1.done; attrs_1_1 = attrs_1.next()) {
1879
+ var attr = attrs_1_1.value;
1880
+ _loop_3(attr);
1881
+ }
1882
+ }
1883
+ catch (e_12_1) { e_12 = { error: e_12_1 }; }
1884
+ finally {
1885
+ try {
1886
+ if (attrs_1_1 && !attrs_1_1.done && (_a = attrs_1.return)) _a.call(attrs_1);
1887
+ }
1888
+ finally { if (e_12) throw e_12.error; }
1889
+ }
1890
+ var originRows = rows.map(function (ele) { return (0, lodash_1.pick)(ele, attrsToPick_1); });
1891
+ this.addSingleRowToResultSelections(entity, originRows, context);
1892
+ }
1893
+ else {
1894
+ this.addSingleRowToResultSelections(entity, rows, context);
1895
+ }
1896
+ };
1897
+ CascadeStore.prototype.addSingleRowToResultSelections = function (entity, rows, context) {
1898
+ var _a;
1899
+ var opRecords = context.opRecords;
1900
+ var lastOperation = opRecords[opRecords.length - 1];
1901
+ if (lastOperation && lastOperation.a === 's') {
1902
+ var entityBranch_1 = lastOperation.d[entity];
1903
+ if (entityBranch_1) {
1904
+ rows.forEach(function (row) {
1905
+ var _a;
1906
+ if (row) {
1907
+ (0, assert_1.default)(row.id);
1908
+ var id = row.id;
1909
+ if (!entityBranch_1[id]) {
1910
+ Object.assign(entityBranch_1, (_a = {},
1911
+ _a[id] = (0, lodash_1.cloneDeep)(row),
1912
+ _a));
1913
+ }
1914
+ else {
1915
+ Object.assign(entityBranch_1[id], (0, lodash_1.cloneDeep)(row));
1916
+ }
1917
+ }
1918
+ });
1919
+ return;
1920
+ }
1921
+ }
1922
+ else {
1923
+ lastOperation = {
1924
+ a: 's',
1925
+ d: {},
1926
+ };
1927
+ opRecords.push(lastOperation);
1928
+ }
1929
+ var entityBranch = {};
1930
+ rows.forEach(function (row) {
1931
+ var _a;
1932
+ if (row) {
1933
+ var id = row.id;
1934
+ Object.assign(entityBranch, (_a = {},
1935
+ _a[id] = (0, lodash_1.cloneDeep)(row),
1936
+ _a));
1937
+ }
1938
+ });
1939
+ Object.assign(lastOperation.d, (_a = {},
1940
+ _a[entity] = entityBranch,
1941
+ _a));
1942
+ };
1943
+ CascadeStore.prototype.cascadeSelectAsync = function (entity, selection, context, option) {
1944
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
1945
+ var data, filter, indexFrom, count, sorter, _a, projection, cascadeSelectionFns, rows, ruException_2;
1946
+ var _this = this;
1947
+ return tslib_1.__generator(this, function (_b) {
1948
+ switch (_b.label) {
1949
+ case 0:
1950
+ data = selection.data, filter = selection.filter, indexFrom = selection.indexFrom, count = selection.count, sorter = selection.sorter;
1951
+ _a = this.destructCascadeSelect(entity, data, context, this.cascadeSelectAsync, this.aggregateAsync, option), projection = _a.projection, cascadeSelectionFns = _a.cascadeSelectionFns;
1952
+ return [4 /*yield*/, this.selectAbjointRowAsync(entity, {
1953
+ data: projection,
1954
+ filter: filter,
1955
+ indexFrom: indexFrom,
1956
+ count: count,
1957
+ sorter: sorter
1958
+ }, context, option)];
1959
+ case 1:
1960
+ rows = _b.sent();
1961
+ if (!option.dontCollect) {
1962
+ this.addToResultSelections(entity, rows, context);
1963
+ }
1964
+ if (!(cascadeSelectionFns.length > 0)) return [3 /*break*/, 3];
1965
+ ruException_2 = [];
1966
+ return [4 /*yield*/, Promise.all(cascadeSelectionFns.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
1967
+ var e_13, rows_2;
1968
+ return tslib_1.__generator(this, function (_a) {
1969
+ switch (_a.label) {
1970
+ case 0:
1971
+ _a.trys.push([0, 2, , 3]);
1972
+ return [4 /*yield*/, ele(rows)];
1973
+ case 1:
1974
+ _a.sent();
1975
+ return [3 /*break*/, 3];
1976
+ case 2:
1977
+ e_13 = _a.sent();
1978
+ if (e_13 instanceof types_1.OakRowUnexistedException) {
1979
+ rows_2 = e_13.getRows();
1980
+ ruException_2.push.apply(ruException_2, tslib_1.__spreadArray([], tslib_1.__read(rows_2), false));
1981
+ }
1982
+ else {
1983
+ throw e_13;
1984
+ }
1985
+ return [3 /*break*/, 3];
1986
+ case 3: return [2 /*return*/];
1987
+ }
1988
+ });
1989
+ }); }))];
1990
+ case 2:
1991
+ _b.sent();
1992
+ if (ruException_2.length > 0) {
1993
+ throw new types_1.OakRowUnexistedException(ruException_2);
1994
+ }
1995
+ _b.label = 3;
1996
+ case 3: return [2 /*return*/, rows];
1997
+ }
1998
+ });
1999
+ });
2000
+ };
2001
+ CascadeStore.prototype.selectAsync = function (entity, selection, context, option) {
2002
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
2003
+ return tslib_1.__generator(this, function (_a) {
2004
+ switch (_a.label) {
2005
+ case 0: return [4 /*yield*/, this.reinforceSelectionAsync(entity, selection, context, option)];
2006
+ case 1:
2007
+ _a.sent();
2008
+ return [2 /*return*/, this.cascadeSelectAsync(entity, selection, context, option)];
2009
+ }
2010
+ });
2011
+ });
2012
+ };
2013
+ CascadeStore.prototype.selectSync = function (entity, selection, context, option) {
2014
+ this.reinforceSelectionSync(entity, selection, context, option);
2015
+ return this.cascadeSelect(entity, selection, context, option);
2016
+ };
2017
+ CascadeStore.prototype.operateSync = function (entity, operation, context, option) {
2018
+ //this.reinforceOperation(entity, operation); // 感觉前台可以无视?
2019
+ return this.cascadeUpdate(entity, operation, context, option);
2020
+ };
2021
+ CascadeStore.prototype.operateAsync = function (entity, operation, context, option) {
2022
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
2023
+ return tslib_1.__generator(this, function (_a) {
2024
+ switch (_a.label) {
2025
+ case 0: return [4 /*yield*/, this.reinforceOperation(entity, operation, context)];
2026
+ case 1:
2027
+ _a.sent();
2028
+ return [2 /*return*/, this.cascadeUpdateAsync(entity, operation, context, option)];
2029
+ }
2030
+ });
2031
+ });
2032
+ };
2033
+ return CascadeStore;
2034
+ }(RowStore_1.RowStore));
2035
+ exports.CascadeStore = CascadeStore;