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