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