@fibery/expression-utils 9.7.3 → 10.0.0
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/lib/_virtual/_rolldown/runtime.js +13 -0
- package/lib/index.d.ts +191 -585
- package/lib/index.js +15 -50
- package/lib/src/context-variables.d.ts +27 -22
- package/lib/src/context-variables.js +106 -138
- package/lib/src/errors.d.ts +5 -2
- package/lib/src/errors.js +14 -14
- package/lib/src/field-expression-visitor-type-aware.d.ts +18 -16
- package/lib/src/field-expression-visitor-type-aware.js +116 -131
- package/lib/src/filter-expression/index.d.ts +12 -9
- package/lib/src/filter-expression/index.js +34 -47
- package/lib/src/filter-expression/operator-utils.d.ts +14 -11
- package/lib/src/filter-expression/operator-utils.js +191 -185
- package/lib/src/filter-expression/operators.d.ts +111 -106
- package/lib/src/filter-expression/operators.js +1130 -846
- package/lib/src/filter-expression/pattern-match-placeholders.d.ts +63 -61
- package/lib/src/filter-expression/pattern-match-placeholders.js +107 -138
- package/lib/src/filter-expression/pattern-match.d.ts +17 -15
- package/lib/src/filter-expression/pattern-match.js +118 -130
- package/lib/src/filter-expression/pattern-to-expression.d.ts +13 -11
- package/lib/src/filter-expression/pattern-to-expression.js +42 -45
- package/lib/src/filter-expression/types.d.ts +29 -26
- package/lib/src/filter-expression/types.js +0 -3
- package/lib/src/params-placeholders.d.ts +55 -51
- package/lib/src/params-placeholders.js +199 -192
- package/lib/src/tsfixme.d.ts +4 -1
- package/lib/src/tsfixme.js +0 -3
- package/lib/src/types.d.ts +47 -45
- package/lib/src/types.js +0 -3
- package/lib/src/utils.d.ts +53 -48
- package/lib/src/utils.js +276 -337
- package/lib/src/visitors.d.ts +43 -37
- package/lib/src/visitors.js +306 -365
- package/lib/test-data/schema-mock.d.ts +68 -751
- package/lib/test-data/schema-mock.js +175 -139
- package/package.json +17 -10
- package/lib/index.js.map +0 -1
- package/lib/src/context-variables.js.map +0 -1
- package/lib/src/errors.js.map +0 -1
- package/lib/src/field-expression-visitor-type-aware.js.map +0 -1
- package/lib/src/filter-expression/index.js.map +0 -1
- package/lib/src/filter-expression/operator-utils.js.map +0 -1
- package/lib/src/filter-expression/operators.js.map +0 -1
- package/lib/src/filter-expression/pattern-match-placeholders.js.map +0 -1
- package/lib/src/filter-expression/pattern-match.js.map +0 -1
- package/lib/src/filter-expression/pattern-to-expression.js.map +0 -1
- package/lib/src/filter-expression/types.js.map +0 -1
- package/lib/src/params-placeholders.js.map +0 -1
- package/lib/src/tsfixme.js.map +0 -1
- package/lib/src/types.js.map +0 -1
- package/lib/src/utils.js.map +0 -1
- package/lib/src/visitors.js.map +0 -1
- package/lib/test-data/schema-mock.js.map +0 -1
package/lib/src/utils.js
CHANGED
|
@@ -1,363 +1,302 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { NotImplementedError } from "./errors.js";
|
|
3
|
+
import uniqBy from "lodash/uniqBy.js";
|
|
4
|
+
import { isRecord, mapValues } from "@fibery/helpers/src/_";
|
|
5
|
+
import isPlainObject from "lodash/isPlainObject.js";
|
|
6
|
+
import isString from "lodash/isString.js";
|
|
7
|
+
import { assert } from "@fibery/helpers/utils/trace";
|
|
8
|
+
import last from "lodash/last.js";
|
|
9
|
+
//#region src/utils.ts
|
|
10
|
+
var utils_exports = /* @__PURE__ */ __exportAll({
|
|
11
|
+
assertIsValidExpression: () => assertIsValidExpression,
|
|
12
|
+
convertableDateTypes: () => convertableDateTypes,
|
|
13
|
+
createExpressionVisitor: () => createExpressionVisitor,
|
|
14
|
+
dateRangeFunctions: () => dateRangeFunctions,
|
|
15
|
+
dateRangeTypes: () => dateRangeTypes,
|
|
16
|
+
dateTypes: () => dateTypes,
|
|
17
|
+
extractFieldExpressions: () => extractFieldExpressions,
|
|
18
|
+
firstLastFunctions: () => firstLastFunctions,
|
|
19
|
+
fromRootKeyword: () => fromRootKeyword,
|
|
20
|
+
getFieldObjectByFieldExpression: () => getFieldObjectByFieldExpression,
|
|
21
|
+
getFieldObjects: () => getFieldObjects,
|
|
22
|
+
getFieldObjectsByFieldExpression: () => getFieldObjectsByFieldExpression,
|
|
23
|
+
getFieldObjectsByFieldExpressionInIdsTerms: () => getFieldObjectsByFieldExpressionInIdsTerms,
|
|
24
|
+
isAccessFunctionExpression: () => isAccessFunctionExpression,
|
|
25
|
+
isBinaryExpression: () => isBinaryExpression,
|
|
26
|
+
isCollectionFunctionExpression: () => isCollectionFunctionExpression,
|
|
27
|
+
isDateRangeFunctionExpression: () => isDateRangeFunctionExpression,
|
|
28
|
+
isDateTimeType: () => isDateTimeType,
|
|
29
|
+
isDateType: () => isDateType,
|
|
30
|
+
isFieldExpression: () => isFieldExpression,
|
|
31
|
+
isFieldExpressionValid: () => isFieldExpressionValid,
|
|
32
|
+
isFieldExpressionValidInIdsTerms: () => isFieldExpressionValidInIdsTerms,
|
|
33
|
+
isFromRootFieldExpression: () => isFromRootFieldExpression,
|
|
34
|
+
isFunctionCallExpression: () => isFunctionCallExpression,
|
|
35
|
+
isMultiFieldAccess: () => isMultiFieldAccess,
|
|
36
|
+
isMultiFieldExpression: () => isMultiFieldExpression,
|
|
37
|
+
isNaryExpression: () => isNaryExpression,
|
|
38
|
+
isQueryExpression: () => isQueryExpression,
|
|
39
|
+
isRangeType: () => isRangeType,
|
|
40
|
+
isSingleDateType: () => isSingleDateType,
|
|
41
|
+
isVariableExpression: () => isVariableExpression,
|
|
42
|
+
logicalOperators: () => logicalOperators,
|
|
43
|
+
mathOperators: () => mathOperators,
|
|
44
|
+
numberTypes: () => numberTypes,
|
|
45
|
+
relationalOperators: () => relationalOperators,
|
|
46
|
+
textTypes: () => textTypes
|
|
47
|
+
});
|
|
14
48
|
const assertIsValidExpression = (expression) => {
|
|
15
|
-
|
|
16
|
-
|
|
49
|
+
assert(Array.isArray(expression), "expression must be array", { expression });
|
|
50
|
+
assert(expression.length > 0, "empty expression does not make any sense");
|
|
17
51
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
52
|
+
const textTypes = [
|
|
53
|
+
"fibery/text",
|
|
54
|
+
"fibery/email",
|
|
55
|
+
"fibery/url",
|
|
56
|
+
"fibery/emoji"
|
|
57
|
+
];
|
|
58
|
+
const dateTypes = ["fibery/date", "fibery/date-time"];
|
|
59
|
+
const dateRangeTypes = ["fibery/date-range", "fibery/date-time-range"];
|
|
60
|
+
const convertableDateTypes = ["fibery/date", "fibery/date-range"];
|
|
61
|
+
const numberTypes = ["fibery/int", "fibery/decimal"];
|
|
62
|
+
const dateRangeFunctions = /* @__PURE__ */ new Set(["q/start", "q/end"]);
|
|
63
|
+
const firstLastFunctions = /* @__PURE__ */ new Set(["q/first", "q/last"]);
|
|
64
|
+
const collectionOps = /* @__PURE__ */ new Set([
|
|
65
|
+
"q/count",
|
|
66
|
+
"q/count-distinct",
|
|
67
|
+
"q/sum",
|
|
68
|
+
"q/min",
|
|
69
|
+
"q/max",
|
|
70
|
+
"q/avg",
|
|
71
|
+
"q/join",
|
|
72
|
+
"q/first",
|
|
73
|
+
"q/last"
|
|
74
|
+
]);
|
|
75
|
+
const binaryOperations = /* @__PURE__ */ new Set([
|
|
76
|
+
"=",
|
|
77
|
+
"!=",
|
|
78
|
+
"<",
|
|
79
|
+
">",
|
|
80
|
+
"<=",
|
|
81
|
+
">=",
|
|
82
|
+
"in",
|
|
83
|
+
"q/contains",
|
|
84
|
+
"q/not-contains",
|
|
85
|
+
"+",
|
|
86
|
+
"-",
|
|
87
|
+
"q/+",
|
|
88
|
+
"q/-",
|
|
89
|
+
"*",
|
|
90
|
+
"/",
|
|
91
|
+
"and",
|
|
92
|
+
"or",
|
|
93
|
+
"q/and",
|
|
94
|
+
"q/or",
|
|
95
|
+
"q/in",
|
|
96
|
+
"q/not-in"
|
|
97
|
+
]);
|
|
98
|
+
const logicalOperators = /* @__PURE__ */ new Set([
|
|
99
|
+
"and",
|
|
100
|
+
"or",
|
|
101
|
+
"q/and",
|
|
102
|
+
"q/or"
|
|
103
|
+
]);
|
|
104
|
+
const relationalOperators = /* @__PURE__ */ new Set([
|
|
105
|
+
"=",
|
|
106
|
+
"!=",
|
|
107
|
+
"<",
|
|
108
|
+
">",
|
|
109
|
+
"<=",
|
|
110
|
+
">="
|
|
36
111
|
]);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"<=",
|
|
46
|
-
">=",
|
|
47
|
-
"in", //asc: obsolete,use q/in
|
|
48
|
-
"q/contains",
|
|
49
|
-
"q/not-contains",
|
|
50
|
-
"+",
|
|
51
|
-
"-",
|
|
52
|
-
"q/+",
|
|
53
|
-
"q/-",
|
|
54
|
-
"*",
|
|
55
|
-
"/",
|
|
56
|
-
"and",
|
|
57
|
-
"or", //asc: obsolete. use q/and, q/or
|
|
58
|
-
"q/and",
|
|
59
|
-
"q/or",
|
|
60
|
-
"q/in",
|
|
61
|
-
"q/not-in",
|
|
112
|
+
const mathOperators = /* @__PURE__ */ new Set([
|
|
113
|
+
"+",
|
|
114
|
+
"-",
|
|
115
|
+
"*",
|
|
116
|
+
"/",
|
|
117
|
+
"q/+",
|
|
118
|
+
"q/-",
|
|
119
|
+
"q/concat"
|
|
62
120
|
]);
|
|
63
|
-
exports.logicalOperators = new Set(["and", "or", "q/and", "q/or"]);
|
|
64
|
-
exports.relationalOperators = new Set(["=", "!=", "<", ">", "<=", ">="]);
|
|
65
|
-
exports.mathOperators = new Set(["+", "-", "*", "/", "q/+", "q/-", "q/concat"]);
|
|
66
121
|
const isDateType = (type) => type === "fibery/date" || type === "fibery/date-range";
|
|
67
|
-
exports.isDateType = isDateType;
|
|
68
122
|
const isDateTimeType = (type) => type === "fibery/date-time" || type === "fibery/date-time-range";
|
|
69
|
-
exports.isDateTimeType = isDateTimeType;
|
|
70
123
|
const isSingleDateType = (type) => type === "fibery/date" || type === "fibery/date-time";
|
|
71
|
-
exports.isSingleDateType = isSingleDateType;
|
|
72
124
|
const isRangeType = (type) => type === "fibery/date-range" || type === "fibery/date-time-range";
|
|
73
|
-
exports.isRangeType = isRangeType;
|
|
74
125
|
const isFunctionCallExpression = (expression) => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
126
|
+
if (!Array.isArray(expression)) return false;
|
|
127
|
+
if (expression.length <= 1) return false;
|
|
128
|
+
const firstElement = expression[0];
|
|
129
|
+
if (!isString(firstElement)) return false;
|
|
130
|
+
return firstElement.startsWith("q/") || [
|
|
131
|
+
"=",
|
|
132
|
+
"!=",
|
|
133
|
+
"<",
|
|
134
|
+
">",
|
|
135
|
+
"<=",
|
|
136
|
+
">=",
|
|
137
|
+
"+",
|
|
138
|
+
"-",
|
|
139
|
+
"*",
|
|
140
|
+
"/",
|
|
141
|
+
"in",
|
|
142
|
+
"and",
|
|
143
|
+
"or",
|
|
144
|
+
"not-in"
|
|
145
|
+
].includes(firstElement);
|
|
87
146
|
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
(expression.length === 2 || expression.length === 3) &&
|
|
94
|
-
exports.dateRangeFunctions.has(expression[0]) &&
|
|
95
|
-
(0, exports.isFieldExpression)(expression[1]);
|
|
96
|
-
exports.isDateRangeFunctionExpression = isDateRangeFunctionExpression;
|
|
97
|
-
const isCollectionFunctionExpression = (expression) => Array.isArray(expression) &&
|
|
98
|
-
(expression.length === 2 || expression.length === 3) &&
|
|
99
|
-
collectionOps.has(expression[0]);
|
|
100
|
-
exports.isCollectionFunctionExpression = isCollectionFunctionExpression;
|
|
101
|
-
const isAccessFunctionExpression = (expression) => Array.isArray(expression) &&
|
|
102
|
-
expression.length === 2 &&
|
|
103
|
-
expression[0] === "q/access?" &&
|
|
104
|
-
(0, exports.isFieldExpression)(expression[1]);
|
|
105
|
-
exports.isAccessFunctionExpression = isAccessFunctionExpression;
|
|
147
|
+
const fromRootKeyword = "q/from-root";
|
|
148
|
+
const isFromRootFieldExpression = (expression) => Array.isArray(expression) && expression[0] === "q/from-root";
|
|
149
|
+
const isDateRangeFunctionExpression = (expression) => Array.isArray(expression) && (expression.length === 2 || expression.length === 3) && dateRangeFunctions.has(expression[0]) && isFieldExpression(expression[1]);
|
|
150
|
+
const isCollectionFunctionExpression = (expression) => Array.isArray(expression) && (expression.length === 2 || expression.length === 3) && collectionOps.has(expression[0]);
|
|
151
|
+
const isAccessFunctionExpression = (expression) => Array.isArray(expression) && expression.length === 2 && expression[0] === "q/access?" && isFieldExpression(expression[1]);
|
|
106
152
|
const isBinaryExpression = (expression) => Array.isArray(expression) && expression.length === 3 && binaryOperations.has(expression[0]);
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
const isMultiFieldAccess = (expression) => Array.isArray(expression) &&
|
|
113
|
-
expression.length === 2 &&
|
|
114
|
-
!(0, exports.isFunctionCallExpression)(expression) &&
|
|
115
|
-
expression.every((x) => (0, isString_1.default)(x));
|
|
116
|
-
exports.isMultiFieldAccess = isMultiFieldAccess;
|
|
117
|
-
const isMultiFieldExpression = (expression) => Array.isArray(expression) &&
|
|
118
|
-
!(0, exports.isFunctionCallExpression)(expression) &&
|
|
119
|
-
expression.some((x) => (0, exports.isMultiFieldAccess)(x)) &&
|
|
120
|
-
expression.every((x) => !(0, exports.isVariableExpression)(x) && !((0, isString_1.default)(x) && binaryOperations.has(x)) && ((0, isString_1.default)(x) || (0, exports.isMultiFieldAccess)(x)));
|
|
121
|
-
exports.isMultiFieldExpression = isMultiFieldExpression;
|
|
122
|
-
const isFieldExpression = (expression) => Array.isArray(expression) &&
|
|
123
|
-
!(0, exports.isFunctionCallExpression)(expression) &&
|
|
124
|
-
expression.every((x) => !(0, exports.isVariableExpression)(x) && (0, isString_1.default)(x));
|
|
125
|
-
exports.isFieldExpression = isFieldExpression;
|
|
153
|
+
const isNaryExpression = (expression) => Array.isArray(expression) && expression.length > 1 && logicalOperators.has(expression[0]);
|
|
154
|
+
const isVariableExpression = (expression) => isString(expression) && expression.startsWith("$");
|
|
155
|
+
const isMultiFieldAccess = (expression) => Array.isArray(expression) && expression.length === 2 && !isFunctionCallExpression(expression) && expression.every((x) => isString(x));
|
|
156
|
+
const isMultiFieldExpression = (expression) => Array.isArray(expression) && !isFunctionCallExpression(expression) && expression.some((x) => isMultiFieldAccess(x)) && expression.every((x) => !isVariableExpression(x) && !(isString(x) && binaryOperations.has(x)) && (isString(x) || isMultiFieldAccess(x)));
|
|
157
|
+
const isFieldExpression = (expression) => Array.isArray(expression) && !isFunctionCallExpression(expression) && expression.every((x) => !isVariableExpression(x) && isString(x));
|
|
126
158
|
const isQueryExpression = (expression) => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
(0, exports.isFromRootFieldExpression)(fromExpression) ||
|
|
133
|
-
(0, exports.isMultiFieldExpression)(fromExpression));
|
|
134
|
-
}
|
|
135
|
-
return false;
|
|
159
|
+
if (isRecord(expression) && "q/from" in expression) {
|
|
160
|
+
const fromExpression = expression["q/from"];
|
|
161
|
+
return fromExpression === null || isFieldExpression(fromExpression) || isFromRootFieldExpression(fromExpression) || isMultiFieldExpression(fromExpression);
|
|
162
|
+
}
|
|
163
|
+
return false;
|
|
136
164
|
};
|
|
137
|
-
exports.isQueryExpression = isQueryExpression;
|
|
138
165
|
const collectFieldExpressions = (memo, expression) => {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if ((0, isString_1.default)(part)) {
|
|
149
|
-
// field path shortcut
|
|
150
|
-
memo.push([part]);
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
collectFieldExpressions(memo, part);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
else if ((0, exports.isFieldExpression)(expression) || (0, exports.isMultiFieldExpression)(expression)) {
|
|
159
|
-
memo.push(expression);
|
|
160
|
-
}
|
|
161
|
-
else if (expression["q/from"] && (0, exports.isFieldExpression)(expression["q/from"])) {
|
|
162
|
-
const innerMemo = [];
|
|
163
|
-
expression["q/select"] &&
|
|
164
|
-
collectFieldExpressions(innerMemo, expression["q/select"]);
|
|
165
|
-
expression["q/where"] && collectFieldExpressions(innerMemo, expression["q/where"]);
|
|
166
|
-
for (const fieldExpression of innerMemo) {
|
|
167
|
-
memo.push([...expression["q/from"], ...fieldExpression]);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
throw new errors_1.NotImplementedError(expression, "expression");
|
|
172
|
-
}
|
|
166
|
+
if (isVariableExpression(expression)) {} else if (isFunctionCallExpression(expression)) for (const part of expression.slice(1)) if (isVariableExpression(part) || part === null) {} else if (isString(part)) memo.push([part]);
|
|
167
|
+
else collectFieldExpressions(memo, part);
|
|
168
|
+
else if (isFieldExpression(expression) || isMultiFieldExpression(expression)) memo.push(expression);
|
|
169
|
+
else if (expression["q/from"] && isFieldExpression(expression["q/from"])) {
|
|
170
|
+
const innerMemo = [];
|
|
171
|
+
expression["q/select"] && collectFieldExpressions(innerMemo, expression["q/select"]);
|
|
172
|
+
expression["q/where"] && collectFieldExpressions(innerMemo, expression["q/where"]);
|
|
173
|
+
for (const fieldExpression of innerMemo) memo.push([...expression["q/from"], ...fieldExpression]);
|
|
174
|
+
} else throw new NotImplementedError(expression, "expression");
|
|
173
175
|
};
|
|
174
|
-
const getFieldObjectsByFieldExpression = ({ typeObject, expression
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return fieldObjects;
|
|
176
|
+
const getFieldObjectsByFieldExpression = ({ typeObject, expression }) => {
|
|
177
|
+
assertIsValidExpression(expression);
|
|
178
|
+
const fieldObjects = [];
|
|
179
|
+
let currentTypeObject = typeObject;
|
|
180
|
+
for (const fieldOrMultiFieldAccess of expression) if (isMultiFieldAccess(fieldOrMultiFieldAccess)) {
|
|
181
|
+
const [multiField, type] = fieldOrMultiFieldAccess;
|
|
182
|
+
const fieldObject = currentTypeObject.fieldObjectsByName[multiField];
|
|
183
|
+
fieldObjects.push(fieldObject);
|
|
184
|
+
const foundField = fieldObject.multiRelatedFieldObjects.find((f) => f.holderType === type);
|
|
185
|
+
assert(foundField, "incorrect expression for multi-field");
|
|
186
|
+
currentTypeObject = foundField.holderTypeObject;
|
|
187
|
+
} else {
|
|
188
|
+
const fieldObject = currentTypeObject.fieldObjectsByName[fieldOrMultiFieldAccess];
|
|
189
|
+
fieldObjects.push(fieldObject);
|
|
190
|
+
currentTypeObject = fieldObject.typeObject;
|
|
191
|
+
}
|
|
192
|
+
return fieldObjects;
|
|
194
193
|
};
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
currentTypeObject = fieldObject.typeObject;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return fieldObjects;
|
|
194
|
+
const getFieldObjectsByFieldExpressionInIdsTerms = ({ typeObject, expression }) => {
|
|
195
|
+
assertIsValidExpression(expression);
|
|
196
|
+
const fieldObjects = [];
|
|
197
|
+
let currentTypeObject = typeObject;
|
|
198
|
+
for (const fieldOrMultiFieldAccess of expression) if (isMultiFieldAccess(fieldOrMultiFieldAccess)) {
|
|
199
|
+
const [multiField, typeId] = fieldOrMultiFieldAccess;
|
|
200
|
+
const fieldObject = currentTypeObject.fieldObjectsById[multiField];
|
|
201
|
+
fieldObjects.push(fieldObject);
|
|
202
|
+
const foundField = fieldObject.multiRelatedFieldObjects.find((f) => f.holderTypeObject.id === typeId);
|
|
203
|
+
assert(foundField, "incorrect expression for multi-field");
|
|
204
|
+
currentTypeObject = foundField.holderTypeObject;
|
|
205
|
+
} else {
|
|
206
|
+
const fieldObject = currentTypeObject.fieldObjectsById[fieldOrMultiFieldAccess];
|
|
207
|
+
fieldObjects.push(fieldObject);
|
|
208
|
+
currentTypeObject = fieldObject.typeObject;
|
|
209
|
+
}
|
|
210
|
+
return fieldObjects;
|
|
216
211
|
};
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const foundField = fieldObject.multiRelatedFieldObjects.find((f) => f.holderType === type);
|
|
231
|
-
if (!foundField) {
|
|
232
|
-
return false;
|
|
233
|
-
}
|
|
234
|
-
return foundField.holderTypeObject;
|
|
235
|
-
}
|
|
236
|
-
else {
|
|
237
|
-
if (Object.hasOwn(typeObject.fieldObjectsByName, fieldOrMultiFieldAccess)) {
|
|
238
|
-
return typeObject.fieldObjectsByName[fieldOrMultiFieldAccess].typeObject;
|
|
239
|
-
}
|
|
240
|
-
else {
|
|
241
|
-
return false;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}, typeObject));
|
|
212
|
+
const isFieldExpressionValid = ({ typeObject, expression }) => {
|
|
213
|
+
assertIsValidExpression(expression);
|
|
214
|
+
return Boolean(expression.reduce((typeObject, fieldOrMultiFieldAccess) => {
|
|
215
|
+
if (!typeObject) return false;
|
|
216
|
+
if (isMultiFieldAccess(fieldOrMultiFieldAccess)) {
|
|
217
|
+
const [multiField, type] = fieldOrMultiFieldAccess;
|
|
218
|
+
if (!Object.hasOwn(typeObject.fieldObjectsByName, multiField)) return false;
|
|
219
|
+
const foundField = typeObject.fieldObjectsByName[multiField].multiRelatedFieldObjects.find((f) => f.holderType === type);
|
|
220
|
+
if (!foundField) return false;
|
|
221
|
+
return foundField.holderTypeObject;
|
|
222
|
+
} else if (Object.hasOwn(typeObject.fieldObjectsByName, fieldOrMultiFieldAccess)) return typeObject.fieldObjectsByName[fieldOrMultiFieldAccess].typeObject;
|
|
223
|
+
else return false;
|
|
224
|
+
}, typeObject));
|
|
245
225
|
};
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
return false;
|
|
262
|
-
}
|
|
263
|
-
return foundField.holderTypeObject;
|
|
264
|
-
}
|
|
265
|
-
else {
|
|
266
|
-
if (!Object.hasOwn(currentTypeObject.fieldObjectsById, fieldOrMultiFieldAccess)) {
|
|
267
|
-
return false;
|
|
268
|
-
}
|
|
269
|
-
return currentTypeObject.fieldObjectsById[fieldOrMultiFieldAccess].typeObject;
|
|
270
|
-
}
|
|
271
|
-
}, typeObject));
|
|
226
|
+
const isFieldExpressionValidInIdsTerms = ({ typeObject, expression }) => {
|
|
227
|
+
assertIsValidExpression(expression);
|
|
228
|
+
return Boolean(expression.reduce((currentTypeObject, fieldOrMultiFieldAccess) => {
|
|
229
|
+
if (!currentTypeObject) return false;
|
|
230
|
+
if (isMultiFieldAccess(fieldOrMultiFieldAccess)) {
|
|
231
|
+
const [multiField, typeId] = fieldOrMultiFieldAccess;
|
|
232
|
+
if (!Object.hasOwn(currentTypeObject.fieldObjectsById, multiField)) return false;
|
|
233
|
+
const foundField = currentTypeObject.fieldObjectsById[multiField].multiRelatedFieldObjects.find((f) => f.holderTypeObject.id === typeId);
|
|
234
|
+
if (!foundField) return false;
|
|
235
|
+
return foundField.holderTypeObject;
|
|
236
|
+
} else {
|
|
237
|
+
if (!Object.hasOwn(currentTypeObject.fieldObjectsById, fieldOrMultiFieldAccess)) return false;
|
|
238
|
+
return currentTypeObject.fieldObjectsById[fieldOrMultiFieldAccess].typeObject;
|
|
239
|
+
}
|
|
240
|
+
}, typeObject));
|
|
272
241
|
};
|
|
273
|
-
|
|
274
|
-
const getFieldObjectByFieldExpression = (x) => (0, last_1.default)((0, exports.getFieldObjectsByFieldExpression)(x));
|
|
275
|
-
exports.getFieldObjectByFieldExpression = getFieldObjectByFieldExpression;
|
|
242
|
+
const getFieldObjectByFieldExpression = (x) => last(getFieldObjectsByFieldExpression(x));
|
|
276
243
|
const getFieldObjects = (expression, typeObject) => {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
244
|
+
const fieldExpression = extractFieldExpressions(expression)[0];
|
|
245
|
+
return getFieldObjectsByFieldExpression({
|
|
246
|
+
typeObject,
|
|
247
|
+
expression: fieldExpression
|
|
248
|
+
});
|
|
282
249
|
};
|
|
283
|
-
exports.getFieldObjects = getFieldObjects;
|
|
284
250
|
/** @deprecated
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
251
|
+
* This method checks few expression forms, that we do not generate on frontend anymore(field-access shortcut, direct value as parameter).
|
|
252
|
+
* I'm afraid to replace it with implementation based on visitor everywhere, as visitors don't allow such expression forms and I don't want to extend visitor with those forms.
|
|
253
|
+
* If you need this method in new places, consider implementing same logic on visitors and use it.
|
|
254
|
+
**/
|
|
289
255
|
const extractFieldExpressions = (expression) => {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
256
|
+
const memo = [];
|
|
257
|
+
collectFieldExpressions(memo, expression);
|
|
258
|
+
return uniqBy(memo, (x) => x.join(","));
|
|
293
259
|
};
|
|
294
|
-
exports.extractFieldExpressions = extractFieldExpressions;
|
|
295
260
|
const createExpressionVisitor = (visitor) => {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
visitExpression: (expression) => {
|
|
336
|
-
if (expression === null) {
|
|
337
|
-
throw new errors_1.NotImplementedError(expression, "expression");
|
|
338
|
-
}
|
|
339
|
-
else if ((0, exports.isVariableExpression)(expression)) {
|
|
340
|
-
return visitorWithDefault.visitVariableExpression(expression, visitorDefault);
|
|
341
|
-
}
|
|
342
|
-
else if ((0, exports.isFromRootFieldExpression)(expression)) {
|
|
343
|
-
return visitorWithDefault.visitFromRootFieldExpression(expression, visitorDefault);
|
|
344
|
-
}
|
|
345
|
-
else if ((0, exports.isFunctionCallExpression)(expression)) {
|
|
346
|
-
return visitorWithDefault.visitFunctionCallExpression(expression, visitorDefault);
|
|
347
|
-
}
|
|
348
|
-
else if ((0, exports.isFieldExpression)(expression) || (0, exports.isMultiFieldExpression)(expression)) {
|
|
349
|
-
return visitorWithDefault.visitFieldExpression(expression, visitorDefault);
|
|
350
|
-
}
|
|
351
|
-
else if ((0, exports.isQueryExpression)(expression)) {
|
|
352
|
-
return visitorWithDefault.visitQueryExpression(expression, visitorDefault);
|
|
353
|
-
}
|
|
354
|
-
else {
|
|
355
|
-
throw new errors_1.NotImplementedError(expression, "expression");
|
|
356
|
-
}
|
|
357
|
-
},
|
|
358
|
-
};
|
|
359
|
-
visitorWithDefault = { ...visitorDefault, ...visitor };
|
|
360
|
-
return visitorWithDefault;
|
|
261
|
+
let visitorWithDefault = null;
|
|
262
|
+
const visitorDefault = {
|
|
263
|
+
visitVariableExpression: (expression) => expression,
|
|
264
|
+
visitFunctionCallExpression: ([fnName, ...args]) => [fnName, ...args.map((x) => visitorWithDefault.visitExpression(x))],
|
|
265
|
+
visitFromRootFieldExpression: ([fromRootKeyword, ...rest]) => [fromRootKeyword, ...rest.map((x) => visitorWithDefault.visitExpression(x))],
|
|
266
|
+
visitFieldExpression: (expression) => expression,
|
|
267
|
+
visitOrderByExpression: (orderByExpression) => orderByExpression.map((x) => {
|
|
268
|
+
const [fieldExpression, orderDir, ...rest] = x;
|
|
269
|
+
return [
|
|
270
|
+
visitorWithDefault.visitExpression(fieldExpression),
|
|
271
|
+
orderDir,
|
|
272
|
+
...rest
|
|
273
|
+
];
|
|
274
|
+
}),
|
|
275
|
+
visitQueryExpression: (subQueryExpression) => {
|
|
276
|
+
const { "q/from": fromExpression, "q/select": selectExpression, "q/where": whereExpression, "q/order-by": orderByExpression } = subQueryExpression;
|
|
277
|
+
return {
|
|
278
|
+
...subQueryExpression,
|
|
279
|
+
...fromExpression ? { "q/from": visitorWithDefault.visitFieldExpression(fromExpression, visitorDefault) } : null,
|
|
280
|
+
...selectExpression ? { "q/select": isPlainObject(selectExpression) ? mapValues(selectExpression, (val) => visitorWithDefault.visitExpression(val)) : visitorWithDefault.visitExpression(selectExpression) } : null,
|
|
281
|
+
...whereExpression ? { "q/where": visitorWithDefault.visitExpression(whereExpression) } : null,
|
|
282
|
+
...orderByExpression ? { "q/order-by": visitorWithDefault.visitOrderByExpression(orderByExpression, visitorDefault) } : null
|
|
283
|
+
};
|
|
284
|
+
},
|
|
285
|
+
visitExpression: (expression) => {
|
|
286
|
+
if (expression === null) throw new NotImplementedError(expression, "expression");
|
|
287
|
+
else if (isVariableExpression(expression)) return visitorWithDefault.visitVariableExpression(expression, visitorDefault);
|
|
288
|
+
else if (isFromRootFieldExpression(expression)) return visitorWithDefault.visitFromRootFieldExpression(expression, visitorDefault);
|
|
289
|
+
else if (isFunctionCallExpression(expression)) return visitorWithDefault.visitFunctionCallExpression(expression, visitorDefault);
|
|
290
|
+
else if (isFieldExpression(expression) || isMultiFieldExpression(expression)) return visitorWithDefault.visitFieldExpression(expression, visitorDefault);
|
|
291
|
+
else if (isQueryExpression(expression)) return visitorWithDefault.visitQueryExpression(expression, visitorDefault);
|
|
292
|
+
else throw new NotImplementedError(expression, "expression");
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
visitorWithDefault = {
|
|
296
|
+
...visitorDefault,
|
|
297
|
+
...visitor
|
|
298
|
+
};
|
|
299
|
+
return visitorWithDefault;
|
|
361
300
|
};
|
|
362
|
-
|
|
363
|
-
|
|
301
|
+
//#endregion
|
|
302
|
+
export { assertIsValidExpression, convertableDateTypes, createExpressionVisitor, dateRangeFunctions, dateRangeTypes, dateTypes, extractFieldExpressions, firstLastFunctions, fromRootKeyword, getFieldObjectByFieldExpression, getFieldObjects, getFieldObjectsByFieldExpression, getFieldObjectsByFieldExpressionInIdsTerms, isAccessFunctionExpression, isBinaryExpression, isCollectionFunctionExpression, isDateRangeFunctionExpression, isDateTimeType, isDateType, isFieldExpression, isFieldExpressionValid, isFieldExpressionValidInIdsTerms, isFromRootFieldExpression, isFunctionCallExpression, isMultiFieldAccess, isMultiFieldExpression, isNaryExpression, isQueryExpression, isRangeType, isSingleDateType, isVariableExpression, logicalOperators, mathOperators, numberTypes, relationalOperators, textTypes, utils_exports };
|