oak-domain 2.5.2 → 2.6.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 (171) 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/ActionDefDict.d.ts +8 -8
  6. package/lib/base-app-domain/ActionDefDict.js +9 -9
  7. package/lib/base-app-domain/EntityDict.d.ts +14 -14
  8. package/lib/base-app-domain/EntityDict.js +2 -2
  9. package/lib/base-app-domain/Modi/Action.d.ts +10 -10
  10. package/lib/base-app-domain/Modi/Action.js +14 -14
  11. package/lib/base-app-domain/Modi/Schema.d.ts +146 -146
  12. package/lib/base-app-domain/Modi/Schema.js +2 -2
  13. package/lib/base-app-domain/Modi/Storage.d.ts +3 -3
  14. package/lib/base-app-domain/Modi/Storage.js +60 -60
  15. package/lib/base-app-domain/ModiEntity/Schema.d.ts +189 -189
  16. package/lib/base-app-domain/ModiEntity/Schema.js +2 -2
  17. package/lib/base-app-domain/ModiEntity/Storage.d.ts +3 -3
  18. package/lib/base-app-domain/ModiEntity/Storage.js +27 -27
  19. package/lib/base-app-domain/Oper/Schema.d.ts +144 -144
  20. package/lib/base-app-domain/Oper/Schema.js +2 -2
  21. package/lib/base-app-domain/Oper/Storage.d.ts +3 -3
  22. package/lib/base-app-domain/Oper/Storage.js +29 -29
  23. package/lib/base-app-domain/OperEntity/Schema.d.ts +205 -205
  24. package/lib/base-app-domain/OperEntity/Schema.js +2 -2
  25. package/lib/base-app-domain/OperEntity/Storage.d.ts +3 -3
  26. package/lib/base-app-domain/OperEntity/Storage.js +27 -27
  27. package/lib/base-app-domain/Storage.d.ts +3 -3
  28. package/lib/base-app-domain/Storage.js +17 -17
  29. package/lib/base-app-domain/User/Action.d.ts +10 -10
  30. package/lib/base-app-domain/User/Action.js +12 -12
  31. package/lib/base-app-domain/User/Schema.d.ts +180 -180
  32. package/lib/base-app-domain/User/Schema.js +2 -2
  33. package/lib/base-app-domain/User/Storage.d.ts +3 -3
  34. package/lib/base-app-domain/User/Storage.js +36 -36
  35. package/lib/base-app-domain/UserEntityGrant/Schema.d.ts +121 -121
  36. package/lib/base-app-domain/UserEntityGrant/Schema.js +2 -2
  37. package/lib/base-app-domain/UserEntityGrant/Storage.d.ts +3 -3
  38. package/lib/base-app-domain/UserEntityGrant/Storage.js +28 -28
  39. package/lib/base-app-domain/_SubQuery.d.ts +44 -44
  40. package/lib/base-app-domain/_SubQuery.js +2 -2
  41. package/lib/base-app-domain/index.d.ts +3 -3
  42. package/lib/base-app-domain/index.js +6 -6
  43. package/lib/checkers/index.d.ts +5 -5
  44. package/lib/checkers/index.js +16 -16
  45. package/lib/compiler/env.d.ts +11 -11
  46. package/lib/compiler/env.js +39 -39
  47. package/lib/compiler/schemalBuilder.d.ts +2 -2
  48. package/lib/compiler/schemalBuilder.js +3355 -3355
  49. package/lib/compiler/uiBuilder.d.ts +1 -1
  50. package/lib/compiler/uiBuilder.js +3 -3
  51. package/lib/entities/Modi.d.ts +11 -11
  52. package/lib/entities/Modi.js +46 -46
  53. package/lib/entities/ModiEntity.d.ts +8 -8
  54. package/lib/entities/ModiEntity.js +15 -15
  55. package/lib/entities/Oper.d.ts +10 -10
  56. package/lib/entities/Oper.js +17 -17
  57. package/lib/entities/OperEntity.d.ts +8 -8
  58. package/lib/entities/OperEntity.js +15 -15
  59. package/lib/entities/User.d.ts +8 -8
  60. package/lib/entities/User.js +28 -28
  61. package/lib/entities/UserEntityGrant.d.ts +7 -7
  62. package/lib/entities/UserEntityGrant.js +12 -12
  63. package/lib/store/AsyncRowStore.d.ts +51 -48
  64. package/lib/store/AsyncRowStore.js +189 -183
  65. package/lib/store/CascadeStore.d.ts +91 -91
  66. package/lib/store/CascadeStore.js +1634 -1603
  67. package/lib/store/SyncRowStore.d.ts +29 -29
  68. package/lib/store/SyncRowStore.js +48 -48
  69. package/lib/store/TriggerExecutor.d.ts +32 -32
  70. package/lib/store/TriggerExecutor.js +532 -532
  71. package/lib/store/actionDef.d.ts +9 -9
  72. package/lib/store/actionDef.js +333 -333
  73. package/lib/store/checker.d.ts +26 -26
  74. package/lib/store/checker.js +829 -867
  75. package/lib/store/filter.d.ts +109 -109
  76. package/lib/store/filter.js +893 -893
  77. package/lib/store/modi.d.ts +13 -13
  78. package/lib/store/modi.js +216 -216
  79. package/lib/store/relation.d.ts +13 -13
  80. package/lib/store/relation.js +66 -66
  81. package/lib/store/selection.d.ts +19 -7
  82. package/lib/store/selection.js +265 -238
  83. package/lib/triggers/index.d.ts +6 -6
  84. package/lib/triggers/index.js +11 -11
  85. package/lib/triggers/modi.d.ts +5 -5
  86. package/lib/triggers/modi.js +72 -72
  87. package/lib/types/Action.d.ts +20 -20
  88. package/lib/types/Action.js +2 -2
  89. package/lib/types/AppLoader.d.ts +11 -11
  90. package/lib/types/AppLoader.js +10 -10
  91. package/lib/types/Aspect.d.ts +13 -12
  92. package/lib/types/Aspect.js +4 -4
  93. package/lib/types/Auth.d.ts +69 -69
  94. package/lib/types/Auth.js +2 -2
  95. package/lib/types/Connector.d.ts +27 -26
  96. package/lib/types/Connector.js +9 -9
  97. package/lib/types/Context.d.ts +7 -7
  98. package/lib/types/Context.js +3 -3
  99. package/lib/types/DataType.d.ts +18 -18
  100. package/lib/types/DataType.js +5 -5
  101. package/lib/types/Demand.d.ts +77 -77
  102. package/lib/types/Demand.js +9 -9
  103. package/lib/types/Endpoint.d.ts +11 -11
  104. package/lib/types/Endpoint.js +3 -3
  105. package/lib/types/Entity.d.ts +183 -183
  106. package/lib/types/Entity.js +14 -14
  107. package/lib/types/Exception.d.ts +100 -98
  108. package/lib/types/Exception.js +327 -283
  109. package/lib/types/Expression.d.ts +163 -163
  110. package/lib/types/Expression.js +427 -427
  111. package/lib/types/Geo.d.ts +18 -18
  112. package/lib/types/Geo.js +2 -2
  113. package/lib/types/Locale.d.ts +24 -24
  114. package/lib/types/Locale.js +2 -2
  115. package/lib/types/Logger.d.ts +5 -5
  116. package/lib/types/Logger.js +3 -3
  117. package/lib/types/Polyfill.d.ts +23 -23
  118. package/lib/types/Polyfill.js +2 -2
  119. package/lib/types/Port.d.ts +17 -17
  120. package/lib/types/Port.js +2 -2
  121. package/lib/types/RowStore.d.ts +12 -12
  122. package/lib/types/RowStore.js +34 -34
  123. package/lib/types/Storage.d.ts +56 -56
  124. package/lib/types/Storage.js +2 -2
  125. package/lib/types/Timer.d.ts +13 -13
  126. package/lib/types/Timer.js +2 -2
  127. package/lib/types/Trigger.d.ts +109 -109
  128. package/lib/types/Trigger.js +49 -49
  129. package/lib/types/Txn.d.ts +2 -2
  130. package/lib/types/Txn.js +3 -3
  131. package/lib/types/Watcher.d.ts +19 -19
  132. package/lib/types/Watcher.js +4 -4
  133. package/lib/types/index.d.ts +21 -21
  134. package/lib/types/index.js +24 -24
  135. package/lib/types/schema/DataTypes.d.ts +32 -32
  136. package/lib/types/schema/DataTypes.js +3 -3
  137. package/lib/utils/SimpleConnector.d.ts +35 -30
  138. package/lib/utils/SimpleConnector.js +134 -117
  139. package/lib/utils/assert.d.ts +5 -5
  140. package/lib/utils/assert.js +9 -9
  141. package/lib/utils/concurrent.d.ts +15 -15
  142. package/lib/utils/concurrent.js +89 -89
  143. package/lib/utils/cron.d.ts +1 -1
  144. package/lib/utils/cron.js +18 -18
  145. package/lib/utils/date.d.ts +1 -1
  146. package/lib/utils/date.js +18 -18
  147. package/lib/utils/geo.d.ts +4 -4
  148. package/lib/utils/geo.js +24 -24
  149. package/lib/utils/lodash.d.ts +20 -20
  150. package/lib/utils/lodash.js +55 -55
  151. package/lib/utils/random/random.d.ts +1 -1
  152. package/lib/utils/random/random.js +24 -24
  153. package/lib/utils/random/random.mp.d.ts +1 -1
  154. package/lib/utils/random/random.mp.js +25 -25
  155. package/lib/utils/random/random.web.d.ts +1 -1
  156. package/lib/utils/random/random.web.js +17 -17
  157. package/lib/utils/string.d.ts +22 -22
  158. package/lib/utils/string.js +70 -70
  159. package/lib/utils/url.d.ts +1 -0
  160. package/lib/utils/url.js +12 -0
  161. package/lib/utils/uuid.d.ts +12 -12
  162. package/lib/utils/uuid.js +194 -194
  163. package/lib/utils/validator.d.ts +23 -23
  164. package/lib/utils/validator.js +123 -123
  165. package/package.json +48 -48
  166. package/src/entities/Modi.ts +68 -68
  167. package/src/entities/ModiEntity.ts +24 -24
  168. package/src/entities/Oper.ts +28 -28
  169. package/src/entities/OperEntity.ts +24 -24
  170. package/src/entities/User.ts +44 -44
  171. package/src/entities/UserEntityGrant.ts +24 -24
@@ -1,893 +1,893 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkFilterRepel = exports.checkFilterContains = exports.makeTreeDescendantFilter = exports.makeTreeAncestorFilter = exports.same = exports.getRelevantIds = exports.repel = exports.contains = exports.judgeValueRelation = exports.combineFilters = exports.unionFilterSegment = exports.addFilterSegment = void 0;
4
- var tslib_1 = require("tslib");
5
- var assert_1 = tslib_1.__importDefault(require("assert"));
6
- var types_1 = require("../types");
7
- var lodash_1 = require("../utils/lodash");
8
- var relation_1 = require("./relation");
9
- function addFilterSegment() {
10
- var filters = [];
11
- for (var _i = 0; _i < arguments.length; _i++) {
12
- filters[_i] = arguments[_i];
13
- }
14
- var filter = {};
15
- filters.forEach(function (ele) {
16
- var _a, _b, _c, _d;
17
- if (ele) {
18
- for (var k in ele) {
19
- if (k === '$and') {
20
- if (filter.$and) {
21
- (_a = filter.$and).push.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(ele[k]), false));
22
- }
23
- else {
24
- filter.$and = ele[k];
25
- }
26
- }
27
- else if (k === '$or') {
28
- if (filter.$or) {
29
- (_b = filter.$or).push.apply(_b, tslib_1.__spreadArray([], tslib_1.__read(ele[k]), false));
30
- }
31
- else {
32
- filter.$or = ele[k];
33
- }
34
- }
35
- else if (filter.hasOwnProperty(k)) {
36
- if (filter.$and) {
37
- filter.$and.push((_c = {},
38
- _c[k] = ele[k],
39
- _c));
40
- }
41
- else {
42
- filter.$and = [
43
- (_d = {},
44
- _d[k] = ele[k],
45
- _d)
46
- ];
47
- }
48
- }
49
- else {
50
- filter[k] = ele[k];
51
- }
52
- }
53
- }
54
- });
55
- return filter;
56
- }
57
- exports.addFilterSegment = addFilterSegment;
58
- function unionFilterSegment() {
59
- var e_1, _a;
60
- var filters = [];
61
- for (var _i = 0; _i < arguments.length; _i++) {
62
- filters[_i] = arguments[_i];
63
- }
64
- var allOnlyOneOr = true;
65
- try {
66
- for (var filters_1 = tslib_1.__values(filters), filters_1_1 = filters_1.next(); !filters_1_1.done; filters_1_1 = filters_1.next()) {
67
- var f = filters_1_1.value;
68
- if (Object.keys(f).length > 1 || !f.$or) {
69
- allOnlyOneOr = false;
70
- break;
71
- }
72
- }
73
- }
74
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
75
- finally {
76
- try {
77
- if (filters_1_1 && !filters_1_1.done && (_a = filters_1.return)) _a.call(filters_1);
78
- }
79
- finally { if (e_1) throw e_1.error; }
80
- }
81
- if (allOnlyOneOr) {
82
- // 优化特殊情况,全部都是$or,直接合并
83
- var ors = filters.map(function (ele) { return ele.$or; });
84
- return {
85
- $or: ors.reduce(function (prev, next) { return prev.concat(next); }, [])
86
- };
87
- }
88
- return {
89
- $or: filters,
90
- };
91
- }
92
- exports.unionFilterSegment = unionFilterSegment;
93
- function combineFilters(filters, union) {
94
- if (union) {
95
- return unionFilterSegment.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(filters), false));
96
- }
97
- return addFilterSegment.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(filters), false));
98
- }
99
- exports.combineFilters = combineFilters;
100
- /**
101
- * 判断value1表达的单个属性查询与同属性上value2表达的查询是包容还是相斥
102
- * 相容即value1所表达的查询结果一定被value2表达的查询结果所包含,例如:
103
- * value1: {
104
- * $eq: 12
105
- * }
106
- * value2: {
107
- * $gt: 8,
108
- * }
109
- * 此时value1相容value2
110
- *
111
- * 相斥即value1所表达的查询结果与value2一定毫无联系,例如:
112
- * value1: {
113
- * $gt: 8,
114
- * }
115
- * value2: {
116
- * $lt: 2,
117
- * }
118
- *
119
- *
120
- * @param value1
121
- * @param value2
122
- * @attention: 1)这里的测试不够充分,有些算子之间的相容或相斥可能有遗漏, 2)有新的算子加入需要修改代码
123
- */
124
- function judgeValueRelation(value1, value2, contained) {
125
- if (typeof value1 === 'object') {
126
- var attr = Object.keys(value1)[0];
127
- if (['$gt', '$lt', '$gte', '$lte', '$eq', '$ne', '$startsWith', '$endsWith', '$includes'].includes(attr)) {
128
- switch (attr) {
129
- case '$gt': {
130
- if (contained) {
131
- // 包容
132
- if (typeof value2 === 'object') {
133
- var attr2 = Object.keys(value2)[0];
134
- return ['$gt', '$gte'].includes(attr2) && value2[attr2] <= value1.$gt;
135
- }
136
- return false;
137
- }
138
- // 互斥
139
- if (typeof value2 === 'object') {
140
- var attr2 = Object.keys(value2)[0];
141
- return ['$lt', '$lte', '$eq'].includes(attr2) && value2[attr2] <= value1.$gt
142
- || attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gt || ele > value1.$gt; }));
143
- }
144
- return value2 <= value1.$gt;
145
- }
146
- case '$gte': {
147
- if (contained) {
148
- // 包容
149
- if (typeof value2 === 'object') {
150
- var attr2 = Object.keys(value2)[0];
151
- return ['$gte'].includes(attr2) && value2[attr2] <= value1.$gte
152
- || ['$gt'].includes(attr2) && value2[attr2] < value1.$gte;
153
- }
154
- return false;
155
- }
156
- // 互斥
157
- if (typeof value2 === 'object') {
158
- var attr2 = Object.keys(value2)[0];
159
- return ['$lt'].includes(attr2) && value2[attr2] <= value1.$gte
160
- || ['$eq', '$lte'].includes(attr2) && value2[attr2] < value1.gte
161
- || attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gte || ele >= value1.$gte; }));
162
- }
163
- return value2 < value1.$gte;
164
- }
165
- case '$lt': {
166
- if (contained) {
167
- // 相容
168
- if (typeof value2 === 'object') {
169
- var attr2 = Object.keys(value2)[0];
170
- return ['$lt', '$lte'].includes(attr2) && value2[attr2] >= value1.$lt;
171
- }
172
- return false;
173
- }
174
- // 互斥
175
- if (typeof value2 === 'object') {
176
- var attr2 = Object.keys(value2)[0];
177
- return ['$gt', '$gte', '$eq'].includes(attr2) && value2[attr2] >= value1.$lt
178
- || attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gt || ele < value1.$lt; }));
179
- }
180
- return value2 >= value1.$gt;
181
- }
182
- case '$lte': {
183
- if (contained) {
184
- // 包容
185
- if (typeof value2 === 'object') {
186
- var attr2 = Object.keys(value2)[0];
187
- return ['$lte'].includes(attr2) && value2[attr2] >= value1.$lte
188
- || ['$lt'].includes(attr2) && value2[attr2] > value1.$lte;
189
- }
190
- return false;
191
- }
192
- // 互斥
193
- if (typeof value2 === 'object') {
194
- var attr2 = Object.keys(value2)[0];
195
- return ['$gt'].includes(attr2) && value2[attr2] >= value1.$lte
196
- || ['$eq', '$gte'].includes(attr2) && value2[attr2] > value1.lte
197
- || attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$lte || ele <= value1.$lte; }));
198
- }
199
- return value2 > value1.$gte;
200
- }
201
- case '$eq': {
202
- if (contained) {
203
- // 相容
204
- if (typeof value2 === 'object') {
205
- var attr2 = Object.keys(value2)[0];
206
- return attr2 === '$eq' && value2[attr2] === value1.$eq || attr2 === '$ne' && value2[attr2] !== value1.$eq
207
- || attr2 === '$gt' && value2[attr2] < value1.$eq || attr2 === '$lt' && value2[attr2] > value1.$eq
208
- || attr2 === '$gte' && value2[attr2] <= value1.$eq || attr2 === '$lte' && value2[attr2] >= value1.$eq
209
- || attr2 === '$startsWith' && value1.$eq.startsWith(value2[attr2])
210
- || attr2 === '$endsWith' && value1.$eq.endsWith(value2[attr2])
211
- || attr2 === '$includes' && value1.$eq.includes(value2[attr2])
212
- || attr2 === '$in' && value2[attr2] instanceof Array && value2[attr2].includes(value1.$eq)
213
- || attr2 === '$nin' && value2[attr2] instanceof Array && !value2[attr2].includes(value1.$eq)
214
- || attr2 === '$between' && value2[attr2][0] <= value1.$eq && value2[attr2][1] >= value1.$eq
215
- || attr2 === '$exists' && value2[attr2] === true;
216
- }
217
- return value2 === value1.$eq;
218
- }
219
- // 互斥
220
- if (typeof value2 === 'object') {
221
- var attr2 = Object.keys(value2)[0];
222
- return attr2 === '$eq' && value2[attr2] !== value1.$eq || attr2 === '$gt' && value2[attr2] >= value1.$eq
223
- || attr2 === '$lt' && value2[attr2] <= value1.$eq
224
- || attr2 === '$gte' && value2[attr2] > value1.$eq || attr2 === '$lte' && value2[attr2] < value1.$eq
225
- || attr2 === '$startsWith' && !value1.$eq.startsWith(value2[attr2])
226
- || attr2 === '$endsWith' && !value1.$eq.endsWith(value2[attr2])
227
- || attr2 === '$includes' && !value1.$eq.includes(value2[attr2])
228
- || attr2 === '$in' && value2[attr2] instanceof Array && !value2[attr2].includes(value1.$eq)
229
- || attr2 === '$between' && (value2[attr2][0] > value1.$eq || value2[attr2][1] < value1.$eq)
230
- || attr2 === '$exists' && value2[attr2] === false;
231
- }
232
- return value2 !== value1.$eq;
233
- }
234
- case '$ne': {
235
- if (contained) {
236
- // 相容
237
- if (typeof value2 === 'object') {
238
- var attr2 = Object.keys(value2)[0];
239
- return attr2 === '$ne' && value2[attr2] === value1.$ne;
240
- }
241
- return false;
242
- }
243
- // 互斥
244
- if (typeof value2 === 'object') {
245
- var attr2 = Object.keys(value2)[0];
246
- return attr2 === '$eq' && value2[attr2] === value1.$ne;
247
- }
248
- return value2 === value1.$ne;
249
- }
250
- case '$startsWith': {
251
- if (contained) {
252
- // 相容
253
- if (typeof value2 === 'object') {
254
- var attr2 = Object.keys(value2)[0];
255
- return attr2 === '$startsWith' && typeof (value2[attr2]) === 'string'
256
- && value1.$startsWith.startsWith(value2[attr2]);
257
- }
258
- return typeof value2 === 'string' && value1.$startsWith.startsWith(value2);
259
- }
260
- // 互斥
261
- if (typeof value2 === 'object') {
262
- var attr2 = Object.keys(value2)[0];
263
- return attr2 === '$startsWith' && typeof (value2[attr2]) === 'string'
264
- && !value1.$startsWith.startsWith(value2[attr2]) && !value2[attr2].startsWith(value1.$startsWith)
265
- || attr2 === '$eq' && !value2[attr2].startsWith(value1.$startsWith);
266
- }
267
- return !value2.startsWith(value1.$startsWith);
268
- }
269
- case '$endsWith': {
270
- if (contained) {
271
- // 相容
272
- if (typeof value2 === 'object') {
273
- var attr2 = Object.keys(value2)[0];
274
- return attr2 === '$endsWith' && typeof (value2[attr2]) === 'string'
275
- && value1.$startsWith.endsWith(value2[attr2]);
276
- }
277
- return typeof value2 === 'string' && value1.$startsWith.endsWith(value2);
278
- }
279
- // 互斥
280
- if (typeof value2 === 'object') {
281
- var attr2 = Object.keys(value2)[0];
282
- return attr2 === '$startsWith' && typeof (value2[attr2]) === 'string'
283
- && !value1.$startsWith.endsWith(value2[attr2]) && !value2[attr2].endsWith(value1.$startsWith)
284
- || attr2 === '$eq' && !value2[attr2].endsWith(value1.$startsWith);
285
- }
286
- return !value2.endsWith(value1.$startsWith);
287
- }
288
- case '$includes': {
289
- if (contained) {
290
- // 相容
291
- if (typeof value2 === 'object') {
292
- var attr2 = Object.keys(value2)[0];
293
- return ['$includes', '$startsWith', '$endsWith'].includes(attr2)
294
- && typeof (value2[attr2]) === 'string'
295
- && (value2[attr2]).includes(value1.$includes);
296
- }
297
- return typeof value2 === 'string' && value2.includes(value1.$includes);
298
- }
299
- // 互斥
300
- if (typeof value2 === 'object') {
301
- var attr2 = Object.keys(value2)[0];
302
- return attr2 === '$eq' && !value2[attr2].includes(value1.$includes)
303
- || attr2 === '$in' && value2[attr2] instanceof Array && !value2[attr2].find(function (ele) { return ele.includes(value1.$includes); });
304
- }
305
- return typeof value2 === 'string' && !value2.includes(value1.$includes);
306
- }
307
- default: {
308
- (0, assert_1.default)(false, "\u4E0D\u80FD\u5904\u7406\u7684\u7B97\u5B50\u300C".concat(attr, "\u300D"));
309
- }
310
- }
311
- }
312
- else if (['$exists'].includes(attr)) {
313
- (0, assert_1.default)(attr === '$exists');
314
- if (contained) {
315
- if (typeof value2 === 'object') {
316
- var attr2 = Object.keys(value2)[0];
317
- return attr2 === '$exists' && value2[attr2] === value1.$exists;
318
- }
319
- return false;
320
- }
321
- return typeof value2 === 'object' && value2.$exists === !(value1.$exists);
322
- }
323
- else if (['$in', '$nin', '$between'].includes(attr)) {
324
- switch (attr) {
325
- case '$in': {
326
- if (contained) {
327
- // 相容
328
- if (value1.$in instanceof Array) {
329
- if (typeof value2 === 'object') {
330
- var attr2 = Object.keys(value2)[0];
331
- if (attr2 === '$in') {
332
- return value2[attr2] instanceof Array && (0, lodash_1.difference)(value1.$in, value2[attr2]).length === 0;
333
- }
334
- else if (attr2 === '$nin') {
335
- return value2[attr2] instanceof Array && (0, lodash_1.intersection)(value1.$in, value2[attr2]).length === 0;
336
- }
337
- else if (attr2 === '$exists') {
338
- return value2[attr2] === true;
339
- }
340
- else if (['$gt', '$gte', '$lt', '$lte', '$between'].includes(attr2)) {
341
- var min33_1, max33_1;
342
- value1.$in.forEach(function (ele) {
343
- if (!min33_1 || min33_1 > ele) {
344
- min33_1 = ele;
345
- }
346
- if (!max33_1 || max33_1 < ele) {
347
- max33_1 = ele;
348
- }
349
- });
350
- return attr2 === '$gt' && value2[attr2] < min33_1 || attr2 === '$gte' && value2[attr2] <= min33_1
351
- || attr2 === '$lt' && value2[attr2] > max33_1 || attr2 === '$lte' && value2[attr2] >= max33_1
352
- || attr2 === '$between' && value2[attr2][0] < min33_1 && value2[attr2][1] > max33_1;
353
- }
354
- }
355
- }
356
- return false;
357
- }
358
- // 相斥
359
- if (value1.$in instanceof Array) {
360
- if (typeof value2 === 'object') {
361
- var attr2 = Object.keys(value2)[0];
362
- if (attr2 === '$in') {
363
- return (0, lodash_1.intersection)(value2[attr2], value1.$in).length === 0;
364
- }
365
- else if (attr2 === '$eq') {
366
- return !value1.$in.includes(value2[attr2]);
367
- }
368
- else if (attr2 === '$exists') {
369
- return value2[attr2] === false;
370
- }
371
- else if (['$gt', '$gte', '$lt', '$lte', '$between'].includes(attr2)) {
372
- var min44_1, max44_1;
373
- value1.$in.forEach(function (ele) {
374
- if (!min44_1 || min44_1 > ele) {
375
- min44_1 = ele;
376
- }
377
- if (!max44_1 || max44_1 < ele) {
378
- max44_1 = ele;
379
- }
380
- });
381
- return attr2 === '$gt' && value2[attr2] >= max44_1 || attr2 === '$gte' && value2[attr2] > max44_1
382
- || attr2 === '$lt' && value2[attr2] <= min44_1 || attr2 === '$lte' && value2[attr2] < min44_1
383
- || attr2 === '$between' && (value2[attr2][0] > max44_1 || value2[attr2][1] < min44_1);
384
- }
385
- }
386
- return !value1.$in.includes(value2);
387
- }
388
- return false;
389
- }
390
- case '$nin': {
391
- if (contained) {
392
- // 相容
393
- if (value1.$nin instanceof Array) {
394
- if (typeof value2 === 'object') {
395
- var attr2 = Object.keys(value2)[0];
396
- if (attr2 === '$nin') {
397
- return value2[attr2] instanceof Array && (0, lodash_1.intersection)(value2[attr2], value1.$nin).length === 0;
398
- }
399
- }
400
- }
401
- return false;
402
- }
403
- // 相斥
404
- if (value1.$nin instanceof Array) {
405
- if (typeof value2 === 'object') {
406
- var attr2 = Object.keys(value2)[0];
407
- if (attr2 === '$in') {
408
- return value2[attr2] instanceof Array && (0, lodash_1.difference)(value2[attr2], value1.$nin).length === 0;
409
- }
410
- }
411
- }
412
- return false;
413
- }
414
- case '$between': {
415
- (0, assert_1.default)(value1.$between instanceof Array);
416
- if (contained) {
417
- // 相容
418
- if (typeof value2 === 'object') {
419
- var attr2 = Object.keys(value2)[0];
420
- if (['$gt', '$gte', '$lt', '$lte', '$between', '$eq'].includes(attr2)) {
421
- return attr2 === '$between' && value2[attr2][0] <= value1.$between[0] && value2[attr2][1] >= value1.$between[1]
422
- || attr2 === '$gt' && value2[attr2] < value1.$between[0] || attr2 === '$gte' && value2[attr2] <= value1.$between[0]
423
- || attr2 === '$lt' && value2[attr2] > value1.$between[1] || attr2 === '$lte' && value2[attr2] >= value1.$between[1];
424
- }
425
- else if (attr2 === '$exists') {
426
- return value2[attr2] === true;
427
- }
428
- }
429
- return false;
430
- }
431
- // 相斥
432
- if (typeof value2 === 'object') {
433
- var attr2 = Object.keys(value2)[0];
434
- if (['$gt', '$gte', '$lt', '$lte', '$between', '$eq'].includes(attr2)) {
435
- return attr2 === '$between' && (value2[attr2][0] > value1.$between[1] || value2[attr2][1] < value1.$between[0])
436
- || attr2 === '$gt' && value2[attr2] > value1.$between[1] || attr2 === '$gte' && value2[attr2] >= value1.$between[1]
437
- || attr2 === '$lt' && value2[attr2] < value1.$between[0] || attr2 === '$lte' && value2[attr2] <= value1.$between[0]
438
- || attr2 === '$eq' && (value2[attr2] > value1.$between[1] || value2[attr2] < value1.$between[0]);
439
- }
440
- else if (attr2 === '$exists') {
441
- return value2[attr2] === false;
442
- }
443
- else if (attr2 === '$in' && value2[attr2] instanceof Array) {
444
- return !value2[attr2].find(function (ele) { return ele >= value1.$between[0] && ele <= value1.$between[1]; });
445
- }
446
- return false;
447
- }
448
- }
449
- default: {
450
- (0, assert_1.default)(false, "\u6682\u4E0D\u652F\u6301\u7684\u7B97\u5B50".concat(attr));
451
- }
452
- }
453
- }
454
- else {
455
- return false;
456
- }
457
- }
458
- else {
459
- // value1是一个等值查询
460
- if (contained) {
461
- // 相容
462
- if (typeof value2 === 'object') {
463
- var attr2 = Object.keys(value2)[0];
464
- return attr2 === '$eq' && value2[attr2] === value1 || attr2 === '$ne' && value2[attr2] !== value1
465
- || attr2 === '$gt' && value2[attr2] < value1 || attr2 === '$lt' && value2[attr2] > value1
466
- || attr2 === '$gte' && value2[attr2] <= value1 || attr2 === '$lte' && value2[attr2] >= value1
467
- || attr2 === '$startsWith' && value1.startsWith(value2[attr2])
468
- || attr2 === '$endsWith' && value1.endsWith(value2[attr2])
469
- || attr2 === '$includes' && value1.includes(value2[attr2])
470
- || attr2 === '$in' && value2[attr2] instanceof Array && value2[attr2].includes(value1)
471
- || attr2 === '$nin' && value2[attr2] instanceof Array && !value2[attr2].includes(value1)
472
- || attr2 === '$between' && value2[attr2][0] <= value1 && value2[attr2][1] >= value1
473
- || attr2 === '$exists' && value2[attr2] === true;
474
- }
475
- return value2 === value1;
476
- }
477
- // 互斥
478
- if (typeof value2 === 'object') {
479
- var attr2 = Object.keys(value2)[0];
480
- return attr2 === '$eq' && value2[attr2] !== value1 || attr2 === '$gt' && value2[attr2] >= value1
481
- || attr2 === '$lt' && value2[attr2] <= value1
482
- || attr2 === '$gte' && value2[attr2] > value1 || attr2 === '$lte' && value2[attr2] < value1
483
- || attr2 === '$startsWith' && !value1.startsWith(value2[attr2])
484
- || attr2 === '$endsWith' && !value1.endsWith(value2[attr2])
485
- || attr2 === '$includes' && !value1.includes(value2[attr2])
486
- || attr2 === '$in' && value2[attr2] instanceof Array && !value2[attr2].includes(value1)
487
- || attr2 === '$between' && (value2[attr2][0] > value1 || value2[attr2][1] < value1)
488
- || attr2 === '$exists' && value2[attr2] === false;
489
- }
490
- return value2 !== value1;
491
- }
492
- }
493
- exports.judgeValueRelation = judgeValueRelation;
494
- function judgeFilter2ValueRelation(entity, schema, attr, filter, conditionalFilterAttrValue, contained) {
495
- var _a;
496
- for (var attr2 in filter) {
497
- if (['$and', '$or', '$not'].includes(attr2)) {
498
- switch (attr2) {
499
- case '$and':
500
- case '$or':
501
- case '$xor': {
502
- var logicQueries = filter[attr2];
503
- var results = logicQueries.map(function (logicQuery) { return judgeFilter2ValueRelation(entity, schema, attr, logicQuery, conditionalFilterAttrValue, contained); });
504
- // 如果filter的多个算子是and关系,则只要有一个包含此条件就是包含,只要有一个与此条件相斥就是相斥
505
- // 如果filter的多个算子是or关系,则必须所有的条件都包含此条件才是包含,必须所有的条件都与此条件相斥才是相斥
506
- if (attr2 === '$and') {
507
- if (results.includes(true)) {
508
- return true;
509
- }
510
- }
511
- else if (attr2 === '$or') {
512
- if (!results.includes(false)) {
513
- return true;
514
- }
515
- }
516
- else {
517
- (0, assert_1.default)(false);
518
- }
519
- break;
520
- }
521
- case '$not': {
522
- /*
523
- * 若filter的not条件被conditionalFilterAttrValue条件包容,则说明两者互斥
524
- * filter包容conditionalFilterAttrValue条件暂时无法由其not条件推论出来
525
- */
526
- var logicQuery = filter[attr2];
527
- if (!contained && judgeFilterRelation(entity, schema, logicQuery, (_a = {}, _a[attr] = conditionalFilterAttrValue, _a), true)) {
528
- return true;
529
- }
530
- break;
531
- }
532
- default: {
533
- (0, assert_1.default)(false);
534
- }
535
- }
536
- }
537
- else if (attr2.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
538
- return false;
539
- }
540
- else if (attr2.toLowerCase() === '$text') {
541
- return false;
542
- }
543
- else {
544
- if (attr === attr2) {
545
- var rel = (0, relation_1.judgeRelation)(schema, entity, attr2);
546
- if (rel === 1) {
547
- return judgeValueRelation(filter[attr2], conditionalFilterAttrValue, contained);
548
- }
549
- else if (rel === 2) {
550
- return judgeFilterRelation(attr2, schema, filter[attr2], conditionalFilterAttrValue, contained);
551
- }
552
- else if (typeof rel === 'string') {
553
- return judgeFilterRelation(rel, schema, filter[attr2], conditionalFilterAttrValue, contained);
554
- }
555
- else {
556
- (0, assert_1.default)(false);
557
- }
558
- }
559
- }
560
- }
561
- // 到这里说明无法判断相容或者相斥,安全起见全返回false
562
- return false;
563
- }
564
- /**
565
- * @param entity
566
- * @param schema
567
- * @param filter
568
- * @param conditionalFilter
569
- * @param contained: true代表filter包容conditionalFilter, false代表filter与conditionalFilter相斥
570
- */
571
- function judgeFilterRelation(entity, schema, filter, conditionalFilter, contained) {
572
- for (var attr in conditionalFilter) {
573
- if (['$and', '$or', '$not'].includes(attr)) {
574
- switch (attr) {
575
- case '$and':
576
- case '$or': {
577
- var logicQueries = conditionalFilter[attr];
578
- var results = logicQueries.map(function (logicQuery) { return judgeFilterRelation(entity, schema, filter, logicQuery, contained); });
579
- if (contained) {
580
- // 如果是包容关系,or和and需要全部被包容
581
- if (results.includes(false)) {
582
- return false;
583
- }
584
- }
585
- else if (!contained) {
586
- // 如果是相斥关系,and只需要和一个相斥,or需要和全部相斥
587
- if (attr === '$and' && results.includes(true) || attr === '$or' && !results.includes(false)) {
588
- return true;
589
- }
590
- }
591
- else {
592
- (0, assert_1.default)(false);
593
- }
594
- break;
595
- }
596
- case '$not': {
597
- /**
598
- * 若filter与conditionalFilter not所定义的部分相斥,则filter与conditionalFilter相容
599
- * 若filter将conditionalFilter not所定义的部分包容,则filter与conditionalFilter相斥
600
- */
601
- var logicQuery = conditionalFilter[attr];
602
- if (contained) {
603
- if (!judgeFilterRelation(entity, schema, filter, logicQuery, false)) {
604
- return false;
605
- }
606
- }
607
- else {
608
- if (judgeFilterRelation(entity, schema, filter, logicQuery, true)) {
609
- return true;
610
- }
611
- }
612
- break;
613
- }
614
- default: {
615
- throw new Error("\u6682\u4E0D\u652F\u6301\u7684\u903B\u8F91\u7B97\u5B50".concat(attr));
616
- }
617
- }
618
- }
619
- else if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
620
- return false;
621
- }
622
- else if (attr.toLowerCase() === '$text') {
623
- return false;
624
- }
625
- else {
626
- if (contained && !judgeFilter2ValueRelation(entity, schema, attr, filter, conditionalFilter[attr], contained)) {
627
- // 相容关系只要有一个不相容就不相容
628
- return false;
629
- }
630
- if (!contained && judgeFilter2ValueRelation(entity, schema, attr, filter, conditionalFilter[attr], contained)) {
631
- // 相斥关系只要有一个相斥就相斥
632
- return true;
633
- }
634
- }
635
- }
636
- // 到这里说明不能否定其相容(所以要返回相容),也不能肯定其相斥(所以要返回不相斥)
637
- return contained;
638
- }
639
- /**
640
- *
641
- * 判断filter是否包含conditionalFilter中的查询条件,即filter查询的结果一定满足conditionalFilter的约束
642
- * filter = {
643
- * a: 1
644
- * b: 2,
645
- * c: 3,
646
- * },
647
- * conditionalFilter = {
648
- * a: 1
649
- * }
650
- * 则包含
651
- * @param entity
652
- * @param schema
653
- * @param filter
654
- * @param conditionalFilter
655
- * @returns
656
- */
657
- function contains(entity, schema, filter, conditionalFilter) {
658
- return judgeFilterRelation(entity, schema, filter, conditionalFilter, true);
659
- // return false;
660
- }
661
- exports.contains = contains;
662
- /**
663
- * 判断filter1和filter2是否相斥,即filter1和filter2查询的结果一定没有交集
664
- * filter1 = {
665
- * a: 2
666
- * },
667
- * filter2 = {
668
- * a: 1
669
- * }
670
- * 则包含
671
- * @param entity
672
- * @param schema
673
- * @param filter
674
- * @param conditionalFilter
675
- */
676
- function repel(entity, schema, filter1, filter2) {
677
- // todo
678
- return judgeFilterRelation(entity, schema, filter1, filter2, false);
679
- // return false;
680
- }
681
- exports.repel = repel;
682
- /**
683
- * 从filter中判断是否有确定的id对象,如果有则返回这些id,没有返回空数组
684
- * @param filter
685
- * @returns
686
- */
687
- function getRelevantIds(filter) {
688
- var _a, _b;
689
- var ids;
690
- var idsAnd;
691
- var idsOr;
692
- if (!filter) {
693
- return [];
694
- }
695
- // 因为要准确判定id,如果有其它的过滤条件,可能会使实际处理的行数少于id指向的行数,只能返回空数组
696
- var attrs = Object.keys(filter);
697
- if ((0, lodash_1.intersection)(attrs, ['id', '$and', '$or']).length > 3) {
698
- return [];
699
- }
700
- if (filter === null || filter === void 0 ? void 0 : filter.$and) {
701
- var idss = filter.$and.map(function (ele) { return getRelevantIds(ele); });
702
- idsAnd = lodash_1.intersection.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(idss), false));
703
- }
704
- if (filter === null || filter === void 0 ? void 0 : filter.$or) {
705
- var idss = filter.$or.map(function (ele) { return getRelevantIds(ele); });
706
- idsOr = lodash_1.union.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(idss), false));
707
- }
708
- if (filter === null || filter === void 0 ? void 0 : filter.id) {
709
- if (typeof filter.id === 'string') {
710
- ids = [filter.id];
711
- }
712
- if ((_a = filter.id) === null || _a === void 0 ? void 0 : _a.$eq) {
713
- ids = [filter.id.$eq];
714
- }
715
- if (((_b = filter.id) === null || _b === void 0 ? void 0 : _b.$in) && filter.id.$in instanceof Array) {
716
- ids = filter.id.$in;
717
- }
718
- }
719
- // 三者如果有基一,直接返回,如果大于一返回intersection
720
- if (!ids && !idsAnd && !idsOr) {
721
- return [];
722
- }
723
- var result = (ids || idsAnd || idsOr);
724
- if (ids) {
725
- result = (0, lodash_1.intersection)(result, ids);
726
- }
727
- if (idsAnd) {
728
- result = (0, lodash_1.intersection)(result, idsAnd);
729
- }
730
- if (idsOr) {
731
- result = (0, lodash_1.intersection)(result, idsOr);
732
- }
733
- return result;
734
- }
735
- exports.getRelevantIds = getRelevantIds;
736
- /**
737
- * 判断两个过滤条件是否完全一致
738
- * @param entity
739
- * @param schema
740
- * @param filter1
741
- * @param filter2
742
- */
743
- function same(entity, schema, filter1, filter2) {
744
- // 当前只需要判断是不是id相等就行了,在runningTree的operation合并的时间使用
745
- if (!filter1 || !filter1.id || Object.keys(filter1).length > 1 || !filter2 || !filter2.id || Object.keys(filter2).length > 1) {
746
- return false;
747
- }
748
- return filter1.id === filter2.id;
749
- }
750
- exports.same = same;
751
- /**
752
- * 寻找在树形结构中满足条件的数据行的上层数据
753
- * 例如在area表中,如果“杭州市”满足这一条件,则希望查到更高层的“浙江省”和“中国”,即可构造出满足条件的filter
754
- * @param entity
755
- * @param parentKey parentId属性名
756
- * @param filter 查询当前行的条件
757
- * @param level
758
- */
759
- function makeTreeAncestorFilter(entity, parentKey, filter, level, includeAll, includeSelf) {
760
- var _a;
761
- if (level === void 0) { level = 1; }
762
- (0, assert_1.default)(level >= 0);
763
- var idInFilters = [];
764
- if (includeSelf) {
765
- idInFilters.push(filter);
766
- }
767
- var currentLevelInFilter = filter;
768
- while (level > 0) {
769
- currentLevelInFilter = {
770
- id: {
771
- $in: {
772
- entity: entity,
773
- data: (_a = {},
774
- _a[parentKey] = 1,
775
- _a),
776
- filter: currentLevelInFilter,
777
- }
778
- },
779
- };
780
- if (includeAll) {
781
- idInFilters.push(currentLevelInFilter);
782
- }
783
- level--;
784
- }
785
- ;
786
- if (includeAll) {
787
- return {
788
- $or: idInFilters,
789
- };
790
- }
791
- return currentLevelInFilter;
792
- }
793
- exports.makeTreeAncestorFilter = makeTreeAncestorFilter;
794
- /**
795
- * 寻找在树形结构中满足条件的数据行的下层数据
796
- * 例如在area表中,如果“杭州市”满足这一条件,则希望查到更低层的“西湖区”,即可构造出满足条件的filter
797
- * @param entity
798
- * @param parentKey parentId属性名
799
- * @param filter 查询当前行的条件
800
- * @param level
801
- */
802
- function makeTreeDescendantFilter(entity, parentKey, filter, level, includeAll, includeSelf) {
803
- var _a;
804
- if (level === void 0) { level = 1; }
805
- (0, assert_1.default)(level >= 0);
806
- (0, assert_1.default)(parentKey.endsWith('Id'));
807
- var parentKeyRef = parentKey.slice(0, parentKey.length - 2);
808
- var idInFilters = [];
809
- if (includeSelf) {
810
- idInFilters.push(filter);
811
- }
812
- var currentLevelInFilter = filter;
813
- while (level > 0) {
814
- currentLevelInFilter = (_a = {},
815
- _a[parentKeyRef] = currentLevelInFilter,
816
- _a);
817
- if (includeAll) {
818
- idInFilters.push(currentLevelInFilter);
819
- }
820
- level--;
821
- }
822
- ;
823
- if (includeAll) {
824
- return {
825
- $or: idInFilters,
826
- };
827
- }
828
- return currentLevelInFilter;
829
- }
830
- exports.makeTreeDescendantFilter = makeTreeDescendantFilter;
831
- /**
832
- * 检查filter是否包含contained(filter查询的数据一定满足contained)
833
- * @param entity
834
- * @param context
835
- * @param contained
836
- * @param filter
837
- * @param dataCompare
838
- * @returns
839
- */
840
- function checkFilterContains(entity, context, contained, filter, dataCompare) {
841
- if (!filter) {
842
- throw new types_1.OakRowInconsistencyException();
843
- }
844
- var schema = context.getSchema();
845
- // 优先判断两个条件是否相容
846
- if (contains(entity, schema, filter, contained)) {
847
- return true;
848
- }
849
- if (dataCompare) {
850
- // 再判断加上了conditionalFilter后取得的行数是否缩减
851
- var filter2 = combineFilters([filter, {
852
- $not: contained,
853
- }]);
854
- var count = context.count(entity, {
855
- filter: filter2,
856
- }, {
857
- dontCollect: true,
858
- blockTrigger: true,
859
- });
860
- if (count instanceof Promise) {
861
- return count.then(function (count2) { return count2 === 0; });
862
- }
863
- return count === 0;
864
- }
865
- return false;
866
- }
867
- exports.checkFilterContains = checkFilterContains;
868
- function checkFilterRepel(entity, context, filter1, filter2, dataCompare) {
869
- if (!filter2) {
870
- throw new types_1.OakRowInconsistencyException();
871
- }
872
- var schema = context.getSchema();
873
- // 优先判断两个条件是否相容
874
- if (repel(entity, schema, filter2, filter1)) {
875
- return true;
876
- }
877
- // 再判断两者同时成立时取得的行数是否为0
878
- if (dataCompare) {
879
- var filter3 = combineFilters([filter2, filter1]);
880
- var count = context.count(entity, {
881
- filter: filter3,
882
- }, {
883
- dontCollect: true,
884
- blockTrigger: true,
885
- });
886
- if (count instanceof Promise) {
887
- return count.then(function (count2) { return count2 === 0; });
888
- }
889
- return count === 0;
890
- }
891
- return false;
892
- }
893
- exports.checkFilterRepel = checkFilterRepel;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkFilterRepel = exports.checkFilterContains = exports.makeTreeDescendantFilter = exports.makeTreeAncestorFilter = exports.same = exports.getRelevantIds = exports.repel = exports.contains = exports.judgeValueRelation = exports.combineFilters = exports.unionFilterSegment = exports.addFilterSegment = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var assert_1 = tslib_1.__importDefault(require("assert"));
6
+ var types_1 = require("../types");
7
+ var lodash_1 = require("../utils/lodash");
8
+ var relation_1 = require("./relation");
9
+ function addFilterSegment() {
10
+ var filters = [];
11
+ for (var _i = 0; _i < arguments.length; _i++) {
12
+ filters[_i] = arguments[_i];
13
+ }
14
+ var filter = {};
15
+ filters.forEach(function (ele) {
16
+ var _a, _b, _c, _d;
17
+ if (ele) {
18
+ for (var k in ele) {
19
+ if (k === '$and') {
20
+ if (filter.$and) {
21
+ (_a = filter.$and).push.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(ele[k]), false));
22
+ }
23
+ else {
24
+ filter.$and = ele[k];
25
+ }
26
+ }
27
+ else if (k === '$or') {
28
+ if (filter.$or) {
29
+ (_b = filter.$or).push.apply(_b, tslib_1.__spreadArray([], tslib_1.__read(ele[k]), false));
30
+ }
31
+ else {
32
+ filter.$or = ele[k];
33
+ }
34
+ }
35
+ else if (filter.hasOwnProperty(k)) {
36
+ if (filter.$and) {
37
+ filter.$and.push((_c = {},
38
+ _c[k] = ele[k],
39
+ _c));
40
+ }
41
+ else {
42
+ filter.$and = [
43
+ (_d = {},
44
+ _d[k] = ele[k],
45
+ _d)
46
+ ];
47
+ }
48
+ }
49
+ else {
50
+ filter[k] = ele[k];
51
+ }
52
+ }
53
+ }
54
+ });
55
+ return filter;
56
+ }
57
+ exports.addFilterSegment = addFilterSegment;
58
+ function unionFilterSegment() {
59
+ var e_1, _a;
60
+ var filters = [];
61
+ for (var _i = 0; _i < arguments.length; _i++) {
62
+ filters[_i] = arguments[_i];
63
+ }
64
+ var allOnlyOneOr = true;
65
+ try {
66
+ for (var filters_1 = tslib_1.__values(filters), filters_1_1 = filters_1.next(); !filters_1_1.done; filters_1_1 = filters_1.next()) {
67
+ var f = filters_1_1.value;
68
+ if (Object.keys(f).length > 1 || !f.$or) {
69
+ allOnlyOneOr = false;
70
+ break;
71
+ }
72
+ }
73
+ }
74
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
75
+ finally {
76
+ try {
77
+ if (filters_1_1 && !filters_1_1.done && (_a = filters_1.return)) _a.call(filters_1);
78
+ }
79
+ finally { if (e_1) throw e_1.error; }
80
+ }
81
+ if (allOnlyOneOr) {
82
+ // 优化特殊情况,全部都是$or,直接合并
83
+ var ors = filters.map(function (ele) { return ele.$or; });
84
+ return {
85
+ $or: ors.reduce(function (prev, next) { return prev.concat(next); }, [])
86
+ };
87
+ }
88
+ return {
89
+ $or: filters,
90
+ };
91
+ }
92
+ exports.unionFilterSegment = unionFilterSegment;
93
+ function combineFilters(filters, union) {
94
+ if (union) {
95
+ return unionFilterSegment.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(filters), false));
96
+ }
97
+ return addFilterSegment.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(filters), false));
98
+ }
99
+ exports.combineFilters = combineFilters;
100
+ /**
101
+ * 判断value1表达的单个属性查询与同属性上value2表达的查询是包容还是相斥
102
+ * 相容即value1所表达的查询结果一定被value2表达的查询结果所包含,例如:
103
+ * value1: {
104
+ * $eq: 12
105
+ * }
106
+ * value2: {
107
+ * $gt: 8,
108
+ * }
109
+ * 此时value1相容value2
110
+ *
111
+ * 相斥即value1所表达的查询结果与value2一定毫无联系,例如:
112
+ * value1: {
113
+ * $gt: 8,
114
+ * }
115
+ * value2: {
116
+ * $lt: 2,
117
+ * }
118
+ *
119
+ *
120
+ * @param value1
121
+ * @param value2
122
+ * @attention: 1)这里的测试不够充分,有些算子之间的相容或相斥可能有遗漏, 2)有新的算子加入需要修改代码
123
+ */
124
+ function judgeValueRelation(value1, value2, contained) {
125
+ if (typeof value1 === 'object') {
126
+ var attr = Object.keys(value1)[0];
127
+ if (['$gt', '$lt', '$gte', '$lte', '$eq', '$ne', '$startsWith', '$endsWith', '$includes'].includes(attr)) {
128
+ switch (attr) {
129
+ case '$gt': {
130
+ if (contained) {
131
+ // 包容
132
+ if (typeof value2 === 'object') {
133
+ var attr2 = Object.keys(value2)[0];
134
+ return ['$gt', '$gte'].includes(attr2) && value2[attr2] <= value1.$gt;
135
+ }
136
+ return false;
137
+ }
138
+ // 互斥
139
+ if (typeof value2 === 'object') {
140
+ var attr2 = Object.keys(value2)[0];
141
+ return ['$lt', '$lte', '$eq'].includes(attr2) && value2[attr2] <= value1.$gt
142
+ || attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gt || ele > value1.$gt; }));
143
+ }
144
+ return value2 <= value1.$gt;
145
+ }
146
+ case '$gte': {
147
+ if (contained) {
148
+ // 包容
149
+ if (typeof value2 === 'object') {
150
+ var attr2 = Object.keys(value2)[0];
151
+ return ['$gte'].includes(attr2) && value2[attr2] <= value1.$gte
152
+ || ['$gt'].includes(attr2) && value2[attr2] < value1.$gte;
153
+ }
154
+ return false;
155
+ }
156
+ // 互斥
157
+ if (typeof value2 === 'object') {
158
+ var attr2 = Object.keys(value2)[0];
159
+ return ['$lt'].includes(attr2) && value2[attr2] <= value1.$gte
160
+ || ['$eq', '$lte'].includes(attr2) && value2[attr2] < value1.gte
161
+ || attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gte || ele >= value1.$gte; }));
162
+ }
163
+ return value2 < value1.$gte;
164
+ }
165
+ case '$lt': {
166
+ if (contained) {
167
+ // 相容
168
+ if (typeof value2 === 'object') {
169
+ var attr2 = Object.keys(value2)[0];
170
+ return ['$lt', '$lte'].includes(attr2) && value2[attr2] >= value1.$lt;
171
+ }
172
+ return false;
173
+ }
174
+ // 互斥
175
+ if (typeof value2 === 'object') {
176
+ var attr2 = Object.keys(value2)[0];
177
+ return ['$gt', '$gte', '$eq'].includes(attr2) && value2[attr2] >= value1.$lt
178
+ || attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gt || ele < value1.$lt; }));
179
+ }
180
+ return value2 >= value1.$gt;
181
+ }
182
+ case '$lte': {
183
+ if (contained) {
184
+ // 包容
185
+ if (typeof value2 === 'object') {
186
+ var attr2 = Object.keys(value2)[0];
187
+ return ['$lte'].includes(attr2) && value2[attr2] >= value1.$lte
188
+ || ['$lt'].includes(attr2) && value2[attr2] > value1.$lte;
189
+ }
190
+ return false;
191
+ }
192
+ // 互斥
193
+ if (typeof value2 === 'object') {
194
+ var attr2 = Object.keys(value2)[0];
195
+ return ['$gt'].includes(attr2) && value2[attr2] >= value1.$lte
196
+ || ['$eq', '$gte'].includes(attr2) && value2[attr2] > value1.lte
197
+ || attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$lte || ele <= value1.$lte; }));
198
+ }
199
+ return value2 > value1.$gte;
200
+ }
201
+ case '$eq': {
202
+ if (contained) {
203
+ // 相容
204
+ if (typeof value2 === 'object') {
205
+ var attr2 = Object.keys(value2)[0];
206
+ return attr2 === '$eq' && value2[attr2] === value1.$eq || attr2 === '$ne' && value2[attr2] !== value1.$eq
207
+ || attr2 === '$gt' && value2[attr2] < value1.$eq || attr2 === '$lt' && value2[attr2] > value1.$eq
208
+ || attr2 === '$gte' && value2[attr2] <= value1.$eq || attr2 === '$lte' && value2[attr2] >= value1.$eq
209
+ || attr2 === '$startsWith' && value1.$eq.startsWith(value2[attr2])
210
+ || attr2 === '$endsWith' && value1.$eq.endsWith(value2[attr2])
211
+ || attr2 === '$includes' && value1.$eq.includes(value2[attr2])
212
+ || attr2 === '$in' && value2[attr2] instanceof Array && value2[attr2].includes(value1.$eq)
213
+ || attr2 === '$nin' && value2[attr2] instanceof Array && !value2[attr2].includes(value1.$eq)
214
+ || attr2 === '$between' && value2[attr2][0] <= value1.$eq && value2[attr2][1] >= value1.$eq
215
+ || attr2 === '$exists' && value2[attr2] === true;
216
+ }
217
+ return value2 === value1.$eq;
218
+ }
219
+ // 互斥
220
+ if (typeof value2 === 'object') {
221
+ var attr2 = Object.keys(value2)[0];
222
+ return attr2 === '$eq' && value2[attr2] !== value1.$eq || attr2 === '$gt' && value2[attr2] >= value1.$eq
223
+ || attr2 === '$lt' && value2[attr2] <= value1.$eq
224
+ || attr2 === '$gte' && value2[attr2] > value1.$eq || attr2 === '$lte' && value2[attr2] < value1.$eq
225
+ || attr2 === '$startsWith' && !value1.$eq.startsWith(value2[attr2])
226
+ || attr2 === '$endsWith' && !value1.$eq.endsWith(value2[attr2])
227
+ || attr2 === '$includes' && !value1.$eq.includes(value2[attr2])
228
+ || attr2 === '$in' && value2[attr2] instanceof Array && !value2[attr2].includes(value1.$eq)
229
+ || attr2 === '$between' && (value2[attr2][0] > value1.$eq || value2[attr2][1] < value1.$eq)
230
+ || attr2 === '$exists' && value2[attr2] === false;
231
+ }
232
+ return value2 !== value1.$eq;
233
+ }
234
+ case '$ne': {
235
+ if (contained) {
236
+ // 相容
237
+ if (typeof value2 === 'object') {
238
+ var attr2 = Object.keys(value2)[0];
239
+ return attr2 === '$ne' && value2[attr2] === value1.$ne;
240
+ }
241
+ return false;
242
+ }
243
+ // 互斥
244
+ if (typeof value2 === 'object') {
245
+ var attr2 = Object.keys(value2)[0];
246
+ return attr2 === '$eq' && value2[attr2] === value1.$ne;
247
+ }
248
+ return value2 === value1.$ne;
249
+ }
250
+ case '$startsWith': {
251
+ if (contained) {
252
+ // 相容
253
+ if (typeof value2 === 'object') {
254
+ var attr2 = Object.keys(value2)[0];
255
+ return attr2 === '$startsWith' && typeof (value2[attr2]) === 'string'
256
+ && value1.$startsWith.startsWith(value2[attr2]);
257
+ }
258
+ return typeof value2 === 'string' && value1.$startsWith.startsWith(value2);
259
+ }
260
+ // 互斥
261
+ if (typeof value2 === 'object') {
262
+ var attr2 = Object.keys(value2)[0];
263
+ return attr2 === '$startsWith' && typeof (value2[attr2]) === 'string'
264
+ && !value1.$startsWith.startsWith(value2[attr2]) && !value2[attr2].startsWith(value1.$startsWith)
265
+ || attr2 === '$eq' && !value2[attr2].startsWith(value1.$startsWith);
266
+ }
267
+ return !value2.startsWith(value1.$startsWith);
268
+ }
269
+ case '$endsWith': {
270
+ if (contained) {
271
+ // 相容
272
+ if (typeof value2 === 'object') {
273
+ var attr2 = Object.keys(value2)[0];
274
+ return attr2 === '$endsWith' && typeof (value2[attr2]) === 'string'
275
+ && value1.$startsWith.endsWith(value2[attr2]);
276
+ }
277
+ return typeof value2 === 'string' && value1.$startsWith.endsWith(value2);
278
+ }
279
+ // 互斥
280
+ if (typeof value2 === 'object') {
281
+ var attr2 = Object.keys(value2)[0];
282
+ return attr2 === '$startsWith' && typeof (value2[attr2]) === 'string'
283
+ && !value1.$startsWith.endsWith(value2[attr2]) && !value2[attr2].endsWith(value1.$startsWith)
284
+ || attr2 === '$eq' && !value2[attr2].endsWith(value1.$startsWith);
285
+ }
286
+ return !value2.endsWith(value1.$startsWith);
287
+ }
288
+ case '$includes': {
289
+ if (contained) {
290
+ // 相容
291
+ if (typeof value2 === 'object') {
292
+ var attr2 = Object.keys(value2)[0];
293
+ return ['$includes', '$startsWith', '$endsWith'].includes(attr2)
294
+ && typeof (value2[attr2]) === 'string'
295
+ && (value2[attr2]).includes(value1.$includes);
296
+ }
297
+ return typeof value2 === 'string' && value2.includes(value1.$includes);
298
+ }
299
+ // 互斥
300
+ if (typeof value2 === 'object') {
301
+ var attr2 = Object.keys(value2)[0];
302
+ return attr2 === '$eq' && !value2[attr2].includes(value1.$includes)
303
+ || attr2 === '$in' && value2[attr2] instanceof Array && !value2[attr2].find(function (ele) { return ele.includes(value1.$includes); });
304
+ }
305
+ return typeof value2 === 'string' && !value2.includes(value1.$includes);
306
+ }
307
+ default: {
308
+ (0, assert_1.default)(false, "\u4E0D\u80FD\u5904\u7406\u7684\u7B97\u5B50\u300C".concat(attr, "\u300D"));
309
+ }
310
+ }
311
+ }
312
+ else if (['$exists'].includes(attr)) {
313
+ (0, assert_1.default)(attr === '$exists');
314
+ if (contained) {
315
+ if (typeof value2 === 'object') {
316
+ var attr2 = Object.keys(value2)[0];
317
+ return attr2 === '$exists' && value2[attr2] === value1.$exists;
318
+ }
319
+ return false;
320
+ }
321
+ return typeof value2 === 'object' && value2.$exists === !(value1.$exists);
322
+ }
323
+ else if (['$in', '$nin', '$between'].includes(attr)) {
324
+ switch (attr) {
325
+ case '$in': {
326
+ if (contained) {
327
+ // 相容
328
+ if (value1.$in instanceof Array) {
329
+ if (typeof value2 === 'object') {
330
+ var attr2 = Object.keys(value2)[0];
331
+ if (attr2 === '$in') {
332
+ return value2[attr2] instanceof Array && (0, lodash_1.difference)(value1.$in, value2[attr2]).length === 0;
333
+ }
334
+ else if (attr2 === '$nin') {
335
+ return value2[attr2] instanceof Array && (0, lodash_1.intersection)(value1.$in, value2[attr2]).length === 0;
336
+ }
337
+ else if (attr2 === '$exists') {
338
+ return value2[attr2] === true;
339
+ }
340
+ else if (['$gt', '$gte', '$lt', '$lte', '$between'].includes(attr2)) {
341
+ var min33_1, max33_1;
342
+ value1.$in.forEach(function (ele) {
343
+ if (!min33_1 || min33_1 > ele) {
344
+ min33_1 = ele;
345
+ }
346
+ if (!max33_1 || max33_1 < ele) {
347
+ max33_1 = ele;
348
+ }
349
+ });
350
+ return attr2 === '$gt' && value2[attr2] < min33_1 || attr2 === '$gte' && value2[attr2] <= min33_1
351
+ || attr2 === '$lt' && value2[attr2] > max33_1 || attr2 === '$lte' && value2[attr2] >= max33_1
352
+ || attr2 === '$between' && value2[attr2][0] < min33_1 && value2[attr2][1] > max33_1;
353
+ }
354
+ }
355
+ }
356
+ return false;
357
+ }
358
+ // 相斥
359
+ if (value1.$in instanceof Array) {
360
+ if (typeof value2 === 'object') {
361
+ var attr2 = Object.keys(value2)[0];
362
+ if (attr2 === '$in') {
363
+ return (0, lodash_1.intersection)(value2[attr2], value1.$in).length === 0;
364
+ }
365
+ else if (attr2 === '$eq') {
366
+ return !value1.$in.includes(value2[attr2]);
367
+ }
368
+ else if (attr2 === '$exists') {
369
+ return value2[attr2] === false;
370
+ }
371
+ else if (['$gt', '$gte', '$lt', '$lte', '$between'].includes(attr2)) {
372
+ var min44_1, max44_1;
373
+ value1.$in.forEach(function (ele) {
374
+ if (!min44_1 || min44_1 > ele) {
375
+ min44_1 = ele;
376
+ }
377
+ if (!max44_1 || max44_1 < ele) {
378
+ max44_1 = ele;
379
+ }
380
+ });
381
+ return attr2 === '$gt' && value2[attr2] >= max44_1 || attr2 === '$gte' && value2[attr2] > max44_1
382
+ || attr2 === '$lt' && value2[attr2] <= min44_1 || attr2 === '$lte' && value2[attr2] < min44_1
383
+ || attr2 === '$between' && (value2[attr2][0] > max44_1 || value2[attr2][1] < min44_1);
384
+ }
385
+ }
386
+ return !value1.$in.includes(value2);
387
+ }
388
+ return false;
389
+ }
390
+ case '$nin': {
391
+ if (contained) {
392
+ // 相容
393
+ if (value1.$nin instanceof Array) {
394
+ if (typeof value2 === 'object') {
395
+ var attr2 = Object.keys(value2)[0];
396
+ if (attr2 === '$nin') {
397
+ return value2[attr2] instanceof Array && (0, lodash_1.intersection)(value2[attr2], value1.$nin).length === 0;
398
+ }
399
+ }
400
+ }
401
+ return false;
402
+ }
403
+ // 相斥
404
+ if (value1.$nin instanceof Array) {
405
+ if (typeof value2 === 'object') {
406
+ var attr2 = Object.keys(value2)[0];
407
+ if (attr2 === '$in') {
408
+ return value2[attr2] instanceof Array && (0, lodash_1.difference)(value2[attr2], value1.$nin).length === 0;
409
+ }
410
+ }
411
+ }
412
+ return false;
413
+ }
414
+ case '$between': {
415
+ (0, assert_1.default)(value1.$between instanceof Array);
416
+ if (contained) {
417
+ // 相容
418
+ if (typeof value2 === 'object') {
419
+ var attr2 = Object.keys(value2)[0];
420
+ if (['$gt', '$gte', '$lt', '$lte', '$between', '$eq'].includes(attr2)) {
421
+ return attr2 === '$between' && value2[attr2][0] <= value1.$between[0] && value2[attr2][1] >= value1.$between[1]
422
+ || attr2 === '$gt' && value2[attr2] < value1.$between[0] || attr2 === '$gte' && value2[attr2] <= value1.$between[0]
423
+ || attr2 === '$lt' && value2[attr2] > value1.$between[1] || attr2 === '$lte' && value2[attr2] >= value1.$between[1];
424
+ }
425
+ else if (attr2 === '$exists') {
426
+ return value2[attr2] === true;
427
+ }
428
+ }
429
+ return false;
430
+ }
431
+ // 相斥
432
+ if (typeof value2 === 'object') {
433
+ var attr2 = Object.keys(value2)[0];
434
+ if (['$gt', '$gte', '$lt', '$lte', '$between', '$eq'].includes(attr2)) {
435
+ return attr2 === '$between' && (value2[attr2][0] > value1.$between[1] || value2[attr2][1] < value1.$between[0])
436
+ || attr2 === '$gt' && value2[attr2] > value1.$between[1] || attr2 === '$gte' && value2[attr2] >= value1.$between[1]
437
+ || attr2 === '$lt' && value2[attr2] < value1.$between[0] || attr2 === '$lte' && value2[attr2] <= value1.$between[0]
438
+ || attr2 === '$eq' && (value2[attr2] > value1.$between[1] || value2[attr2] < value1.$between[0]);
439
+ }
440
+ else if (attr2 === '$exists') {
441
+ return value2[attr2] === false;
442
+ }
443
+ else if (attr2 === '$in' && value2[attr2] instanceof Array) {
444
+ return !value2[attr2].find(function (ele) { return ele >= value1.$between[0] && ele <= value1.$between[1]; });
445
+ }
446
+ return false;
447
+ }
448
+ }
449
+ default: {
450
+ (0, assert_1.default)(false, "\u6682\u4E0D\u652F\u6301\u7684\u7B97\u5B50".concat(attr));
451
+ }
452
+ }
453
+ }
454
+ else {
455
+ return false;
456
+ }
457
+ }
458
+ else {
459
+ // value1是一个等值查询
460
+ if (contained) {
461
+ // 相容
462
+ if (typeof value2 === 'object') {
463
+ var attr2 = Object.keys(value2)[0];
464
+ return attr2 === '$eq' && value2[attr2] === value1 || attr2 === '$ne' && value2[attr2] !== value1
465
+ || attr2 === '$gt' && value2[attr2] < value1 || attr2 === '$lt' && value2[attr2] > value1
466
+ || attr2 === '$gte' && value2[attr2] <= value1 || attr2 === '$lte' && value2[attr2] >= value1
467
+ || attr2 === '$startsWith' && value1.startsWith(value2[attr2])
468
+ || attr2 === '$endsWith' && value1.endsWith(value2[attr2])
469
+ || attr2 === '$includes' && value1.includes(value2[attr2])
470
+ || attr2 === '$in' && value2[attr2] instanceof Array && value2[attr2].includes(value1)
471
+ || attr2 === '$nin' && value2[attr2] instanceof Array && !value2[attr2].includes(value1)
472
+ || attr2 === '$between' && value2[attr2][0] <= value1 && value2[attr2][1] >= value1
473
+ || attr2 === '$exists' && value2[attr2] === true;
474
+ }
475
+ return value2 === value1;
476
+ }
477
+ // 互斥
478
+ if (typeof value2 === 'object') {
479
+ var attr2 = Object.keys(value2)[0];
480
+ return attr2 === '$eq' && value2[attr2] !== value1 || attr2 === '$gt' && value2[attr2] >= value1
481
+ || attr2 === '$lt' && value2[attr2] <= value1
482
+ || attr2 === '$gte' && value2[attr2] > value1 || attr2 === '$lte' && value2[attr2] < value1
483
+ || attr2 === '$startsWith' && !value1.startsWith(value2[attr2])
484
+ || attr2 === '$endsWith' && !value1.endsWith(value2[attr2])
485
+ || attr2 === '$includes' && !value1.includes(value2[attr2])
486
+ || attr2 === '$in' && value2[attr2] instanceof Array && !value2[attr2].includes(value1)
487
+ || attr2 === '$between' && (value2[attr2][0] > value1 || value2[attr2][1] < value1)
488
+ || attr2 === '$exists' && value2[attr2] === false;
489
+ }
490
+ return value2 !== value1;
491
+ }
492
+ }
493
+ exports.judgeValueRelation = judgeValueRelation;
494
+ function judgeFilter2ValueRelation(entity, schema, attr, filter, conditionalFilterAttrValue, contained) {
495
+ var _a;
496
+ for (var attr2 in filter) {
497
+ if (['$and', '$or', '$not'].includes(attr2)) {
498
+ switch (attr2) {
499
+ case '$and':
500
+ case '$or':
501
+ case '$xor': {
502
+ var logicQueries = filter[attr2];
503
+ var results = logicQueries.map(function (logicQuery) { return judgeFilter2ValueRelation(entity, schema, attr, logicQuery, conditionalFilterAttrValue, contained); });
504
+ // 如果filter的多个算子是and关系,则只要有一个包含此条件就是包含,只要有一个与此条件相斥就是相斥
505
+ // 如果filter的多个算子是or关系,则必须所有的条件都包含此条件才是包含,必须所有的条件都与此条件相斥才是相斥
506
+ if (attr2 === '$and') {
507
+ if (results.includes(true)) {
508
+ return true;
509
+ }
510
+ }
511
+ else if (attr2 === '$or') {
512
+ if (!results.includes(false)) {
513
+ return true;
514
+ }
515
+ }
516
+ else {
517
+ (0, assert_1.default)(false);
518
+ }
519
+ break;
520
+ }
521
+ case '$not': {
522
+ /*
523
+ * 若filter的not条件被conditionalFilterAttrValue条件包容,则说明两者互斥
524
+ * filter包容conditionalFilterAttrValue条件暂时无法由其not条件推论出来
525
+ */
526
+ var logicQuery = filter[attr2];
527
+ if (!contained && judgeFilterRelation(entity, schema, logicQuery, (_a = {}, _a[attr] = conditionalFilterAttrValue, _a), true)) {
528
+ return true;
529
+ }
530
+ break;
531
+ }
532
+ default: {
533
+ (0, assert_1.default)(false);
534
+ }
535
+ }
536
+ }
537
+ else if (attr2.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
538
+ return false;
539
+ }
540
+ else if (attr2.toLowerCase() === '$text') {
541
+ return false;
542
+ }
543
+ else {
544
+ if (attr === attr2) {
545
+ var rel = (0, relation_1.judgeRelation)(schema, entity, attr2);
546
+ if (rel === 1) {
547
+ return judgeValueRelation(filter[attr2], conditionalFilterAttrValue, contained);
548
+ }
549
+ else if (rel === 2) {
550
+ return judgeFilterRelation(attr2, schema, filter[attr2], conditionalFilterAttrValue, contained);
551
+ }
552
+ else if (typeof rel === 'string') {
553
+ return judgeFilterRelation(rel, schema, filter[attr2], conditionalFilterAttrValue, contained);
554
+ }
555
+ else {
556
+ (0, assert_1.default)(false);
557
+ }
558
+ }
559
+ }
560
+ }
561
+ // 到这里说明无法判断相容或者相斥,安全起见全返回false
562
+ return false;
563
+ }
564
+ /**
565
+ * @param entity
566
+ * @param schema
567
+ * @param filter
568
+ * @param conditionalFilter
569
+ * @param contained: true代表filter包容conditionalFilter, false代表filter与conditionalFilter相斥
570
+ */
571
+ function judgeFilterRelation(entity, schema, filter, conditionalFilter, contained) {
572
+ for (var attr in conditionalFilter) {
573
+ if (['$and', '$or', '$not'].includes(attr)) {
574
+ switch (attr) {
575
+ case '$and':
576
+ case '$or': {
577
+ var logicQueries = conditionalFilter[attr];
578
+ var results = logicQueries.map(function (logicQuery) { return judgeFilterRelation(entity, schema, filter, logicQuery, contained); });
579
+ if (contained) {
580
+ // 如果是包容关系,or和and需要全部被包容
581
+ if (results.includes(false)) {
582
+ return false;
583
+ }
584
+ }
585
+ else if (!contained) {
586
+ // 如果是相斥关系,and只需要和一个相斥,or需要和全部相斥
587
+ if (attr === '$and' && results.includes(true) || attr === '$or' && !results.includes(false)) {
588
+ return true;
589
+ }
590
+ }
591
+ else {
592
+ (0, assert_1.default)(false);
593
+ }
594
+ break;
595
+ }
596
+ case '$not': {
597
+ /**
598
+ * 若filter与conditionalFilter not所定义的部分相斥,则filter与conditionalFilter相容
599
+ * 若filter将conditionalFilter not所定义的部分包容,则filter与conditionalFilter相斥
600
+ */
601
+ var logicQuery = conditionalFilter[attr];
602
+ if (contained) {
603
+ if (!judgeFilterRelation(entity, schema, filter, logicQuery, false)) {
604
+ return false;
605
+ }
606
+ }
607
+ else {
608
+ if (judgeFilterRelation(entity, schema, filter, logicQuery, true)) {
609
+ return true;
610
+ }
611
+ }
612
+ break;
613
+ }
614
+ default: {
615
+ throw new Error("\u6682\u4E0D\u652F\u6301\u7684\u903B\u8F91\u7B97\u5B50".concat(attr));
616
+ }
617
+ }
618
+ }
619
+ else if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
620
+ return false;
621
+ }
622
+ else if (attr.toLowerCase() === '$text') {
623
+ return false;
624
+ }
625
+ else {
626
+ if (contained && !judgeFilter2ValueRelation(entity, schema, attr, filter, conditionalFilter[attr], contained)) {
627
+ // 相容关系只要有一个不相容就不相容
628
+ return false;
629
+ }
630
+ if (!contained && judgeFilter2ValueRelation(entity, schema, attr, filter, conditionalFilter[attr], contained)) {
631
+ // 相斥关系只要有一个相斥就相斥
632
+ return true;
633
+ }
634
+ }
635
+ }
636
+ // 到这里说明不能否定其相容(所以要返回相容),也不能肯定其相斥(所以要返回不相斥)
637
+ return contained;
638
+ }
639
+ /**
640
+ *
641
+ * 判断filter是否包含conditionalFilter中的查询条件,即filter查询的结果一定满足conditionalFilter的约束
642
+ * filter = {
643
+ * a: 1
644
+ * b: 2,
645
+ * c: 3,
646
+ * },
647
+ * conditionalFilter = {
648
+ * a: 1
649
+ * }
650
+ * 则包含
651
+ * @param entity
652
+ * @param schema
653
+ * @param filter
654
+ * @param conditionalFilter
655
+ * @returns
656
+ */
657
+ function contains(entity, schema, filter, conditionalFilter) {
658
+ return judgeFilterRelation(entity, schema, filter, conditionalFilter, true);
659
+ // return false;
660
+ }
661
+ exports.contains = contains;
662
+ /**
663
+ * 判断filter1和filter2是否相斥,即filter1和filter2查询的结果一定没有交集
664
+ * filter1 = {
665
+ * a: 2
666
+ * },
667
+ * filter2 = {
668
+ * a: 1
669
+ * }
670
+ * 则包含
671
+ * @param entity
672
+ * @param schema
673
+ * @param filter
674
+ * @param conditionalFilter
675
+ */
676
+ function repel(entity, schema, filter1, filter2) {
677
+ // todo
678
+ return judgeFilterRelation(entity, schema, filter1, filter2, false);
679
+ // return false;
680
+ }
681
+ exports.repel = repel;
682
+ /**
683
+ * 从filter中判断是否有确定的id对象,如果有则返回这些id,没有返回空数组
684
+ * @param filter
685
+ * @returns
686
+ */
687
+ function getRelevantIds(filter) {
688
+ var _a, _b;
689
+ var ids;
690
+ var idsAnd;
691
+ var idsOr;
692
+ if (!filter) {
693
+ return [];
694
+ }
695
+ // 因为要准确判定id,如果有其它的过滤条件,可能会使实际处理的行数少于id指向的行数,只能返回空数组
696
+ var attrs = Object.keys(filter);
697
+ if ((0, lodash_1.intersection)(attrs, ['id', '$and', '$or']).length > 3) {
698
+ return [];
699
+ }
700
+ if (filter === null || filter === void 0 ? void 0 : filter.$and) {
701
+ var idss = filter.$and.map(function (ele) { return getRelevantIds(ele); });
702
+ idsAnd = lodash_1.intersection.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(idss), false));
703
+ }
704
+ if (filter === null || filter === void 0 ? void 0 : filter.$or) {
705
+ var idss = filter.$or.map(function (ele) { return getRelevantIds(ele); });
706
+ idsOr = lodash_1.union.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(idss), false));
707
+ }
708
+ if (filter === null || filter === void 0 ? void 0 : filter.id) {
709
+ if (typeof filter.id === 'string') {
710
+ ids = [filter.id];
711
+ }
712
+ if ((_a = filter.id) === null || _a === void 0 ? void 0 : _a.$eq) {
713
+ ids = [filter.id.$eq];
714
+ }
715
+ if (((_b = filter.id) === null || _b === void 0 ? void 0 : _b.$in) && filter.id.$in instanceof Array) {
716
+ ids = filter.id.$in;
717
+ }
718
+ }
719
+ // 三者如果有基一,直接返回,如果大于一返回intersection
720
+ if (!ids && !idsAnd && !idsOr) {
721
+ return [];
722
+ }
723
+ var result = (ids || idsAnd || idsOr);
724
+ if (ids) {
725
+ result = (0, lodash_1.intersection)(result, ids);
726
+ }
727
+ if (idsAnd) {
728
+ result = (0, lodash_1.intersection)(result, idsAnd);
729
+ }
730
+ if (idsOr) {
731
+ result = (0, lodash_1.intersection)(result, idsOr);
732
+ }
733
+ return result;
734
+ }
735
+ exports.getRelevantIds = getRelevantIds;
736
+ /**
737
+ * 判断两个过滤条件是否完全一致
738
+ * @param entity
739
+ * @param schema
740
+ * @param filter1
741
+ * @param filter2
742
+ */
743
+ function same(entity, schema, filter1, filter2) {
744
+ // 当前只需要判断是不是id相等就行了,在runningTree的operation合并的时间使用
745
+ if (!filter1 || !filter1.id || Object.keys(filter1).length > 1 || !filter2 || !filter2.id || Object.keys(filter2).length > 1) {
746
+ return false;
747
+ }
748
+ return filter1.id === filter2.id;
749
+ }
750
+ exports.same = same;
751
+ /**
752
+ * 寻找在树形结构中满足条件的数据行的上层数据
753
+ * 例如在area表中,如果“杭州市”满足这一条件,则希望查到更高层的“浙江省”和“中国”,即可构造出满足条件的filter
754
+ * @param entity
755
+ * @param parentKey parentId属性名
756
+ * @param filter 查询当前行的条件
757
+ * @param level
758
+ */
759
+ function makeTreeAncestorFilter(entity, parentKey, filter, level, includeAll, includeSelf) {
760
+ var _a;
761
+ if (level === void 0) { level = 1; }
762
+ (0, assert_1.default)(level >= 0);
763
+ var idInFilters = [];
764
+ if (includeSelf) {
765
+ idInFilters.push(filter);
766
+ }
767
+ var currentLevelInFilter = filter;
768
+ while (level > 0) {
769
+ currentLevelInFilter = {
770
+ id: {
771
+ $in: {
772
+ entity: entity,
773
+ data: (_a = {},
774
+ _a[parentKey] = 1,
775
+ _a),
776
+ filter: currentLevelInFilter,
777
+ }
778
+ },
779
+ };
780
+ if (includeAll) {
781
+ idInFilters.push(currentLevelInFilter);
782
+ }
783
+ level--;
784
+ }
785
+ ;
786
+ if (includeAll) {
787
+ return {
788
+ $or: idInFilters,
789
+ };
790
+ }
791
+ return currentLevelInFilter;
792
+ }
793
+ exports.makeTreeAncestorFilter = makeTreeAncestorFilter;
794
+ /**
795
+ * 寻找在树形结构中满足条件的数据行的下层数据
796
+ * 例如在area表中,如果“杭州市”满足这一条件,则希望查到更低层的“西湖区”,即可构造出满足条件的filter
797
+ * @param entity
798
+ * @param parentKey parentId属性名
799
+ * @param filter 查询当前行的条件
800
+ * @param level
801
+ */
802
+ function makeTreeDescendantFilter(entity, parentKey, filter, level, includeAll, includeSelf) {
803
+ var _a;
804
+ if (level === void 0) { level = 1; }
805
+ (0, assert_1.default)(level >= 0);
806
+ (0, assert_1.default)(parentKey.endsWith('Id'));
807
+ var parentKeyRef = parentKey.slice(0, parentKey.length - 2);
808
+ var idInFilters = [];
809
+ if (includeSelf) {
810
+ idInFilters.push(filter);
811
+ }
812
+ var currentLevelInFilter = filter;
813
+ while (level > 0) {
814
+ currentLevelInFilter = (_a = {},
815
+ _a[parentKeyRef] = currentLevelInFilter,
816
+ _a);
817
+ if (includeAll) {
818
+ idInFilters.push(currentLevelInFilter);
819
+ }
820
+ level--;
821
+ }
822
+ ;
823
+ if (includeAll) {
824
+ return {
825
+ $or: idInFilters,
826
+ };
827
+ }
828
+ return currentLevelInFilter;
829
+ }
830
+ exports.makeTreeDescendantFilter = makeTreeDescendantFilter;
831
+ /**
832
+ * 检查filter是否包含contained(filter查询的数据一定满足contained)
833
+ * @param entity
834
+ * @param context
835
+ * @param contained
836
+ * @param filter
837
+ * @param dataCompare
838
+ * @returns
839
+ */
840
+ function checkFilterContains(entity, context, contained, filter, dataCompare) {
841
+ if (!filter) {
842
+ throw new types_1.OakRowInconsistencyException();
843
+ }
844
+ var schema = context.getSchema();
845
+ // 优先判断两个条件是否相容
846
+ if (contains(entity, schema, filter, contained)) {
847
+ return true;
848
+ }
849
+ if (dataCompare) {
850
+ // 再判断加上了conditionalFilter后取得的行数是否缩减
851
+ var filter2 = combineFilters([filter, {
852
+ $not: contained,
853
+ }]);
854
+ var count = context.count(entity, {
855
+ filter: filter2,
856
+ }, {
857
+ dontCollect: true,
858
+ blockTrigger: true,
859
+ });
860
+ if (count instanceof Promise) {
861
+ return count.then(function (count2) { return count2 === 0; });
862
+ }
863
+ return count === 0;
864
+ }
865
+ return false;
866
+ }
867
+ exports.checkFilterContains = checkFilterContains;
868
+ function checkFilterRepel(entity, context, filter1, filter2, dataCompare) {
869
+ if (!filter2) {
870
+ throw new types_1.OakRowInconsistencyException();
871
+ }
872
+ var schema = context.getSchema();
873
+ // 优先判断两个条件是否相容
874
+ if (repel(entity, schema, filter2, filter1)) {
875
+ return true;
876
+ }
877
+ // 再判断两者同时成立时取得的行数是否为0
878
+ if (dataCompare) {
879
+ var filter3 = combineFilters([filter2, filter1]);
880
+ var count = context.count(entity, {
881
+ filter: filter3,
882
+ }, {
883
+ dontCollect: true,
884
+ blockTrigger: true,
885
+ });
886
+ if (count instanceof Promise) {
887
+ return count.then(function (count2) { return count2 === 0; });
888
+ }
889
+ return count === 0;
890
+ }
891
+ return false;
892
+ }
893
+ exports.checkFilterRepel = checkFilterRepel;