oak-domain 3.0.0 → 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 -2031
  103. package/lib/store/RelationAuth.d.ts +103 -96
  104. package/lib/store/RelationAuth.js +1492 -1307
  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 -1739
  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 -192
  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,623 +1,623 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createCreateCheckers = exports.createRemoveCheckers = exports.translateCheckerInSyncContext = exports.translateCheckerInAsyncContext = void 0;
4
- var tslib_1 = require("tslib");
5
- var assert_1 = tslib_1.__importDefault(require("assert"));
6
- var filter_1 = require("../store/filter");
7
- var Exception_1 = require("../types/Exception");
8
- var types_1 = require("../types");
9
- var actionDef_1 = require("./actionDef");
10
- var lodash_1 = require("../utils/lodash");
11
- var action_1 = require("../actions/action");
12
- /**
13
- *
14
- * @param checker 要翻译的checker
15
- * @param silent 如果silent,则row和relation类型的checker只会把限制条件加到查询上,而不报错(除掉create动作)
16
- * @returns
17
- */
18
- function translateCheckerInAsyncContext(checker) {
19
- var _this = this;
20
- var entity = checker.entity, type = checker.type;
21
- var when = 'before'; // 现在create的relation改成提前的expression检查了,原先是先插入再后检查,性能不行,而且select也需要实现前检查
22
- switch (type) {
23
- case 'data': {
24
- var checkerFn_1 = checker.checker;
25
- var fn = (function (_a, context) {
26
- var operation = _a.operation;
27
- return tslib_1.__awaiter(_this, void 0, void 0, function () {
28
- var data;
29
- return tslib_1.__generator(this, function (_b) {
30
- switch (_b.label) {
31
- case 0:
32
- data = operation.data;
33
- return [4 /*yield*/, checkerFn_1(data, context)];
34
- case 1:
35
- _b.sent();
36
- return [2 /*return*/, 0];
37
- }
38
- });
39
- });
40
- });
41
- return {
42
- fn: fn,
43
- when: when,
44
- };
45
- }
46
- case 'row': {
47
- var filter_2 = checker.filter, errMsg_1 = checker.errMsg, inconsistentRows_1 = checker.inconsistentRows;
48
- var fn = (function (_a, context, option) {
49
- var operation = _a.operation;
50
- return tslib_1.__awaiter(_this, void 0, void 0, function () {
51
- var operationFilter, action, filter2, _b, entity2, selection2, rows2, e, rows2, e;
52
- return tslib_1.__generator(this, function (_c) {
53
- switch (_c.label) {
54
- case 0:
55
- operationFilter = operation.filter, action = operation.action;
56
- if (!(typeof filter_2 === 'function')) return [3 /*break*/, 2];
57
- return [4 /*yield*/, filter_2(operation, context, option)];
58
- case 1:
59
- _b = _c.sent();
60
- return [3 /*break*/, 3];
61
- case 2:
62
- _b = filter_2;
63
- _c.label = 3;
64
- case 3:
65
- filter2 = _b;
66
- if (!['select', 'count', 'stat'].includes(action)) return [3 /*break*/, 4];
67
- operation.filter = (0, filter_1.combineFilters)(entity, context.getSchema(), [operationFilter, filter2]);
68
- return [2 /*return*/, 0];
69
- case 4: return [4 /*yield*/, (0, filter_1.checkFilterContains)(entity, context, filter2, operationFilter || {}, true)];
70
- case 5:
71
- if (_c.sent()) {
72
- return [2 /*return*/, 0];
73
- }
74
- if (!inconsistentRows_1) return [3 /*break*/, 7];
75
- entity2 = inconsistentRows_1.entity, selection2 = inconsistentRows_1.selection;
76
- return [4 /*yield*/, context.select(entity2, selection2(operationFilter), {
77
- dontCollect: true,
78
- blockTrigger: true,
79
- })];
80
- case 6:
81
- rows2 = _c.sent();
82
- e = new Exception_1.OakRowInconsistencyException(undefined, errMsg_1);
83
- e.addData(entity2, rows2);
84
- throw e;
85
- case 7: return [4 /*yield*/, context.select(entity, {
86
- data: (0, actionDef_1.getFullProjection)(entity, context.getSchema()),
87
- filter: Object.assign({}, operationFilter, {
88
- $not: filter2,
89
- })
90
- }, {
91
- dontCollect: true,
92
- blockTrigger: true,
93
- })];
94
- case 8:
95
- rows2 = _c.sent();
96
- e = new Exception_1.OakRowInconsistencyException(undefined, errMsg_1);
97
- e.addData(entity, rows2);
98
- throw e;
99
- }
100
- });
101
- });
102
- });
103
- return {
104
- fn: fn,
105
- when: when,
106
- };
107
- }
108
- case 'relation': {
109
- var relationFilter_1 = checker.relationFilter, errMsg_2 = checker.errMsg;
110
- var fn = (function (_a, context, option) {
111
- var operation = _a.operation;
112
- return tslib_1.__awaiter(_this, void 0, void 0, function () {
113
- var result, _b, filter, action, errMsg2;
114
- return tslib_1.__generator(this, function (_c) {
115
- switch (_c.label) {
116
- case 0:
117
- if (context.isRoot()) {
118
- return [2 /*return*/, 0];
119
- }
120
- if (!(typeof relationFilter_1 === 'function')) return [3 /*break*/, 2];
121
- return [4 /*yield*/, relationFilter_1(operation, context, option)];
122
- case 1:
123
- _b = _c.sent();
124
- return [3 /*break*/, 3];
125
- case 2:
126
- _b = relationFilter_1;
127
- _c.label = 3;
128
- case 3:
129
- result = _b;
130
- if (!result) return [3 /*break*/, 5];
131
- filter = operation.filter, action = operation.action;
132
- if (action === 'create') {
133
- console.warn("".concat(entity, "\u5BF9\u8C61\u7684create\u7C7B\u578B\u7684checker\u4E2D\uFF0C\u5B58\u5728\u65E0\u6CD5\u8F6C\u6362\u4E3A\u8868\u8FBE\u5F0F\u5F62\u5F0F\u7684\u60C5\u51B5\uFF0C\u8BF7\u5C3D\u91CF\u4F7F\u7528authDef\u683C\u5F0F\u5B9A\u4E49\u8FD9\u7C7Bchecker"));
134
- return [2 /*return*/, 0];
135
- }
136
- if (['select', 'count', 'stat'].includes(action)) {
137
- operation.filter = (0, filter_1.combineFilters)(entity, context.getSchema(), [filter, result]);
138
- return [2 /*return*/, 0];
139
- }
140
- (0, assert_1.default)(filter);
141
- return [4 /*yield*/, (0, filter_1.checkFilterContains)(entity, context, result, filter, true)];
142
- case 4:
143
- if (_c.sent()) {
144
- return [2 /*return*/];
145
- }
146
- errMsg2 = typeof errMsg_2 === 'function' ? errMsg_2(operation, context, option) : errMsg_2;
147
- throw new Exception_1.OakUserUnpermittedException(errMsg2);
148
- case 5: return [2 /*return*/, 0];
149
- }
150
- });
151
- });
152
- });
153
- return {
154
- fn: fn,
155
- when: when,
156
- };
157
- }
158
- case 'logical':
159
- case 'logicalRelation': {
160
- var checkerFn_2 = checker.checker;
161
- var fn = (function (_a, context, option) {
162
- var operation = _a.operation;
163
- return tslib_1.__awaiter(_this, void 0, void 0, function () {
164
- return tslib_1.__generator(this, function (_b) {
165
- switch (_b.label) {
166
- case 0:
167
- if (context.isRoot() && type === 'logicalRelation') {
168
- return [2 /*return*/, 0];
169
- }
170
- return [4 /*yield*/, checkerFn_2(operation, context, option)];
171
- case 1:
172
- _b.sent();
173
- return [2 /*return*/, 0];
174
- }
175
- });
176
- });
177
- });
178
- return {
179
- fn: fn,
180
- when: when,
181
- };
182
- }
183
- default: {
184
- (0, assert_1.default)(false);
185
- }
186
- }
187
- }
188
- exports.translateCheckerInAsyncContext = translateCheckerInAsyncContext;
189
- function translateCheckerInSyncContext(checker) {
190
- var entity = checker.entity, type = checker.type;
191
- var when = 'before'; // 现在create的relation改成提前的expression检查了,原先是先插入再后检查,性能不行,而且select也需要实现前检查
192
- switch (type) {
193
- case 'data': {
194
- var checkerFn_3 = checker.checker;
195
- var fn = function (operation, context) { return checkerFn_3(operation.data, context); };
196
- return {
197
- fn: fn,
198
- when: when,
199
- };
200
- }
201
- case 'row': {
202
- var filter_3 = checker.filter, errMsg_3 = checker.errMsg;
203
- var fn = function (operation, context, option) {
204
- var operationFilter = operation.filter, action = operation.action;
205
- var filter2 = typeof filter_3 === 'function' ? filter_3(operation, context, option) : filter_3;
206
- (0, assert_1.default)(operationFilter);
207
- (0, assert_1.default)(!(filter2 instanceof Promise));
208
- if ((0, filter_1.checkFilterContains)(entity, context, filter2, operationFilter, true)) {
209
- return;
210
- }
211
- var e = new Exception_1.OakRowInconsistencyException(undefined, errMsg_3);
212
- throw e;
213
- };
214
- return {
215
- fn: fn,
216
- when: when,
217
- };
218
- }
219
- case 'relation': {
220
- var relationFilter_2 = checker.relationFilter, errMsg_4 = checker.errMsg;
221
- var fn = function (operation, context, option) {
222
- if (context.isRoot()) {
223
- return;
224
- }
225
- var result = typeof relationFilter_2 === 'function' ? relationFilter_2(operation, context, option) : relationFilter_2;
226
- (0, assert_1.default)(!(result instanceof Promise));
227
- if (result) {
228
- var filter = operation.filter, action = operation.action;
229
- if (action === 'create') {
230
- console.warn("".concat(entity, "\u5BF9\u8C61\u7684create\u7C7B\u578B\u7684checker\u4E2D\uFF0C\u5B58\u5728\u65E0\u6CD5\u8F6C\u6362\u4E3A\u8868\u8FBE\u5F0F\u5F62\u5F0F\u7684\u60C5\u51B5\uFF0C\u8BF7\u5C3D\u91CF\u4F7F\u7528authDef\u683C\u5F0F\u5B9A\u4E49\u8FD9\u7C7Bchecker"));
231
- return;
232
- }
233
- (0, assert_1.default)(filter);
234
- if ((0, filter_1.checkFilterContains)(entity, context, result, filter, true)) {
235
- return;
236
- }
237
- var errMsg2 = typeof errMsg_4 === 'function' ? errMsg_4(operation, context, option) : errMsg_4;
238
- throw new Exception_1.OakUserUnpermittedException(errMsg2);
239
- }
240
- };
241
- return {
242
- fn: fn,
243
- when: when,
244
- };
245
- }
246
- case 'logical':
247
- case 'logicalRelation': {
248
- var checkerFn_4 = checker.checker;
249
- var fn = function (operation, context, option) {
250
- if (context.isRoot() && type === 'logicalRelation') {
251
- return;
252
- }
253
- checkerFn_4(operation, context, option);
254
- };
255
- return {
256
- fn: fn,
257
- when: when,
258
- };
259
- }
260
- default: {
261
- (0, assert_1.default)(false);
262
- }
263
- }
264
- }
265
- exports.translateCheckerInSyncContext = translateCheckerInSyncContext;
266
- /**
267
- * 对对象的删除,检查其是否会产生其他行上的空指针,不允许这种情况的出现
268
- * @param schema
269
- * @returns
270
- * 如果有的对象允许删除,需要使用trigger来处理其相关联的外键对象,这些trigger写作before,则会在checker之前执行,仍然可以删除成功
271
- */
272
- function createRemoveCheckers(schema) {
273
- var e_1, _a;
274
- var checkers = [];
275
- // 先建立所有的一对多的关系
276
- var OneToManyMatrix = {};
277
- var OneToManyOnEntityMatrix = {};
278
- var addToMto = function (e, f, attr) {
279
- var _a;
280
- if (OneToManyMatrix[f]) {
281
- (_a = OneToManyMatrix[f]) === null || _a === void 0 ? void 0 : _a.push([e, attr]);
282
- }
283
- else {
284
- OneToManyMatrix[f] = [[e, attr]];
285
- }
286
- };
287
- var addToMtoEntity = function (e, fs) {
288
- var e_2, _a;
289
- var _b;
290
- try {
291
- for (var fs_1 = tslib_1.__values(fs), fs_1_1 = fs_1.next(); !fs_1_1.done; fs_1_1 = fs_1.next()) {
292
- var f = fs_1_1.value;
293
- if (!OneToManyOnEntityMatrix[f]) {
294
- OneToManyOnEntityMatrix[f] = [e];
295
- }
296
- else {
297
- (_b = OneToManyOnEntityMatrix[f]) === null || _b === void 0 ? void 0 : _b.push(e);
298
- }
299
- }
300
- }
301
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
302
- finally {
303
- try {
304
- if (fs_1_1 && !fs_1_1.done && (_a = fs_1.return)) _a.call(fs_1);
305
- }
306
- finally { if (e_2) throw e_2.error; }
307
- }
308
- };
309
- for (var entity in schema) {
310
- if (['operEntity', 'modiEntity', 'userEntityGrant'].includes(entity)) {
311
- continue; // 系统功能性数据,不用处理
312
- }
313
- var attributes = schema[entity].attributes;
314
- for (var attr in attributes) {
315
- if (attributes[attr].type === 'ref') {
316
- addToMto(entity, attributes[attr].ref, attr);
317
- }
318
- else if (attr === 'entity') {
319
- if (attributes[attr].ref) {
320
- addToMtoEntity(entity, attributes[attr].ref);
321
- }
322
- else if (process.env.NODE_ENV === 'development') {
323
- console.warn("".concat(entity, "\u7684entity\u53CD\u6307\u6307\u9488\u627E\u4E0D\u5230\u6709\u6548\u7684\u5BF9\u8C61"));
324
- }
325
- }
326
- }
327
- }
328
- // 当删除一时,要确认多上面没有指向一的数据
329
- var entities = (0, lodash_1.union)(Object.keys(OneToManyMatrix), Object.keys(OneToManyOnEntityMatrix));
330
- var _loop_1 = function (entity) {
331
- checkers.push({
332
- entity: entity,
333
- action: 'remove',
334
- type: 'logical',
335
- priority: types_1.CHECKER_MAX_PRIORITY,
336
- checker: function (operation, context, option) {
337
- var e_3, _a, e_4, _b;
338
- var promises = [];
339
- if (OneToManyMatrix[entity]) {
340
- var _loop_2 = function (otm) {
341
- var _g, _h;
342
- var _j = tslib_1.__read(otm, 2), e = _j[0], attr = _j[1];
343
- var proj = (_g = {
344
- id: 1
345
- },
346
- _g[attr] = 1,
347
- _g);
348
- var filter = operation.filter && (_h = {},
349
- _h[attr.slice(0, attr.length - 2)] = operation.filter,
350
- _h);
351
- var result = context.select(e, {
352
- data: proj,
353
- filter: filter,
354
- indexFrom: 0,
355
- count: 1
356
- }, { dontCollect: true });
357
- if (result instanceof Promise) {
358
- promises.push(result.then(function (_a) {
359
- var _b = tslib_1.__read(_a, 1), row = _b[0];
360
- if (row) {
361
- var err = new Exception_1.OakRowInconsistencyException(undefined, "\u60A8\u65E0\u6CD5\u5220\u9664\u5B58\u5728\u6709\u6548\u6570\u636E\u300C".concat(e, "\u300D\u5173\u8054\u7684\u884C"));
362
- err.addData(e, [row]);
363
- throw err;
364
- }
365
- }));
366
- }
367
- else {
368
- var _k = tslib_1.__read(result, 1), row = _k[0];
369
- if (row) {
370
- var err = new Exception_1.OakRowInconsistencyException(undefined, "\u60A8\u65E0\u6CD5\u5220\u9664\u5B58\u5728\u6709\u6548\u6570\u636E\u300C".concat(e, "\u300D\u5173\u8054\u7684\u884C"));
371
- err.addData(e, [row]);
372
- throw err;
373
- }
374
- }
375
- };
376
- try {
377
- for (var _c = (e_3 = void 0, tslib_1.__values(OneToManyMatrix[entity])), _d = _c.next(); !_d.done; _d = _c.next()) {
378
- var otm = _d.value;
379
- _loop_2(otm);
380
- }
381
- }
382
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
383
- finally {
384
- try {
385
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
386
- }
387
- finally { if (e_3) throw e_3.error; }
388
- }
389
- }
390
- if (OneToManyOnEntityMatrix[entity]) {
391
- var _loop_3 = function (otm) {
392
- var _l, _m, _o;
393
- var proj = {
394
- id: 1,
395
- entity: 1,
396
- entityId: 1,
397
- };
398
- var filter = operation.filter && (_l = {},
399
- _l[entity] = operation.filter,
400
- _l);
401
- var result = context.select(otm, {
402
- data: proj,
403
- filter: filter,
404
- indexFrom: 0,
405
- count: 1
406
- }, { dontCollect: true });
407
- if (result instanceof Promise) {
408
- promises.push(result.then(function (_a) {
409
- var _b = tslib_1.__read(_a, 1), row = _b[0];
410
- if (row) {
411
- var e = new Exception_1.OakRowInconsistencyException(undefined, "\u60A8\u65E0\u6CD5\u5220\u9664\u5B58\u5728\u6709\u6548\u6570\u636E\u300C".concat(otm, "\u300D\u5173\u8054\u7684\u884C"));
412
- e.addData(otm, [row]);
413
- throw e;
414
- }
415
- }));
416
- }
417
- else {
418
- var _p = tslib_1.__read(result, 1), row = _p[0];
419
- if (row) {
420
- var record = {
421
- a: 's',
422
- d: (_m = {},
423
- _m[otm] = (_o = {},
424
- _o[row.id] = row,
425
- _o),
426
- _m)
427
- };
428
- var e = new Exception_1.OakRowInconsistencyException(undefined, "\u60A8\u65E0\u6CD5\u5220\u9664\u5B58\u5728\u6709\u6548\u6570\u636E\u300C".concat(otm, "\u300D\u5173\u8054\u7684\u884C"));
429
- e.addData(otm, [row]);
430
- throw e;
431
- }
432
- }
433
- };
434
- try {
435
- for (var _e = (e_4 = void 0, tslib_1.__values(OneToManyOnEntityMatrix[entity])), _f = _e.next(); !_f.done; _f = _e.next()) {
436
- var otm = _f.value;
437
- _loop_3(otm);
438
- }
439
- }
440
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
441
- finally {
442
- try {
443
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
444
- }
445
- finally { if (e_4) throw e_4.error; }
446
- }
447
- }
448
- if (promises.length > 0) {
449
- return Promise.all(promises).then(function () { return undefined; });
450
- }
451
- }
452
- });
453
- };
454
- try {
455
- for (var entities_1 = tslib_1.__values(entities), entities_1_1 = entities_1.next(); !entities_1_1.done; entities_1_1 = entities_1.next()) {
456
- var entity = entities_1_1.value;
457
- _loop_1(entity);
458
- }
459
- }
460
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
461
- finally {
462
- try {
463
- if (entities_1_1 && !entities_1_1.done && (_a = entities_1.return)) _a.call(entities_1);
464
- }
465
- finally { if (e_1) throw e_1.error; }
466
- }
467
- return checkers;
468
- }
469
- exports.createRemoveCheckers = createRemoveCheckers;
470
- function checkAttributeLegal(schema, entity, data) {
471
- var _a;
472
- var _b;
473
- var attributes = schema[entity].attributes;
474
- for (var attr in data) {
475
- if (attributes[attr]) {
476
- var _c = attributes[attr], type = _c.type, params = _c.params, defaultValue = _c.default, enumeration = _c.enumeration, notNull = _c.notNull;
477
- if (data[attr] === null || data[attr] === undefined) {
478
- if (notNull && defaultValue === undefined) {
479
- throw new Exception_1.OakAttrNotNullException(entity, [attr]);
480
- }
481
- if (defaultValue !== undefined) {
482
- Object.assign(data, (_a = {},
483
- _a[attr] = defaultValue,
484
- _a));
485
- }
486
- continue;
487
- }
488
- switch (type) {
489
- case 'char':
490
- case 'varchar': {
491
- if (typeof data[attr] !== 'string') {
492
- throw new Exception_1.OakInputIllegalException(entity, [attr], 'not a string');
493
- }
494
- var length_1 = params.length;
495
- if (length_1 && data[attr].length > length_1) {
496
- throw new Exception_1.OakInputIllegalException(entity, [attr], 'too long');
497
- }
498
- break;
499
- }
500
- case 'int':
501
- case 'smallint':
502
- case 'tinyint':
503
- case 'bigint':
504
- case 'decimal':
505
- case 'money': {
506
- if (typeof data[attr] !== 'number') {
507
- throw new Exception_1.OakInputIllegalException(entity, [attr], 'not a number');
508
- }
509
- var _d = params || {}, min = _d.min, max = _d.max;
510
- if (typeof min === 'number' && data[attr] < min) {
511
- throw new Exception_1.OakInputIllegalException(entity, [attr], 'too small');
512
- }
513
- if (typeof max === 'number' && data[attr] > max) {
514
- throw new Exception_1.OakInputIllegalException(entity, [attr], 'too big');
515
- }
516
- break;
517
- }
518
- case 'enum': {
519
- (0, assert_1.default)(enumeration);
520
- if (!enumeration.includes(data[attr])) {
521
- throw new Exception_1.OakInputIllegalException(entity, [attr], 'not in enumberation');
522
- }
523
- break;
524
- }
525
- }
526
- }
527
- else {
528
- // 这里似乎还有一种update中带cascade remove的case,等遇到再说(貌似cascadeUpdate没有处理完整这种情况) by Xc
529
- if (typeof data[attr] === 'object' && ((_b = data[attr]) === null || _b === void 0 ? void 0 : _b.action) === 'remove') {
530
- console.warn('cascade remove可能是未处理的边界,请注意');
531
- }
532
- }
533
- }
534
- }
535
- function createCreateCheckers(schema) {
536
- var checkers = [];
537
- var _loop_4 = function (entity) {
538
- var _a = schema[entity], attributes = _a.attributes, actions = _a.actions;
539
- var notNullAttrs = Object.keys(attributes).filter(function (ele) { return attributes[ele].notNull; });
540
- var updateActions = (0, lodash_1.difference)(actions, action_1.excludeUpdateActions);
541
- checkers.push({
542
- entity: entity,
543
- type: 'data',
544
- action: 'create',
545
- checker: function (data) {
546
- var checkData = function (data2) {
547
- var e_5, _a, e_6, _b;
548
- var illegalNullAttrs = (0, lodash_1.difference)(notNullAttrs, Object.keys(data2));
549
- if (illegalNullAttrs.length > 0) {
550
- try {
551
- // 要处理多对一的cascade create
552
- for (var illegalNullAttrs_1 = (e_5 = void 0, tslib_1.__values(illegalNullAttrs)), illegalNullAttrs_1_1 = illegalNullAttrs_1.next(); !illegalNullAttrs_1_1.done; illegalNullAttrs_1_1 = illegalNullAttrs_1.next()) {
553
- var attr = illegalNullAttrs_1_1.value;
554
- if (attr === 'entityId') {
555
- if (illegalNullAttrs.includes('entity')) {
556
- continue;
557
- }
558
- }
559
- else if (attr === 'entity' && attributes[attr].ref) {
560
- var hasCascadeCreate = false;
561
- try {
562
- for (var _c = (e_6 = void 0, tslib_1.__values(attributes[attr].ref)), _d = _c.next(); !_d.done; _d = _c.next()) {
563
- var ref = _d.value;
564
- if (data2[ref] && data2[ref].action === 'create') {
565
- hasCascadeCreate = true;
566
- break;
567
- }
568
- }
569
- }
570
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
571
- finally {
572
- try {
573
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
574
- }
575
- finally { if (e_6) throw e_6.error; }
576
- }
577
- if (hasCascadeCreate) {
578
- continue;
579
- }
580
- }
581
- else if (attributes[attr].type === 'ref') {
582
- var ref = attributes[attr].ref;
583
- var attr2 = attr.slice(0, attr.length - 2);
584
- if (data2[attr2] && data2[attr2].action === 'create') {
585
- continue;
586
- }
587
- }
588
- // 到这里说明确实是有not null的属性没有赋值
589
- throw new Exception_1.OakAttrNotNullException(entity, illegalNullAttrs);
590
- }
591
- }
592
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
593
- finally {
594
- try {
595
- if (illegalNullAttrs_1_1 && !illegalNullAttrs_1_1.done && (_a = illegalNullAttrs_1.return)) _a.call(illegalNullAttrs_1);
596
- }
597
- finally { if (e_5) throw e_5.error; }
598
- }
599
- }
600
- checkAttributeLegal(schema, entity, data2);
601
- };
602
- if (data instanceof Array) {
603
- data.forEach(function (ele) { return checkData(ele); });
604
- }
605
- else {
606
- checkData(data);
607
- }
608
- }
609
- }, {
610
- entity: entity,
611
- type: 'data',
612
- action: updateActions,
613
- checker: function (data) {
614
- checkAttributeLegal(schema, entity, data);
615
- }
616
- });
617
- };
618
- for (var entity in schema) {
619
- _loop_4(entity);
620
- }
621
- return checkers;
622
- }
623
- exports.createCreateCheckers = createCreateCheckers;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCreateCheckers = exports.createRemoveCheckers = exports.translateCheckerInSyncContext = exports.translateCheckerInAsyncContext = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var assert_1 = tslib_1.__importDefault(require("assert"));
6
+ var filter_1 = require("../store/filter");
7
+ var Exception_1 = require("../types/Exception");
8
+ var types_1 = require("../types");
9
+ var actionDef_1 = require("./actionDef");
10
+ var lodash_1 = require("../utils/lodash");
11
+ var action_1 = require("../actions/action");
12
+ /**
13
+ *
14
+ * @param checker 要翻译的checker
15
+ * @param silent 如果silent,则row和relation类型的checker只会把限制条件加到查询上,而不报错(除掉create动作)
16
+ * @returns
17
+ */
18
+ function translateCheckerInAsyncContext(checker) {
19
+ var _this = this;
20
+ var entity = checker.entity, type = checker.type;
21
+ var when = 'before'; // 现在create的relation改成提前的expression检查了,原先是先插入再后检查,性能不行,而且select也需要实现前检查
22
+ switch (type) {
23
+ case 'data': {
24
+ var checkerFn_1 = checker.checker;
25
+ var fn = (function (_a, context) {
26
+ var operation = _a.operation;
27
+ return tslib_1.__awaiter(_this, void 0, void 0, function () {
28
+ var data;
29
+ return tslib_1.__generator(this, function (_b) {
30
+ switch (_b.label) {
31
+ case 0:
32
+ data = operation.data;
33
+ return [4 /*yield*/, checkerFn_1(data, context)];
34
+ case 1:
35
+ _b.sent();
36
+ return [2 /*return*/, 0];
37
+ }
38
+ });
39
+ });
40
+ });
41
+ return {
42
+ fn: fn,
43
+ when: when,
44
+ };
45
+ }
46
+ case 'row': {
47
+ var filter_2 = checker.filter, errMsg_1 = checker.errMsg, inconsistentRows_1 = checker.inconsistentRows;
48
+ var fn = (function (_a, context, option) {
49
+ var operation = _a.operation;
50
+ return tslib_1.__awaiter(_this, void 0, void 0, function () {
51
+ var operationFilter, action, filter2, _b, entity2, selection2, rows2, e, rows2, e;
52
+ return tslib_1.__generator(this, function (_c) {
53
+ switch (_c.label) {
54
+ case 0:
55
+ operationFilter = operation.filter, action = operation.action;
56
+ if (!(typeof filter_2 === 'function')) return [3 /*break*/, 2];
57
+ return [4 /*yield*/, filter_2(operation, context, option)];
58
+ case 1:
59
+ _b = _c.sent();
60
+ return [3 /*break*/, 3];
61
+ case 2:
62
+ _b = filter_2;
63
+ _c.label = 3;
64
+ case 3:
65
+ filter2 = _b;
66
+ if (!['select', 'count', 'stat'].includes(action)) return [3 /*break*/, 4];
67
+ operation.filter = (0, filter_1.combineFilters)(entity, context.getSchema(), [operationFilter, filter2]);
68
+ return [2 /*return*/, 0];
69
+ case 4: return [4 /*yield*/, (0, filter_1.checkFilterContains)(entity, context, filter2, operationFilter || {}, true)];
70
+ case 5:
71
+ if (_c.sent()) {
72
+ return [2 /*return*/, 0];
73
+ }
74
+ if (!inconsistentRows_1) return [3 /*break*/, 7];
75
+ entity2 = inconsistentRows_1.entity, selection2 = inconsistentRows_1.selection;
76
+ return [4 /*yield*/, context.select(entity2, selection2(operationFilter), {
77
+ dontCollect: true,
78
+ blockTrigger: true,
79
+ })];
80
+ case 6:
81
+ rows2 = _c.sent();
82
+ e = new Exception_1.OakRowInconsistencyException(undefined, errMsg_1);
83
+ e.addData(entity2, rows2);
84
+ throw e;
85
+ case 7: return [4 /*yield*/, context.select(entity, {
86
+ data: (0, actionDef_1.getFullProjection)(entity, context.getSchema()),
87
+ filter: Object.assign({}, operationFilter, {
88
+ $not: filter2,
89
+ })
90
+ }, {
91
+ dontCollect: true,
92
+ blockTrigger: true,
93
+ })];
94
+ case 8:
95
+ rows2 = _c.sent();
96
+ e = new Exception_1.OakRowInconsistencyException(undefined, errMsg_1);
97
+ e.addData(entity, rows2);
98
+ throw e;
99
+ }
100
+ });
101
+ });
102
+ });
103
+ return {
104
+ fn: fn,
105
+ when: when,
106
+ };
107
+ }
108
+ case 'relation': {
109
+ var relationFilter_1 = checker.relationFilter, errMsg_2 = checker.errMsg;
110
+ var fn = (function (_a, context, option) {
111
+ var operation = _a.operation;
112
+ return tslib_1.__awaiter(_this, void 0, void 0, function () {
113
+ var result, _b, filter, action, errMsg2;
114
+ return tslib_1.__generator(this, function (_c) {
115
+ switch (_c.label) {
116
+ case 0:
117
+ if (context.isRoot()) {
118
+ return [2 /*return*/, 0];
119
+ }
120
+ if (!(typeof relationFilter_1 === 'function')) return [3 /*break*/, 2];
121
+ return [4 /*yield*/, relationFilter_1(operation, context, option)];
122
+ case 1:
123
+ _b = _c.sent();
124
+ return [3 /*break*/, 3];
125
+ case 2:
126
+ _b = relationFilter_1;
127
+ _c.label = 3;
128
+ case 3:
129
+ result = _b;
130
+ if (!result) return [3 /*break*/, 5];
131
+ filter = operation.filter, action = operation.action;
132
+ if (action === 'create') {
133
+ console.warn("".concat(entity, "\u5BF9\u8C61\u7684create\u7C7B\u578B\u7684checker\u4E2D\uFF0C\u5B58\u5728\u65E0\u6CD5\u8F6C\u6362\u4E3A\u8868\u8FBE\u5F0F\u5F62\u5F0F\u7684\u60C5\u51B5\uFF0C\u8BF7\u5C3D\u91CF\u4F7F\u7528authDef\u683C\u5F0F\u5B9A\u4E49\u8FD9\u7C7Bchecker"));
134
+ return [2 /*return*/, 0];
135
+ }
136
+ if (['select', 'count', 'stat'].includes(action)) {
137
+ operation.filter = (0, filter_1.combineFilters)(entity, context.getSchema(), [filter, result]);
138
+ return [2 /*return*/, 0];
139
+ }
140
+ (0, assert_1.default)(filter);
141
+ return [4 /*yield*/, (0, filter_1.checkFilterContains)(entity, context, result, filter, true)];
142
+ case 4:
143
+ if (_c.sent()) {
144
+ return [2 /*return*/];
145
+ }
146
+ errMsg2 = typeof errMsg_2 === 'function' ? errMsg_2(operation, context, option) : errMsg_2;
147
+ throw new Exception_1.OakUserUnpermittedException(errMsg2);
148
+ case 5: return [2 /*return*/, 0];
149
+ }
150
+ });
151
+ });
152
+ });
153
+ return {
154
+ fn: fn,
155
+ when: when,
156
+ };
157
+ }
158
+ case 'logical':
159
+ case 'logicalRelation': {
160
+ var checkerFn_2 = checker.checker;
161
+ var fn = (function (_a, context, option) {
162
+ var operation = _a.operation;
163
+ return tslib_1.__awaiter(_this, void 0, void 0, function () {
164
+ return tslib_1.__generator(this, function (_b) {
165
+ switch (_b.label) {
166
+ case 0:
167
+ if (context.isRoot() && type === 'logicalRelation') {
168
+ return [2 /*return*/, 0];
169
+ }
170
+ return [4 /*yield*/, checkerFn_2(operation, context, option)];
171
+ case 1:
172
+ _b.sent();
173
+ return [2 /*return*/, 0];
174
+ }
175
+ });
176
+ });
177
+ });
178
+ return {
179
+ fn: fn,
180
+ when: when,
181
+ };
182
+ }
183
+ default: {
184
+ (0, assert_1.default)(false);
185
+ }
186
+ }
187
+ }
188
+ exports.translateCheckerInAsyncContext = translateCheckerInAsyncContext;
189
+ function translateCheckerInSyncContext(checker) {
190
+ var entity = checker.entity, type = checker.type;
191
+ var when = 'before'; // 现在create的relation改成提前的expression检查了,原先是先插入再后检查,性能不行,而且select也需要实现前检查
192
+ switch (type) {
193
+ case 'data': {
194
+ var checkerFn_3 = checker.checker;
195
+ var fn = function (operation, context) { return checkerFn_3(operation.data, context); };
196
+ return {
197
+ fn: fn,
198
+ when: when,
199
+ };
200
+ }
201
+ case 'row': {
202
+ var filter_3 = checker.filter, errMsg_3 = checker.errMsg;
203
+ var fn = function (operation, context, option) {
204
+ var operationFilter = operation.filter, action = operation.action;
205
+ var filter2 = typeof filter_3 === 'function' ? filter_3(operation, context, option) : filter_3;
206
+ (0, assert_1.default)(operationFilter);
207
+ (0, assert_1.default)(!(filter2 instanceof Promise));
208
+ if ((0, filter_1.checkFilterContains)(entity, context, filter2, operationFilter, true)) {
209
+ return;
210
+ }
211
+ var e = new Exception_1.OakRowInconsistencyException(undefined, errMsg_3);
212
+ throw e;
213
+ };
214
+ return {
215
+ fn: fn,
216
+ when: when,
217
+ };
218
+ }
219
+ case 'relation': {
220
+ var relationFilter_2 = checker.relationFilter, errMsg_4 = checker.errMsg;
221
+ var fn = function (operation, context, option) {
222
+ if (context.isRoot()) {
223
+ return;
224
+ }
225
+ var result = typeof relationFilter_2 === 'function' ? relationFilter_2(operation, context, option) : relationFilter_2;
226
+ (0, assert_1.default)(!(result instanceof Promise));
227
+ if (result) {
228
+ var filter = operation.filter, action = operation.action;
229
+ if (action === 'create') {
230
+ console.warn("".concat(entity, "\u5BF9\u8C61\u7684create\u7C7B\u578B\u7684checker\u4E2D\uFF0C\u5B58\u5728\u65E0\u6CD5\u8F6C\u6362\u4E3A\u8868\u8FBE\u5F0F\u5F62\u5F0F\u7684\u60C5\u51B5\uFF0C\u8BF7\u5C3D\u91CF\u4F7F\u7528authDef\u683C\u5F0F\u5B9A\u4E49\u8FD9\u7C7Bchecker"));
231
+ return;
232
+ }
233
+ (0, assert_1.default)(filter);
234
+ if ((0, filter_1.checkFilterContains)(entity, context, result, filter, true)) {
235
+ return;
236
+ }
237
+ var errMsg2 = typeof errMsg_4 === 'function' ? errMsg_4(operation, context, option) : errMsg_4;
238
+ throw new Exception_1.OakUserUnpermittedException(errMsg2);
239
+ }
240
+ };
241
+ return {
242
+ fn: fn,
243
+ when: when,
244
+ };
245
+ }
246
+ case 'logical':
247
+ case 'logicalRelation': {
248
+ var checkerFn_4 = checker.checker;
249
+ var fn = function (operation, context, option) {
250
+ if (context.isRoot() && type === 'logicalRelation') {
251
+ return;
252
+ }
253
+ checkerFn_4(operation, context, option);
254
+ };
255
+ return {
256
+ fn: fn,
257
+ when: when,
258
+ };
259
+ }
260
+ default: {
261
+ (0, assert_1.default)(false);
262
+ }
263
+ }
264
+ }
265
+ exports.translateCheckerInSyncContext = translateCheckerInSyncContext;
266
+ /**
267
+ * 对对象的删除,检查其是否会产生其他行上的空指针,不允许这种情况的出现
268
+ * @param schema
269
+ * @returns
270
+ * 如果有的对象允许删除,需要使用trigger来处理其相关联的外键对象,这些trigger写作before,则会在checker之前执行,仍然可以删除成功
271
+ */
272
+ function createRemoveCheckers(schema) {
273
+ var e_1, _a;
274
+ var checkers = [];
275
+ // 先建立所有的一对多的关系
276
+ var OneToManyMatrix = {};
277
+ var OneToManyOnEntityMatrix = {};
278
+ var addToMto = function (e, f, attr) {
279
+ var _a;
280
+ if (OneToManyMatrix[f]) {
281
+ (_a = OneToManyMatrix[f]) === null || _a === void 0 ? void 0 : _a.push([e, attr]);
282
+ }
283
+ else {
284
+ OneToManyMatrix[f] = [[e, attr]];
285
+ }
286
+ };
287
+ var addToMtoEntity = function (e, fs) {
288
+ var e_2, _a;
289
+ var _b;
290
+ try {
291
+ for (var fs_1 = tslib_1.__values(fs), fs_1_1 = fs_1.next(); !fs_1_1.done; fs_1_1 = fs_1.next()) {
292
+ var f = fs_1_1.value;
293
+ if (!OneToManyOnEntityMatrix[f]) {
294
+ OneToManyOnEntityMatrix[f] = [e];
295
+ }
296
+ else {
297
+ (_b = OneToManyOnEntityMatrix[f]) === null || _b === void 0 ? void 0 : _b.push(e);
298
+ }
299
+ }
300
+ }
301
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
302
+ finally {
303
+ try {
304
+ if (fs_1_1 && !fs_1_1.done && (_a = fs_1.return)) _a.call(fs_1);
305
+ }
306
+ finally { if (e_2) throw e_2.error; }
307
+ }
308
+ };
309
+ for (var entity in schema) {
310
+ if (['operEntity', 'modiEntity', 'userEntityGrant'].includes(entity)) {
311
+ continue; // 系统功能性数据,不用处理
312
+ }
313
+ var attributes = schema[entity].attributes;
314
+ for (var attr in attributes) {
315
+ if (attributes[attr].type === 'ref') {
316
+ addToMto(entity, attributes[attr].ref, attr);
317
+ }
318
+ else if (attr === 'entity') {
319
+ if (attributes[attr].ref) {
320
+ addToMtoEntity(entity, attributes[attr].ref);
321
+ }
322
+ else if (process.env.NODE_ENV === 'development') {
323
+ console.warn("".concat(entity, "\u7684entity\u53CD\u6307\u6307\u9488\u627E\u4E0D\u5230\u6709\u6548\u7684\u5BF9\u8C61"));
324
+ }
325
+ }
326
+ }
327
+ }
328
+ // 当删除一时,要确认多上面没有指向一的数据
329
+ var entities = (0, lodash_1.union)(Object.keys(OneToManyMatrix), Object.keys(OneToManyOnEntityMatrix));
330
+ var _loop_1 = function (entity) {
331
+ checkers.push({
332
+ entity: entity,
333
+ action: 'remove',
334
+ type: 'logical',
335
+ priority: types_1.CHECKER_MAX_PRIORITY,
336
+ checker: function (operation, context, option) {
337
+ var e_3, _a, e_4, _b;
338
+ var promises = [];
339
+ if (OneToManyMatrix[entity]) {
340
+ var _loop_2 = function (otm) {
341
+ var _g, _h;
342
+ var _j = tslib_1.__read(otm, 2), e = _j[0], attr = _j[1];
343
+ var proj = (_g = {
344
+ id: 1
345
+ },
346
+ _g[attr] = 1,
347
+ _g);
348
+ var filter = operation.filter && (_h = {},
349
+ _h[attr.slice(0, attr.length - 2)] = operation.filter,
350
+ _h);
351
+ var result = context.select(e, {
352
+ data: proj,
353
+ filter: filter,
354
+ indexFrom: 0,
355
+ count: 1
356
+ }, { dontCollect: true });
357
+ if (result instanceof Promise) {
358
+ promises.push(result.then(function (_a) {
359
+ var _b = tslib_1.__read(_a, 1), row = _b[0];
360
+ if (row) {
361
+ var err = new Exception_1.OakRowInconsistencyException(undefined, "\u60A8\u65E0\u6CD5\u5220\u9664\u5B58\u5728\u6709\u6548\u6570\u636E\u300C".concat(e, "\u300D\u5173\u8054\u7684\u884C"));
362
+ err.addData(e, [row]);
363
+ throw err;
364
+ }
365
+ }));
366
+ }
367
+ else {
368
+ var _k = tslib_1.__read(result, 1), row = _k[0];
369
+ if (row) {
370
+ var err = new Exception_1.OakRowInconsistencyException(undefined, "\u60A8\u65E0\u6CD5\u5220\u9664\u5B58\u5728\u6709\u6548\u6570\u636E\u300C".concat(e, "\u300D\u5173\u8054\u7684\u884C"));
371
+ err.addData(e, [row]);
372
+ throw err;
373
+ }
374
+ }
375
+ };
376
+ try {
377
+ for (var _c = (e_3 = void 0, tslib_1.__values(OneToManyMatrix[entity])), _d = _c.next(); !_d.done; _d = _c.next()) {
378
+ var otm = _d.value;
379
+ _loop_2(otm);
380
+ }
381
+ }
382
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
383
+ finally {
384
+ try {
385
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
386
+ }
387
+ finally { if (e_3) throw e_3.error; }
388
+ }
389
+ }
390
+ if (OneToManyOnEntityMatrix[entity]) {
391
+ var _loop_3 = function (otm) {
392
+ var _l, _m, _o;
393
+ var proj = {
394
+ id: 1,
395
+ entity: 1,
396
+ entityId: 1,
397
+ };
398
+ var filter = operation.filter && (_l = {},
399
+ _l[entity] = operation.filter,
400
+ _l);
401
+ var result = context.select(otm, {
402
+ data: proj,
403
+ filter: filter,
404
+ indexFrom: 0,
405
+ count: 1
406
+ }, { dontCollect: true });
407
+ if (result instanceof Promise) {
408
+ promises.push(result.then(function (_a) {
409
+ var _b = tslib_1.__read(_a, 1), row = _b[0];
410
+ if (row) {
411
+ var e = new Exception_1.OakRowInconsistencyException(undefined, "\u60A8\u65E0\u6CD5\u5220\u9664\u5B58\u5728\u6709\u6548\u6570\u636E\u300C".concat(otm, "\u300D\u5173\u8054\u7684\u884C"));
412
+ e.addData(otm, [row]);
413
+ throw e;
414
+ }
415
+ }));
416
+ }
417
+ else {
418
+ var _p = tslib_1.__read(result, 1), row = _p[0];
419
+ if (row) {
420
+ var record = {
421
+ a: 's',
422
+ d: (_m = {},
423
+ _m[otm] = (_o = {},
424
+ _o[row.id] = row,
425
+ _o),
426
+ _m)
427
+ };
428
+ var e = new Exception_1.OakRowInconsistencyException(undefined, "\u60A8\u65E0\u6CD5\u5220\u9664\u5B58\u5728\u6709\u6548\u6570\u636E\u300C".concat(otm, "\u300D\u5173\u8054\u7684\u884C"));
429
+ e.addData(otm, [row]);
430
+ throw e;
431
+ }
432
+ }
433
+ };
434
+ try {
435
+ for (var _e = (e_4 = void 0, tslib_1.__values(OneToManyOnEntityMatrix[entity])), _f = _e.next(); !_f.done; _f = _e.next()) {
436
+ var otm = _f.value;
437
+ _loop_3(otm);
438
+ }
439
+ }
440
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
441
+ finally {
442
+ try {
443
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
444
+ }
445
+ finally { if (e_4) throw e_4.error; }
446
+ }
447
+ }
448
+ if (promises.length > 0) {
449
+ return Promise.all(promises).then(function () { return undefined; });
450
+ }
451
+ }
452
+ });
453
+ };
454
+ try {
455
+ for (var entities_1 = tslib_1.__values(entities), entities_1_1 = entities_1.next(); !entities_1_1.done; entities_1_1 = entities_1.next()) {
456
+ var entity = entities_1_1.value;
457
+ _loop_1(entity);
458
+ }
459
+ }
460
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
461
+ finally {
462
+ try {
463
+ if (entities_1_1 && !entities_1_1.done && (_a = entities_1.return)) _a.call(entities_1);
464
+ }
465
+ finally { if (e_1) throw e_1.error; }
466
+ }
467
+ return checkers;
468
+ }
469
+ exports.createRemoveCheckers = createRemoveCheckers;
470
+ function checkAttributeLegal(schema, entity, data) {
471
+ var _a;
472
+ var _b;
473
+ var attributes = schema[entity].attributes;
474
+ for (var attr in data) {
475
+ if (attributes[attr]) {
476
+ var _c = attributes[attr], type = _c.type, params = _c.params, defaultValue = _c.default, enumeration = _c.enumeration, notNull = _c.notNull;
477
+ if (data[attr] === null || data[attr] === undefined) {
478
+ if (notNull && defaultValue === undefined) {
479
+ throw new Exception_1.OakAttrNotNullException(entity, [attr]);
480
+ }
481
+ if (defaultValue !== undefined) {
482
+ Object.assign(data, (_a = {},
483
+ _a[attr] = defaultValue,
484
+ _a));
485
+ }
486
+ continue;
487
+ }
488
+ switch (type) {
489
+ case 'char':
490
+ case 'varchar': {
491
+ if (typeof data[attr] !== 'string') {
492
+ throw new Exception_1.OakInputIllegalException(entity, [attr], 'not a string');
493
+ }
494
+ var length_1 = params.length;
495
+ if (length_1 && data[attr].length > length_1) {
496
+ throw new Exception_1.OakInputIllegalException(entity, [attr], 'too long');
497
+ }
498
+ break;
499
+ }
500
+ case 'int':
501
+ case 'smallint':
502
+ case 'tinyint':
503
+ case 'bigint':
504
+ case 'decimal':
505
+ case 'money': {
506
+ if (typeof data[attr] !== 'number') {
507
+ throw new Exception_1.OakInputIllegalException(entity, [attr], 'not a number');
508
+ }
509
+ var _d = params || {}, min = _d.min, max = _d.max;
510
+ if (typeof min === 'number' && data[attr] < min) {
511
+ throw new Exception_1.OakInputIllegalException(entity, [attr], 'too small');
512
+ }
513
+ if (typeof max === 'number' && data[attr] > max) {
514
+ throw new Exception_1.OakInputIllegalException(entity, [attr], 'too big');
515
+ }
516
+ break;
517
+ }
518
+ case 'enum': {
519
+ (0, assert_1.default)(enumeration);
520
+ if (!enumeration.includes(data[attr])) {
521
+ throw new Exception_1.OakInputIllegalException(entity, [attr], 'not in enumberation');
522
+ }
523
+ break;
524
+ }
525
+ }
526
+ }
527
+ else {
528
+ // 这里似乎还有一种update中带cascade remove的case,等遇到再说(貌似cascadeUpdate没有处理完整这种情况) by Xc
529
+ if (typeof data[attr] === 'object' && ((_b = data[attr]) === null || _b === void 0 ? void 0 : _b.action) === 'remove') {
530
+ console.warn('cascade remove可能是未处理的边界,请注意');
531
+ }
532
+ }
533
+ }
534
+ }
535
+ function createCreateCheckers(schema) {
536
+ var checkers = [];
537
+ var _loop_4 = function (entity) {
538
+ var _a = schema[entity], attributes = _a.attributes, actions = _a.actions;
539
+ var notNullAttrs = Object.keys(attributes).filter(function (ele) { return attributes[ele].notNull; });
540
+ var updateActions = (0, lodash_1.difference)(actions, action_1.excludeUpdateActions);
541
+ checkers.push({
542
+ entity: entity,
543
+ type: 'data',
544
+ action: 'create',
545
+ checker: function (data) {
546
+ var checkData = function (data2) {
547
+ var e_5, _a, e_6, _b;
548
+ var illegalNullAttrs = (0, lodash_1.difference)(notNullAttrs, Object.keys(data2));
549
+ if (illegalNullAttrs.length > 0) {
550
+ try {
551
+ // 要处理多对一的cascade create
552
+ for (var illegalNullAttrs_1 = (e_5 = void 0, tslib_1.__values(illegalNullAttrs)), illegalNullAttrs_1_1 = illegalNullAttrs_1.next(); !illegalNullAttrs_1_1.done; illegalNullAttrs_1_1 = illegalNullAttrs_1.next()) {
553
+ var attr = illegalNullAttrs_1_1.value;
554
+ if (attr === 'entityId') {
555
+ if (illegalNullAttrs.includes('entity')) {
556
+ continue;
557
+ }
558
+ }
559
+ else if (attr === 'entity' && attributes[attr].ref) {
560
+ var hasCascadeCreate = false;
561
+ try {
562
+ for (var _c = (e_6 = void 0, tslib_1.__values(attributes[attr].ref)), _d = _c.next(); !_d.done; _d = _c.next()) {
563
+ var ref = _d.value;
564
+ if (data2[ref] && data2[ref].action === 'create') {
565
+ hasCascadeCreate = true;
566
+ break;
567
+ }
568
+ }
569
+ }
570
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
571
+ finally {
572
+ try {
573
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
574
+ }
575
+ finally { if (e_6) throw e_6.error; }
576
+ }
577
+ if (hasCascadeCreate) {
578
+ continue;
579
+ }
580
+ }
581
+ else if (attributes[attr].type === 'ref') {
582
+ var ref = attributes[attr].ref;
583
+ var attr2 = attr.slice(0, attr.length - 2);
584
+ if (data2[attr2] && data2[attr2].action === 'create') {
585
+ continue;
586
+ }
587
+ }
588
+ // 到这里说明确实是有not null的属性没有赋值
589
+ throw new Exception_1.OakAttrNotNullException(entity, illegalNullAttrs);
590
+ }
591
+ }
592
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
593
+ finally {
594
+ try {
595
+ if (illegalNullAttrs_1_1 && !illegalNullAttrs_1_1.done && (_a = illegalNullAttrs_1.return)) _a.call(illegalNullAttrs_1);
596
+ }
597
+ finally { if (e_5) throw e_5.error; }
598
+ }
599
+ }
600
+ checkAttributeLegal(schema, entity, data2);
601
+ };
602
+ if (data instanceof Array) {
603
+ data.forEach(function (ele) { return checkData(ele); });
604
+ }
605
+ else {
606
+ checkData(data);
607
+ }
608
+ }
609
+ }, {
610
+ entity: entity,
611
+ type: 'data',
612
+ action: updateActions,
613
+ checker: function (data) {
614
+ checkAttributeLegal(schema, entity, data);
615
+ }
616
+ });
617
+ };
618
+ for (var entity in schema) {
619
+ _loop_4(entity);
620
+ }
621
+ return checkers;
622
+ }
623
+ exports.createCreateCheckers = createCreateCheckers;