@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.
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 -337
  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,363 +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 isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
10
- const isString_1 = __importDefault(require("lodash/isString"));
11
- const last_1 = __importDefault(require("lodash/last"));
12
- const uniqBy_1 = __importDefault(require("lodash/uniqBy"));
13
- 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
+ });
14
48
  const assertIsValidExpression = (expression) => {
15
- (0, trace_1.assert)(Array.isArray(expression), "expression must be array", { expression });
16
- (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");
17
51
  };
18
- exports.assertIsValidExpression = assertIsValidExpression;
19
- exports.textTypes = ["fibery/text", "fibery/email", "fibery/url", "fibery/emoji"];
20
- exports.dateTypes = ["fibery/date", "fibery/date-time"];
21
- exports.dateRangeTypes = ["fibery/date-range", "fibery/date-time-range"];
22
- exports.convertableDateTypes = ["fibery/date", "fibery/date-range"];
23
- exports.numberTypes = ["fibery/int", "fibery/decimal"];
24
- exports.dateRangeFunctions = new Set(["q/start", "q/end"]);
25
- exports.firstLastFunctions = new Set(["q/first", "q/last"]);
26
- const collectionOps = new Set([
27
- "q/count",
28
- "q/count-distinct",
29
- "q/sum",
30
- "q/min",
31
- "q/max",
32
- "q/avg",
33
- "q/join",
34
- "q/first",
35
- "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
+ ">="
36
111
  ]);
37
- // [op, left, right]
38
- // [=, $true, $false]
39
- // [=, $my-id, ["fibery/id"]]
40
- const binaryOperations = new Set([
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
- if (!Array.isArray(expression)) {
76
- return false;
77
- }
78
- if (expression.length <= 1) {
79
- return false;
80
- }
81
- const firstElement = expression[0];
82
- if (!(0, isString_1.default)(firstElement)) {
83
- return false;
84
- }
85
- return (firstElement.startsWith("q/") ||
86
- ["=", "!=", "<", ">", "<=", ">=", "+", "-", "*", "/", "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);
87
146
  };
88
- exports.isFunctionCallExpression = isFunctionCallExpression;
89
- exports.fromRootKeyword = "q/from-root";
90
- const isFromRootFieldExpression = (expression) => Array.isArray(expression) && expression[0] === exports.fromRootKeyword;
91
- exports.isFromRootFieldExpression = isFromRootFieldExpression;
92
- const isDateRangeFunctionExpression = (expression) => Array.isArray(expression) &&
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
- exports.isBinaryExpression = isBinaryExpression;
108
- const isNaryExpression = (expression) => Array.isArray(expression) && expression.length > 1 && exports.logicalOperators.has(expression[0]);
109
- exports.isNaryExpression = isNaryExpression;
110
- const isVariableExpression = (expression) => (0, isString_1.default)(expression) && expression.startsWith("$");
111
- exports.isVariableExpression = isVariableExpression;
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
- if ((0, _1.isRecord)(expression) && "q/from" in expression) {
128
- const fromExpression = expression["q/from"];
129
- //asc: fromExpression === null for denormalizeSelect for reference collection case
130
- return (fromExpression === null ||
131
- (0, exports.isFieldExpression)(fromExpression) ||
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
- if ((0, exports.isVariableExpression)(expression)) {
140
- /* empty */
141
- }
142
- else if ((0, exports.isFunctionCallExpression)(expression)) {
143
- for (const part of expression.slice(1)) {
144
- if ((0, exports.isVariableExpression)(part) || part === null) {
145
- /* empty */
146
- }
147
- else {
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
- (0, exports.assertIsValidExpression)(expression);
176
- const fieldObjects = [];
177
- let currentTypeObject = typeObject;
178
- for (const fieldOrMultiFieldAccess of expression) {
179
- if ((0, exports.isMultiFieldAccess)(fieldOrMultiFieldAccess)) {
180
- const [multiField, type] = fieldOrMultiFieldAccess;
181
- const fieldObject = currentTypeObject.fieldObjectsByName[multiField];
182
- fieldObjects.push(fieldObject);
183
- const foundField = fieldObject.multiRelatedFieldObjects.find((f) => f.holderType === type);
184
- (0, trace_1.assert)(foundField, "incorrect expression for multi-field");
185
- currentTypeObject = foundField.holderTypeObject;
186
- }
187
- else {
188
- const fieldObject = currentTypeObject.fieldObjectsByName[fieldOrMultiFieldAccess];
189
- fieldObjects.push(fieldObject);
190
- currentTypeObject = fieldObject.typeObject;
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
- exports.getFieldObjectsByFieldExpression = getFieldObjectsByFieldExpression;
196
- const getFieldObjectsByFieldExpressionInIdsTerms = ({ typeObject, expression, }) => {
197
- (0, exports.assertIsValidExpression)(expression);
198
- const fieldObjects = [];
199
- let currentTypeObject = typeObject;
200
- for (const fieldOrMultiFieldAccess of expression) {
201
- if ((0, exports.isMultiFieldAccess)(fieldOrMultiFieldAccess)) {
202
- const [multiField, typeId] = fieldOrMultiFieldAccess;
203
- const fieldObject = currentTypeObject.fieldObjectsById[multiField];
204
- fieldObjects.push(fieldObject);
205
- const foundField = fieldObject.multiRelatedFieldObjects.find((f) => f.holderTypeObject.id === typeId);
206
- (0, trace_1.assert)(foundField, "incorrect expression for multi-field");
207
- currentTypeObject = foundField.holderTypeObject;
208
- }
209
- else {
210
- const fieldObject = currentTypeObject.fieldObjectsById[fieldOrMultiFieldAccess];
211
- fieldObjects.push(fieldObject);
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
- exports.getFieldObjectsByFieldExpressionInIdsTerms = getFieldObjectsByFieldExpressionInIdsTerms;
218
- const isFieldExpressionValid = ({ typeObject, expression, }) => {
219
- (0, exports.assertIsValidExpression)(expression);
220
- return Boolean(expression.reduce((typeObject, fieldOrMultiFieldAccess) => {
221
- if (!typeObject) {
222
- return false;
223
- }
224
- if ((0, exports.isMultiFieldAccess)(fieldOrMultiFieldAccess)) {
225
- const [multiField, type] = fieldOrMultiFieldAccess;
226
- if (!Object.hasOwn(typeObject.fieldObjectsByName, multiField)) {
227
- return false;
228
- }
229
- const fieldObject = typeObject.fieldObjectsByName[multiField];
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
- exports.isFieldExpressionValid = isFieldExpressionValid;
247
- const isFieldExpressionValidInIdsTerms = ({ typeObject, expression, }) => {
248
- (0, exports.assertIsValidExpression)(expression);
249
- return Boolean(expression.reduce((currentTypeObject, fieldOrMultiFieldAccess) => {
250
- if (!currentTypeObject) {
251
- return false;
252
- }
253
- if ((0, exports.isMultiFieldAccess)(fieldOrMultiFieldAccess)) {
254
- const [multiField, typeId] = fieldOrMultiFieldAccess;
255
- if (!Object.hasOwn(currentTypeObject.fieldObjectsById, multiField)) {
256
- return false;
257
- }
258
- const fieldObject = currentTypeObject.fieldObjectsById[multiField];
259
- const foundField = fieldObject.multiRelatedFieldObjects.find((f) => f.holderTypeObject.id === typeId);
260
- if (!foundField) {
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
- exports.isFieldExpressionValidInIdsTerms = isFieldExpressionValidInIdsTerms;
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
- const fieldExpression = (0, exports.extractFieldExpressions)(expression)[0];
278
- return (0, exports.getFieldObjectsByFieldExpression)({
279
- typeObject,
280
- expression: fieldExpression,
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
- * This method checks few expression forms, that we do not generate on frontend anymore(field-access shortcut, direct value as parameter).
286
- * 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.
287
- * If you need this method in new places, consider implementing same logic on visitors and use it.
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
- const memo = [];
291
- collectFieldExpressions(memo, expression);
292
- return (0, uniqBy_1.default)(memo, (x) => x.join(","));
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
- let visitorWithDefault = null;
297
- const visitorDefault = {
298
- visitVariableExpression: (expression) => expression,
299
- visitFunctionCallExpression: ([fnName, ...args]) => [
300
- fnName,
301
- ...args.map((x) => visitorWithDefault.visitExpression(x)),
302
- ],
303
- visitFromRootFieldExpression: ([fromRootKeyword, ...rest]) => [
304
- fromRootKeyword,
305
- ...rest.map((x) => visitorWithDefault.visitExpression(x)),
306
- ],
307
- visitFieldExpression: (expression) => expression,
308
- visitOrderByExpression: (orderByExpression) => orderByExpression.map((x) => {
309
- const [fieldExpression, orderDir, ...rest] = x;
310
- const fieldExpressionNew = visitorWithDefault.visitExpression(fieldExpression);
311
- return [fieldExpressionNew, orderDir, ...rest];
312
- }),
313
- visitQueryExpression: (subQueryExpression) => {
314
- const { "q/from": fromExpression, "q/select": selectExpression, "q/where": whereExpression, "q/order-by": orderByExpression, } = subQueryExpression;
315
- return {
316
- ...subQueryExpression,
317
- ...(fromExpression
318
- ? { "q/from": visitorWithDefault.visitFieldExpression(fromExpression, visitorDefault) }
319
- : null),
320
- ...(selectExpression
321
- ? {
322
- "q/select": (0, isPlainObject_1.default)(selectExpression)
323
- ? (0, _1.mapValues)(selectExpression, (val) => visitorWithDefault.visitExpression(val))
324
- : visitorWithDefault.visitExpression(selectExpression),
325
- }
326
- : null),
327
- ...(whereExpression ? { "q/where": visitorWithDefault.visitExpression(whereExpression) } : null),
328
- ...(orderByExpression
329
- ? {
330
- "q/order-by": visitorWithDefault.visitOrderByExpression(orderByExpression, visitorDefault),
331
- }
332
- : null),
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
- exports.createExpressionVisitor = createExpressionVisitor;
363
- //# 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 };