@byted-apaas/server-sdk-node 0.0.1-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/LICENSE +7 -0
  2. package/NOTICE +3 -0
  3. package/README.md +1 -0
  4. package/application/application.d.ts +27 -0
  5. package/application/application.js +2 -0
  6. package/application/application.ts +40 -0
  7. package/application/function/function.d.ts +14 -0
  8. package/application/function/function.js +21 -0
  9. package/application/function/function.ts +37 -0
  10. package/application/impl/common.d.ts +2 -0
  11. package/application/impl/common.js +50 -0
  12. package/application/impl/common.ts +50 -0
  13. package/application/impl/impl.d.ts +16 -0
  14. package/application/impl/impl.js +38 -0
  15. package/application/impl/impl.ts +48 -0
  16. package/constants/constants.d.ts +10 -0
  17. package/constants/constants.js +15 -0
  18. package/constants/constants.ts +15 -0
  19. package/context/context.d.ts +74 -0
  20. package/context/context.js +25 -0
  21. package/context/context.ts +130 -0
  22. package/context/db/db.d.ts +73 -0
  23. package/context/db/db.js +4 -0
  24. package/context/db/db.ts +80 -0
  25. package/context/db/impl/IObject.d.ts +251 -0
  26. package/context/db/impl/IObject.js +5 -0
  27. package/context/db/impl/IObject.ts +247 -0
  28. package/context/db/impl/db.d.ts +61 -0
  29. package/context/db/impl/db.js +86 -0
  30. package/context/db/impl/db.ts +95 -0
  31. package/context/db/impl/object.d.ts +92 -0
  32. package/context/db/impl/object.js +524 -0
  33. package/context/db/impl/object.ts +631 -0
  34. package/context/db/impl/oql/ioql.d.ts +6 -0
  35. package/context/db/impl/oql/ioql.js +4 -0
  36. package/context/db/impl/oql/ioql.ts +9 -0
  37. package/context/db/impl/oql/oql.d.ts +8 -0
  38. package/context/db/impl/oql/oql.js +21 -0
  39. package/context/db/impl/oql/oql.ts +25 -0
  40. package/context/db/impl/order.d.ts +9 -0
  41. package/context/db/impl/order.js +15 -0
  42. package/context/db/impl/order.ts +19 -0
  43. package/context/db/impl/propertiesStore.d.ts +8 -0
  44. package/context/db/impl/propertiesStore.js +29 -0
  45. package/context/db/impl/propertiesStore.ts +31 -0
  46. package/context/db/impl/queryBuilder.d.ts +29 -0
  47. package/context/db/impl/queryBuilder.js +78 -0
  48. package/context/db/impl/queryBuilder.ts +106 -0
  49. package/context/db/impl/transaction/index.d.ts +20 -0
  50. package/context/db/impl/transaction/index.js +277 -0
  51. package/context/db/impl/transaction/index.ts +340 -0
  52. package/context/db/impl/transaction/operation.d.ts +27 -0
  53. package/context/db/impl/transaction/operation.js +69 -0
  54. package/context/db/impl/transaction/operation.ts +72 -0
  55. package/context/db/impl/transaction.d.ts +67 -0
  56. package/context/db/impl/transaction.js +4 -0
  57. package/context/db/impl/transaction.ts +79 -0
  58. package/context/globalConfig/globalConfig.d.ts +7 -0
  59. package/context/globalConfig/globalConfig.js +16 -0
  60. package/context/globalConfig/globalConfig.ts +14 -0
  61. package/context/metadata/components/common.d.ts +6 -0
  62. package/context/metadata/components/common.js +17 -0
  63. package/context/metadata/components/common.ts +22 -0
  64. package/context/metadata/components/components.d.ts +12 -0
  65. package/context/metadata/components/components.js +19 -0
  66. package/context/metadata/components/components.ts +22 -0
  67. package/context/metadata/components/desktop/list.d.ts +16 -0
  68. package/context/metadata/components/desktop/list.js +402 -0
  69. package/context/metadata/components/desktop/list.ts +457 -0
  70. package/context/metadata/components/desktop/recordDetail.d.ts +11 -0
  71. package/context/metadata/components/desktop/recordDetail.js +53 -0
  72. package/context/metadata/components/desktop/recordDetail.ts +67 -0
  73. package/context/metadata/components/mobile/list.d.ts +24 -0
  74. package/context/metadata/components/mobile/list.js +145 -0
  75. package/context/metadata/components/mobile/list.ts +179 -0
  76. package/context/metadata/metadata.d.ts +61 -0
  77. package/context/metadata/metadata.js +103 -0
  78. package/context/metadata/metadata.ts +178 -0
  79. package/context/metadata/objects/fields.d.ts +130 -0
  80. package/context/metadata/objects/fields.js +334 -0
  81. package/context/metadata/objects/fields.ts +457 -0
  82. package/context/metadata/objects/fields.util.d.ts +40 -0
  83. package/context/metadata/objects/fields.util.js +319 -0
  84. package/context/metadata/objects/fields.util.ts +360 -0
  85. package/context/metadata/objects/objects.d.ts +11 -0
  86. package/context/metadata/objects/objects.js +20 -0
  87. package/context/metadata/objects/objects.ts +30 -0
  88. package/context/metadata/types/common.d.ts +6 -0
  89. package/context/metadata/types/common.js +9 -0
  90. package/context/metadata/types/common.ts +13 -0
  91. package/context/metadata/types/components.d.ts +90 -0
  92. package/context/metadata/types/components.js +4 -0
  93. package/context/metadata/types/components.ts +108 -0
  94. package/context/metadata/types/objects.d.ts +226 -0
  95. package/context/metadata/types/objects.js +4 -0
  96. package/context/metadata/types/objects.ts +254 -0
  97. package/context/msg/msg.d.ts +38 -0
  98. package/context/msg/msg.js +35 -0
  99. package/context/msg/msg.ts +52 -0
  100. package/context/resources/IResources.d.ts +66 -0
  101. package/context/resources/IResources.js +2 -0
  102. package/context/resources/IResources.ts +73 -0
  103. package/context/resources/impl/resources.d.ts +42 -0
  104. package/context/resources/impl/resources.js +150 -0
  105. package/context/resources/impl/resources.ts +173 -0
  106. package/context/tasks/tasks.d.ts +33 -0
  107. package/context/tasks/tasks.js +139 -0
  108. package/context/tasks/tasks.ts +159 -0
  109. package/data/index.d.ts +27 -0
  110. package/data/index.js +4 -0
  111. package/data/index.ts +30 -0
  112. package/hooks/api.d.ts +61 -0
  113. package/hooks/api.js +327 -0
  114. package/hooks/api.ts +341 -0
  115. package/hooks/hooks.d.ts +1 -0
  116. package/hooks/hooks.js +88 -0
  117. package/hooks/hooks.ts +100 -0
  118. package/kunlun/kunlun.d.ts +55 -0
  119. package/kunlun/kunlun.js +58 -0
  120. package/kunlun/kunlun.ts +72 -0
  121. package/kunlun/operator/IOperator.d.ts +158 -0
  122. package/kunlun/operator/IOperator.js +4 -0
  123. package/kunlun/operator/IOperator.ts +166 -0
  124. package/kunlun/operator/impl/expression.d.ts +47 -0
  125. package/kunlun/operator/impl/expression.js +56 -0
  126. package/kunlun/operator/impl/expression.ts +74 -0
  127. package/kunlun/operator/impl/logic.d.ts +50 -0
  128. package/kunlun/operator/impl/logic.js +509 -0
  129. package/kunlun/operator/impl/logic.ts +598 -0
  130. package/kunlun/operator/impl/logicV2.d.ts +58 -0
  131. package/kunlun/operator/impl/logicV2.js +200 -0
  132. package/kunlun/operator/impl/logicV2.ts +254 -0
  133. package/kunlun/operator/impl/operator.d.ts +179 -0
  134. package/kunlun/operator/impl/operator.js +222 -0
  135. package/kunlun/operator/impl/operator.ts +258 -0
  136. package/kunlun/operator/impl/operatorV2.d.ts +180 -0
  137. package/kunlun/operator/impl/operatorV2.js +216 -0
  138. package/kunlun/operator/impl/operatorV2.ts +249 -0
  139. package/lib/core.d.ts +4 -0
  140. package/lib/core.js +19 -0
  141. package/lib/core.ts +7 -0
  142. package/package.json +50 -0
  143. package/request/common.d.ts +13 -0
  144. package/request/common.js +87 -0
  145. package/request/common.ts +91 -0
  146. package/request/constants.d.ts +10 -0
  147. package/request/constants.js +25 -0
  148. package/request/constants.ts +25 -0
  149. package/request/faasinfra.d.ts +8 -0
  150. package/request/faasinfra.js +358 -0
  151. package/request/faasinfra.ts +393 -0
  152. package/request/innerapi.d.ts +67 -0
  153. package/request/innerapi.js +1412 -0
  154. package/request/innerapi.ts +1648 -0
  155. package/request/interface.d.ts +95 -0
  156. package/request/interface.js +41 -0
  157. package/request/interface.ts +169 -0
  158. package/request/openapi.d.ts +69 -0
  159. package/request/openapi.js +1014 -0
  160. package/request/openapi.ts +1223 -0
  161. package/types/types.d.ts +79 -0
  162. package/types/types.js +4 -0
  163. package/types/types.ts +108 -0
@@ -0,0 +1,598 @@
1
+ // Copyright 2022 ByteDance Ltd. and/or its affiliates
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import {Expression, ConstantExpressionField, MetadataExpressionField, UserDataType, PathType} from './expression'
5
+ import {operates} from './operator'
6
+ import {utils, exceptions} from '@byted-apaas/server-common-node'
7
+ import * as Request from '../../../request/interface'
8
+ import * as common from "@byted-apaas/server-common-node";
9
+ import fieldType = common.fieldType
10
+
11
+ const dayjs = require("dayjs");
12
+
13
+ // 条件操作
14
+ export class Condition {
15
+ left: string
16
+ right: Condition | UserDataType
17
+ op: string
18
+
19
+ /**
20
+ *
21
+ * @param objectApiName 对象的ApiName
22
+ * @param fieldApiName 字段的ApiName,指左值,即key
23
+ * @param index 条件的排序
24
+ * @returns {Expression}
25
+ */
26
+ toExpression(objectApiName: string, fieldApiName: string, index: number): Expression {
27
+ this.left = fieldApiName;
28
+ let left: MetadataExpressionField;
29
+ let right: ConstantExpressionField;
30
+ let op: string;
31
+
32
+ if (this.right instanceof Condition) {
33
+ left = new MetadataExpressionField(objectApiName, this.right.left);
34
+ right = new ConstantExpressionField(this.right.right)
35
+ left.settings.fieldPath.unshift(new MetadataExpressionField(objectApiName, fieldApiName).settings.fieldPath[0]);
36
+ op = this.right.op;
37
+ } else {
38
+ left = new MetadataExpressionField(objectApiName, fieldApiName)
39
+ right = new ConstantExpressionField(this.right);
40
+ op = this.op;
41
+ }
42
+ return new Expression(left, op, right, index);
43
+ }
44
+
45
+ constructor(left: string, right: Condition | UserDataType, op: string = operates.EQ) {
46
+ this.left = left;
47
+ this.right = right;
48
+ if (op) {
49
+ this.op = op;
50
+ }
51
+ }
52
+ }
53
+
54
+ // param type should be spercific
55
+ function isKunlunTypeOrSerializedObject(obj: { [index: string]: any }) {
56
+ // instanceof 改动8
57
+ // return !!(obj instanceof fieldType.KunlunFieldType || (obj && obj.toJSON && obj.toJSON instanceof Function));
58
+ return !!(fieldType.isKFieldType(obj) || (obj && obj.toJSON && obj.toJSON instanceof Function));
59
+ }
60
+
61
+ export type Criterion = {
62
+ conditions: Expression[]
63
+ logic: string
64
+ }
65
+
66
+ /**
67
+ * 构造 criterion
68
+ *
69
+ * @param logic
70
+ * @param objectApiName
71
+ * @param maxId 添加 _id>maxId 条件,为 -1 表示不使用该参数
72
+ */
73
+ export function buildCriterion(logic: Logic, objectApiName: string, maxId: number = -1): Criterion | string {
74
+ let index = 0;
75
+ let logics: string[] = [];
76
+ let expressions: Expression[] = [];
77
+
78
+ // check type of logic
79
+ function f(logic: Logic | { [index: string]: any }, value: string = "and") {
80
+ // 1.如果是逻辑表达式
81
+ if (logic instanceof Logic) {
82
+ logics.push("(");
83
+ for (let i = 0; i < logic.logics.length; i++) {
84
+ let p = logic.logics[i];
85
+ f(p, logic.value);
86
+ if (i < logic.logics.length - 1) {
87
+ logics.push(` ${logic.value} `);
88
+ }
89
+ }
90
+
91
+ if (logic.expressions) {
92
+ f(logic.expressions, logic.value);
93
+ }
94
+
95
+ logics.push(")");
96
+ return;
97
+ }
98
+
99
+ // 2.否则是算法表达式
100
+ let exps = logic;
101
+ let logs = [];
102
+ for (let left of Object.keys(exps)) {
103
+ let right: { [index: string]: any } = logic[left];
104
+ let copy: { [index: string]: any } = right;
105
+ if (utils.isObject(right) && !(right instanceof Condition) && !isKunlunTypeOrSerializedObject(right)) {
106
+ for (let leftInner of Object.keys(right)) {
107
+ index++;
108
+ logs.push(index);
109
+ copy = {};
110
+ copy[leftInner] = right[leftInner];
111
+ expressions.push(buildExpression(objectApiName, left, copy, index));
112
+ }
113
+ } else {
114
+ index++;
115
+ logs.push(index);
116
+ expressions.push(buildExpression(objectApiName, left, right, index));
117
+ }
118
+ }
119
+
120
+ logics.push(logs.join(` ${value} `));
121
+ }
122
+
123
+ f(logic);
124
+
125
+ // 如果未设置filter,默认添加: _isDeleted = false
126
+ if (expressions.length < 1) {
127
+ if(maxId >= 0) {
128
+ return `{"conditions":[{"index":1,"left":{"type":"metadataVariable","settings":{"fieldPath":[{"objectApiName":"${objectApiName}","fieldApiName":"_isDeleted"}]}},"operator":"equals","right":{"type":"constant","settings":{"data":false}}},{"index":2,"left":{"type":"metadataVariable","settings":{"fieldPath":[{"objectApiName":"${objectApiName}","fieldApiName":"_id"}]}},"operator":"greaterThan","right":{"type":"constant","settings":{"data":${maxId}}}}],"logic":"1 and 2"}`;
129
+ } else {
130
+ return `{"conditions":[{"index":1,"left":{"type":"metadataVariable","settings":{"fieldPath":[{"objectApiName":"${objectApiName}","fieldApiName":"_isDeleted"}]}},"operator":"equals","right":{"type":"constant","settings":{"data":false}}}],"logic":"1"}`;
131
+ }
132
+ }
133
+
134
+ let isContainDeleted = false;
135
+ for (let i = 0; i < expressions.length; i++) {
136
+ let expression = expressions[i];
137
+ try {
138
+ if (expression.left.type === "metadataVariable" && expression.left.settings.fieldPath[0].fieldApiName === "_isDeleted") {
139
+ isContainDeleted = true;
140
+ break;
141
+ }
142
+ } catch (e) {
143
+ console.error(e);
144
+ }
145
+ }
146
+
147
+ if (!isContainDeleted) {
148
+ expressions.push(buildExpression(objectApiName, "_isDeleted", false, expressions.length + 1));
149
+ logics.splice(0, 0, "(");
150
+ logics.push(" and ", `${expressions.length}`, ")");
151
+ }
152
+
153
+ if (maxId >= 0) {
154
+ expressions.push(buildExpression(objectApiName, "_id", new Condition("_id", maxId, operates.GT), expressions.length + 1));
155
+ logics.splice(0, 0, "(");
156
+ logics.push(" and ", `${expressions.length}`, ")");
157
+ }
158
+
159
+ return {conditions: expressions, logic: logics.join("")};
160
+ }
161
+
162
+ export function buildExpression(objectApiName: string, left: string, right: Condition | UserDataType, index = 1): Expression {
163
+ if (!(right instanceof Condition)) {
164
+ right = new Condition(left, right);
165
+ }
166
+
167
+ if (right instanceof Condition) {
168
+ if (utils.isObject(right.right) && !(right.right instanceof Condition) && !isKunlunTypeOrSerializedObject(right.right as object)) {
169
+ let key: string = Object.keys(right.right)[0]
170
+ let rr: {
171
+ [index: string]: any
172
+ } = right.right as object
173
+ if (key === 'id') {
174
+ if (rr[key] instanceof Condition) {
175
+ right.op = rr[key].op;
176
+ right.right = {id: rr[key].right}
177
+ }
178
+ } else if (!(rr[key] instanceof Condition)) {
179
+ right.right = new Condition(key, rr[key], right.op);
180
+ } else {
181
+ rr[key].left = key;
182
+ right.right = rr[key];
183
+ }
184
+ }
185
+ return right.toExpression(objectApiName, left, index);
186
+ }
187
+ return null;
188
+ }
189
+
190
+ // 逻辑操作:and 和 or
191
+ export class Logic {
192
+ value: string
193
+ expressions: { [index: string]: any }
194
+ logics: Logic[]
195
+
196
+ constructor(expressions: { [index: string]: any } = null, logicValue: string = "and") {
197
+ // 通常情况
198
+ this.value = logicValue;
199
+ this.expressions = expressions;
200
+ this.logics = [];
201
+
202
+ if (!expressions) {
203
+ return;
204
+ }
205
+
206
+ // 下钻右值为逻辑表达式 and|or 的情况
207
+ for (let left of Object.keys(expressions)) {
208
+ if (expressions[left] instanceof Object && !(expressions[left] instanceof Array)) {
209
+ let right: {
210
+ [index: string]: any,
211
+ } = expressions[left] as object;
212
+ for (const key of Object.keys(right)) {
213
+ if (right[key] instanceof Logic) {
214
+ this.handleRightLogic(right, left);
215
+ delete expressions[left];
216
+ break;
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ // 右值为逻辑表达式 and|or 的情况
223
+ for (let left of Object.keys(expressions)) {
224
+ let right = expressions[left];
225
+ if (right instanceof Logic) {
226
+ this.handleRightLogic(expressions);
227
+ break;
228
+ }
229
+ }
230
+ }
231
+
232
+ /**
233
+ * 处理:右值包含逻辑表达式and|or的情况
234
+ *
235
+ * @param expressions
236
+ * @param lookupField
237
+ */
238
+ handleRightLogic(expressions: { [index: string]: any }, lookupField: string = null) {
239
+ this.expressions = null;
240
+ for (let left of Object.keys(expressions)) {
241
+ let right = expressions[left];
242
+
243
+ // 1.如果右值为Logic类型
244
+ if (right instanceof Logic) {
245
+ // 1.1 表达式不为空
246
+ if (right.expressions) {
247
+ let exp: { [index: string]: any } = {};
248
+ if (lookupField && right.expressions instanceof Condition) {
249
+ right.expressions.left = lookupField;
250
+ let obj: { [index: string]: any } = {};
251
+ obj[left] = right.expressions.right;
252
+ right.expressions.right = obj
253
+ exp[lookupField] = right.expressions;
254
+ } else {
255
+ exp[left] = right.expressions;
256
+ }
257
+ this.logics.push(new Logic(exp, "and"));
258
+ } else {
259
+ // 1.2 表达式为空
260
+ let log = new Logic(null, right.value);
261
+ for (let logic of right.logics) {
262
+ let exp: { [index: string]: any } = {};
263
+ if (lookupField && logic.expressions instanceof Condition) {
264
+ logic.expressions.left = lookupField;
265
+ let obj: { [index: string]: any } = {};
266
+ obj[left] = logic.expressions.right;
267
+ logic.expressions.right = obj;
268
+ exp[lookupField] = logic.expressions;
269
+ } else {
270
+ if (logic.expressions instanceof Condition) {
271
+ logic.expressions.left = left;
272
+ }
273
+ exp[left] = logic.expressions;
274
+ }
275
+ log.logics.push(new Logic(exp, logic.value));
276
+ }
277
+ this.logics.push(log);
278
+ }
279
+ } else {
280
+ let exp: { [index: string]: any } = {};
281
+ exp[left] = right;
282
+
283
+ let log = new Logic(exp, "and");
284
+ this.logics.push(log);
285
+ }
286
+ }
287
+ }
288
+
289
+ addLogic(logic: Logic) {
290
+ if (logic instanceof Logic) {
291
+ this.logics.push(logic);
292
+ }
293
+ }
294
+ }
295
+
296
+ export function buildLogic(param: (Logic | object)[], logicValue = "and"): Logic {
297
+ if (param.length === 1) {
298
+ return new Logic(param[0], logicValue)
299
+ }
300
+
301
+ let logic = new Logic(null, logicValue);
302
+ param.forEach(p => {
303
+ if (!p) {
304
+ return;
305
+ }
306
+
307
+ // Check:会走到这里吗?
308
+ if (p instanceof Logic) {
309
+ logic.logics.push(p);
310
+ } else if (p instanceof Object && !(p instanceof Array)) {
311
+ logic.logics.push(new Logic(p, "and"));
312
+ } else {
313
+ throw new exceptions.InvalidParamError(`param (${p}) is invalid`);
314
+ }
315
+ });
316
+ return logic;
317
+ }
318
+
319
+ // where参数的树状结构
320
+ class Filter {
321
+ filters: Filter[]
322
+ logic: string
323
+ expression: Expression
324
+
325
+ /**
326
+ * @param logic: filters之间的逻辑关系
327
+ */
328
+ constructor(logic: string) {
329
+ if (!logic) {
330
+ this.logic = "and";
331
+ } else {
332
+ this.logic = logic;
333
+ }
334
+ this.expression = null;
335
+ this.filters = [];
336
+ }
337
+
338
+ // 现在仅考虑一层结构,不考虑嵌套
339
+ toCriterion() {
340
+ let conditions: Expression[] = [];
341
+ let logics: number[] = [];
342
+
343
+ this.filters.forEach((filter, i) => {
344
+ if (!filter.expression) {
345
+ return;
346
+ }
347
+
348
+ conditions.push({
349
+ index: i + 1,
350
+ left: filter.expression.left,
351
+ right: filter.expression.right,
352
+ operator: filter.expression.operator,
353
+ });
354
+ logics.push(i + 1);
355
+ });
356
+
357
+ let criterion = {conditions: conditions, logic: logics.join(" and ")};
358
+ return JSON.stringify(criterion);
359
+ }
360
+ }
361
+
362
+ export async function handleCriterion(criterion: string | Criterion): Promise<string | Criterion> {
363
+ if (typeof criterion === "string") {
364
+ return criterion
365
+ }
366
+
367
+ if (!criterion || !criterion.conditions || !(criterion.conditions instanceof Array)) {
368
+ return criterion;
369
+ }
370
+
371
+ let fieldRes: FieldResType = await handleLookupCondition(criterion.conditions);
372
+
373
+ for (let condition of criterion.conditions) {
374
+ if (fieldRes && fieldRes.lookupMap) {
375
+ let val: LookupField = fieldRes.lookupMap.get(condition.left.settings.fieldPath[0].fieldApiName);
376
+ if (condition.left.settings.fieldPath[1] && val && val.objectApiName) {
377
+ condition.left.settings.fieldPath[1].objectApiName = val.objectApiName;
378
+ }
379
+ }
380
+ await handleCondition(condition, fieldRes);
381
+ }
382
+
383
+ return JSON.stringify(criterion);
384
+ }
385
+
386
+ type FieldResType = {
387
+ isFirstLayer?: boolean
388
+ lookupMap?: Map<string, LookupField>,
389
+ dateSet?: Set<string>
390
+ }
391
+
392
+ async function handleLookupCondition(conditions: Expression[]): Promise<FieldResType> {
393
+ if (!(conditions && conditions[0] && conditions[0].left && conditions[0].left.settings && conditions[0].left.settings.fieldPath)) {
394
+ return null;
395
+ }
396
+
397
+ let queryMap = new Map(), fieldName;
398
+ let opList = [operates.LT, operates.LTE, operates.GT, operates.GTE, operates.HAS_ANY_OF, operates.HAS_NONE_OF];
399
+
400
+ for (const condition of conditions) {
401
+ if (condition.left.settings.fieldPath.length > 1 || opList.includes(condition.operator)) {
402
+ fieldName = condition.left.settings.fieldPath[0].fieldApiName;
403
+ queryMap.set(fieldName, {});
404
+ }
405
+ }
406
+
407
+ if (queryMap.size === 0) {
408
+ return {}
409
+ }
410
+
411
+ let lookupMap = new Map<string, LookupField>(), dateSet = new Set<string>()
412
+
413
+ let firstObjApiName = conditions[0].left.settings.fieldPath[0].objectApiName;
414
+
415
+ let fields = [];
416
+
417
+ if (queryMap.size === 1) {
418
+ fields.push(await Request.GetInstance().getField(firstObjApiName, fieldName));
419
+ } else {
420
+ fields = await Request.GetInstance().getFields(firstObjApiName);
421
+ }
422
+
423
+ for (const field of fields) {
424
+ parseFieldInfo(field, lookupMap, dateSet)
425
+ }
426
+
427
+ return {
428
+ lookupMap: lookupMap,
429
+ dateSet: dateSet,
430
+ }
431
+ }
432
+
433
+ type Field = {
434
+ type: {
435
+ name: string;
436
+ settings: {
437
+ referenced_object_api_name: string;
438
+ is_hierarchy: boolean;
439
+ target_is_hierarchy: boolean;
440
+ return_type: string;
441
+ };
442
+ }
443
+ api_name: string;
444
+ }
445
+
446
+ type LookupField = {
447
+ objectApiName: string,
448
+ isHierarchy: boolean,
449
+ targetIsHierarchy: boolean
450
+ }
451
+
452
+ function parseFieldInfo(field: Field, lookupMap: Map<string, LookupField>, dateSet: Set<string>) {
453
+
454
+ if (!(field && field.type && field.type.name && field.type.settings)) {
455
+ return;
456
+ }
457
+
458
+ switch (field.type.name) {
459
+ case 'lookup':
460
+ lookupMap.set(field.api_name, {
461
+ objectApiName: field.type.settings.referenced_object_api_name,
462
+ isHierarchy: field.type.settings.is_hierarchy,
463
+ targetIsHierarchy: field.type.settings.target_is_hierarchy,
464
+ });
465
+ break;
466
+ case 'date':
467
+ dateSet.add(field.api_name);
468
+ break;
469
+ case 'formula':
470
+ if (field.type.settings.return_type === "date") {
471
+ dateSet.add(field.api_name);
472
+ }
473
+ break;
474
+ }
475
+ }
476
+
477
+ async function handleCondition(condition: Expression, fieldRes: FieldResType) {
478
+ if (!condition || !condition.left || condition.left.type !== "metadataVariable") {
479
+ return;
480
+ }
481
+
482
+ if (!condition.left.settings || !condition.left.settings.fieldPath || !(condition.left.settings.fieldPath instanceof Array)) {
483
+ return;
484
+ }
485
+
486
+ let fieldPathList: PathType[] = condition.left.settings.fieldPath;
487
+ for (let i = 0; i < fieldPathList.length; i++) {
488
+ fieldRes.isFirstLayer = (i === 0);
489
+ if (fieldPathList.length === (i + 1)) { // 最后的查询条件 path 做处理
490
+ switch (condition.operator) {
491
+ case operates.LT:
492
+ // 日期类型的lte,需要转换成isOnOrBefore
493
+ await handleDateAndDatetimeOperator(fieldRes, condition, fieldPathList[i], operates.isOnOrBefore, -1);
494
+ break;
495
+ case operates.LTE:
496
+ // 日期类型的lte,需要转换成isOnOrBefore
497
+ await handleDateAndDatetimeOperator(fieldRes, condition, fieldPathList[i], operates.isOnOrBefore);
498
+ break;
499
+ case operates.GT:
500
+ // 日期类型的gte,需要转换成isOnOrAfter
501
+ await handleDateAndDatetimeOperator(fieldRes, condition, fieldPathList[i], operates.isOnOrAfter, 1);
502
+ break;
503
+ case operates.GTE:
504
+ // 日期类型的gte,需要转换成isOnOrAfter
505
+ await handleDateAndDatetimeOperator(fieldRes, condition, fieldPathList[i], operates.isOnOrAfter);
506
+ break;
507
+ case operates.HAS_ANY_OF:
508
+ case operates.HAS_NONE_OF:
509
+ // hierarchy lookup,需要为extendLogicTags添加all
510
+ // lookup 下钻的话,只在最后一层加 all
511
+ await handleHierarchyFieldPath(fieldRes, fieldPathList[i]);
512
+ break;
513
+ }
514
+ }
515
+
516
+ }
517
+ }
518
+
519
+ // 日期类型的lte,需要转换成isOnOrBefore
520
+ // 日期类型的gte,需要转换成isOnOrAfter
521
+ async function handleDateAndDatetimeOperator(fieldRes: FieldResType, condition: Expression, path: PathType, operator: string, addDay: number = 0) {
522
+ if (!condition || !path || !path.objectApiName || !path.fieldApiName) {
523
+ return;
524
+ }
525
+
526
+ let isDateType = false;
527
+
528
+ if (fieldRes && fieldRes.isFirstLayer) {
529
+ if (fieldRes.dateSet && fieldRes.dateSet.has(path.fieldApiName)) {
530
+ isDateType = true;
531
+ }
532
+ } else {
533
+ let field = await Request.GetInstance().getField(path.objectApiName, path.fieldApiName);
534
+ isDateType = isDate(field)
535
+ }
536
+
537
+ if (isDateType) {
538
+ condition.operator = operator;
539
+ if (condition.right && condition.right.settings && condition.right.settings.data) {
540
+ condition.right.settings.data = dateAdd(condition.right.settings.data as Date, addDay);
541
+ }
542
+ }
543
+ }
544
+
545
+ // hierarchy lookup,需要为extendLogicTags添加all
546
+ async function handleHierarchyFieldPath(fieldRes: FieldResType, path: PathType) {
547
+ if (!path || !path.objectApiName || !path.fieldApiName) {
548
+ return;
549
+ }
550
+
551
+ let isHierarchyType = false;
552
+
553
+ if (fieldRes && fieldRes.isFirstLayer && fieldRes.lookupMap) {
554
+ let val = fieldRes.lookupMap.get(path.fieldApiName);
555
+ if (val && val.targetIsHierarchy) {
556
+ isHierarchyType = true;
557
+ }
558
+ } else {
559
+ let field = await Request.GetInstance().getField(path.objectApiName, path.fieldApiName);
560
+ isHierarchyType = targetIsHierarchy(field);
561
+ }
562
+
563
+ if (!isHierarchyType) {
564
+ return;
565
+ }
566
+
567
+ if (!path.extendLogicTags) {
568
+ path.extendLogicTags = [];
569
+ }
570
+ path.extendLogicTags.push("all");
571
+ }
572
+
573
+ function isHierarchy(field: Field) {
574
+ return field && field.type && field.type.name === "lookup" && field.type.settings && field.type.settings.is_hierarchy;
575
+ }
576
+
577
+ function targetIsHierarchy(field: Field) {
578
+ return field && field.type && field.type.name === "lookup" && field.type.settings && field.type.settings.target_is_hierarchy;
579
+ }
580
+
581
+ function isDate(field: Field) {
582
+ return field && field.type && ((field.type.name === "date") || (field.type.name === "formula" && field.type.settings && field.type.settings.return_type === "date"));
583
+ }
584
+
585
+ function dateAdd(date: Date | fieldType.Date | string, num: number) {
586
+ try {
587
+ if (date instanceof Date) {
588
+ return dayjs(date).add(num, "day").format("YYYY-MM-DD");
589
+ // } else if (date instanceof fieldType.Date) { // instanceof 改动9
590
+ } else if (fieldType.isKFieldTypeDate(date)) {
591
+ return dayjs((date as any).toJSON()).add(num, "day").format("YYYY-MM-DD");
592
+ } else if (typeof (date) === "string") {
593
+ return dayjs(date).add(num, "day").format("YYYY-MM-DD");
594
+ }
595
+ } catch (e) {
596
+ return date;
597
+ }
598
+ }
@@ -0,0 +1,58 @@
1
+ import { OrderV2 } from "../../../request/interface";
2
+ import { UserDataType } from "./expression";
3
+ export declare type FieldPath = {
4
+ objectApiName: string;
5
+ fieldApiName: string;
6
+ extendLogicTags: string[];
7
+ };
8
+ export declare type SettingType = {
9
+ data?: any;
10
+ fieldPath?: FieldPath[];
11
+ };
12
+ export declare type Expression = {
13
+ type: string;
14
+ settings: SettingType;
15
+ };
16
+ export declare function NewMetadataExpressionField(objectApiName: string, fieldApiName: string): Expression;
17
+ export declare function NewConstantExpressionField(data: UserDataType): Expression;
18
+ export declare type ExpressionV2 = {
19
+ leftValue: string;
20
+ operator: string;
21
+ rightValue: any;
22
+ };
23
+ export declare class ArithmeticExpression {
24
+ left: Expression;
25
+ operator: string;
26
+ right: Expression;
27
+ expr: ExpressionV2;
28
+ constructor(leftValue: string, op: string, rightValue: any);
29
+ }
30
+ export declare class LogicalExpression {
31
+ type: string;
32
+ arithmeticExpressions: ArithmeticExpression[];
33
+ logicalExpressions: LogicalExpression[];
34
+ constructor(logicType: string, expressions: ArithmeticExpression[], logics: LogicalExpression[]);
35
+ }
36
+ export declare class QueryV2 {
37
+ objectApiName: string;
38
+ _limit: number;
39
+ _offset: number;
40
+ fields: string[];
41
+ _order: OrderV2[];
42
+ filter: LogicalExpression;
43
+ buildCriterion(filter: LogicalExpression): any[];
44
+ where(condition: LogicalExpression | Record<string, ArithmeticExpression>): QueryV2;
45
+ offset(offset: number): QueryV2;
46
+ limit(limit: number): QueryV2;
47
+ orderBy(fieldApiNames: string[]): QueryV2;
48
+ orderByDesc(fieldApiNames: string[]): QueryV2;
49
+ select(fieldApiNames: string[]): QueryV2;
50
+ constructor(objectApiName: string);
51
+ }
52
+ export declare type logicalRet = {
53
+ logicalExps: LogicalExpression[];
54
+ arithmeticExps: ArithmeticExpression[];
55
+ };
56
+ export declare function logical(exps: (LogicalExpression | Record<string, ArithmeticExpression>)[]): logicalRet;
57
+ export declare type CriterionV2 = (Record<string, CriterionV2> | ExpressionV2)[];
58
+ export declare function buildCriterionV2(filter: LogicalExpression): CriterionV2;