@fibery/expression-utils 6.0.1 → 7.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/expression-utils.js +71 -10
- package/lib/utils.js +3 -5
- package/lib/visitors.js +69 -9
- package/package.json +1 -1
package/lib/expression-utils.js
CHANGED
|
@@ -262,7 +262,7 @@ const collectFieldExpressions = (memo, expression) => {
|
|
|
262
262
|
}
|
|
263
263
|
}
|
|
264
264
|
}
|
|
265
|
-
} else if (isFieldExpression(expression)) {
|
|
265
|
+
} else if (isFieldExpression(expression) || isMultiFieldExpression(expression)) {
|
|
266
266
|
memo.push(expression);
|
|
267
267
|
} else if (expression["q/from"] && isFieldExpression(expression["q/from"])) {
|
|
268
268
|
const innerMemo = [];
|
|
@@ -287,7 +287,6 @@ const createExpressionVisitor = visitor => {
|
|
|
287
287
|
visitFunctionCallExpression: ([fnName, ...args]) => [fnName, ...args.map(x => visitorWithDefault.visitExpression(x))],
|
|
288
288
|
visitFromRootFieldExpression: ([fromRootKeyword, ...rest]) => [fromRootKeyword, ...rest.map(x => visitorWithDefault.visitExpression(x))],
|
|
289
289
|
visitFieldExpression: expression => expression,
|
|
290
|
-
visitMultiFieldExpression: expression => expression,
|
|
291
290
|
visitOrderByExpression: orderByExpression => orderByExpression.map(x => {
|
|
292
291
|
const [fieldExpression, orderDir] = x;
|
|
293
292
|
const fieldExpressionNew = visitorWithDefault.visitExpression(fieldExpression);
|
|
@@ -325,10 +324,8 @@ const createExpressionVisitor = visitor => {
|
|
|
325
324
|
return visitorWithDefault.visitFromRootFieldExpression(expression, visitorDefault);
|
|
326
325
|
} else if (isFunctionCallExpression(expression)) {
|
|
327
326
|
return visitorWithDefault.visitFunctionCallExpression(expression, visitorDefault);
|
|
328
|
-
} else if (isFieldExpression(expression)) {
|
|
327
|
+
} else if (isFieldExpression(expression) || isMultiFieldExpression(expression)) {
|
|
329
328
|
return visitorWithDefault.visitFieldExpression(expression, visitorDefault);
|
|
330
|
-
} else if (isMultiFieldExpression(expression)) {
|
|
331
|
-
return visitorWithDefault.visitMultiFieldExpression(expression, visitorDefault);
|
|
332
329
|
} else if (isQueryExpression(expression)) {
|
|
333
330
|
return visitorWithDefault.visitQueryExpression(expression, visitorDefault);
|
|
334
331
|
} else {
|
|
@@ -360,6 +357,7 @@ var utils = {
|
|
|
360
357
|
isBinaryExpression: isBinaryExpression,
|
|
361
358
|
isNaryExpression: isNaryExpression,
|
|
362
359
|
isVariableExpression: isVariableExpression,
|
|
360
|
+
isMultiFieldAccess: isMultiFieldAccess,
|
|
363
361
|
isMultiFieldExpression: isMultiFieldExpression,
|
|
364
362
|
isFieldExpression: isFieldExpression,
|
|
365
363
|
isQueryExpression: isQueryExpression,
|
|
@@ -384,7 +382,24 @@ const visitFieldExpressionForReplaceIdsWithNamesVisitor = ({
|
|
|
384
382
|
currentTypeObject,
|
|
385
383
|
fieldExpressionInNamesTerms
|
|
386
384
|
}, fieldId) => {
|
|
387
|
-
if (currentTypeObject && currentTypeObject.fieldObjectsById.hasOwnProperty(fieldId)) {
|
|
385
|
+
if (currentTypeObject && isMultiFieldAccess(fieldId) && currentTypeObject.fieldObjectsById.hasOwnProperty(fieldId[0])) {
|
|
386
|
+
var _fieldObject$multiRel;
|
|
387
|
+
const typeId = fieldId[1];
|
|
388
|
+
const fieldObject = currentTypeObject.fieldObjectsById[fieldId[0]];
|
|
389
|
+
const nextTypeObject = (_fieldObject$multiRel = fieldObject.multiRelatedFieldObjects.find(f => f.holderTypeObject.id === typeId)) == null ? void 0 : _fieldObject$multiRel.holderTypeObject;
|
|
390
|
+
if (!nextTypeObject) {
|
|
391
|
+
return onFieldNotFound({
|
|
392
|
+
currentTypeObject,
|
|
393
|
+
fieldExpressionInNamesTerms,
|
|
394
|
+
fieldId: fieldId,
|
|
395
|
+
expression
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
currentTypeObject: nextTypeObject,
|
|
400
|
+
fieldExpressionInNamesTerms: [...fieldExpressionInNamesTerms, [fieldObject.name, nextTypeObject.name]]
|
|
401
|
+
};
|
|
402
|
+
} else if (currentTypeObject && currentTypeObject.fieldObjectsById.hasOwnProperty(fieldId)) {
|
|
388
403
|
const fieldObject = currentTypeObject.fieldObjectsById[fieldId];
|
|
389
404
|
return {
|
|
390
405
|
currentTypeObject: fieldObject.typeObject,
|
|
@@ -459,7 +474,24 @@ const visitFieldExpressionForReplaceNamesWithIdsVisitor = ({
|
|
|
459
474
|
currentTypeObject,
|
|
460
475
|
fieldExpressionInIdsTerms
|
|
461
476
|
}, field) => {
|
|
462
|
-
if (currentTypeObject && currentTypeObject.fieldObjectsByName.hasOwnProperty(field)) {
|
|
477
|
+
if (currentTypeObject && isMultiFieldAccess(field) && currentTypeObject.fieldObjectsByName.hasOwnProperty(field[0])) {
|
|
478
|
+
var _fieldObject$multiRel2;
|
|
479
|
+
const type = field[1];
|
|
480
|
+
const fieldObject = currentTypeObject.fieldObjectsByName[field[0]];
|
|
481
|
+
const nextTypeObject = (_fieldObject$multiRel2 = fieldObject.multiRelatedFieldObjects.find(f => f.holderType === type)) == null ? void 0 : _fieldObject$multiRel2.holderTypeObject;
|
|
482
|
+
if (!nextTypeObject) {
|
|
483
|
+
return onFieldNotFound({
|
|
484
|
+
currentTypeObject,
|
|
485
|
+
fieldExpressionInIdsTerms,
|
|
486
|
+
field,
|
|
487
|
+
expression
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
return {
|
|
491
|
+
currentTypeObject: nextTypeObject,
|
|
492
|
+
fieldExpressionInIdsTerms: [...fieldExpressionInIdsTerms, [fieldObject.id, nextTypeObject.id]]
|
|
493
|
+
};
|
|
494
|
+
} else if (currentTypeObject && currentTypeObject.fieldObjectsByName.hasOwnProperty(field)) {
|
|
463
495
|
const fieldObject = currentTypeObject.fieldObjectsByName[field];
|
|
464
496
|
return {
|
|
465
497
|
currentTypeObject: fieldObject.typeObject,
|
|
@@ -531,7 +563,22 @@ const deleteExpressionsWithNotFoundFieldsVisitor = typeObject => {
|
|
|
531
563
|
}
|
|
532
564
|
},
|
|
533
565
|
visitFieldExpression: expression => {
|
|
534
|
-
const fieldTypeObject = expression.reduce((holderTypeObject, field) =>
|
|
566
|
+
const fieldTypeObject = expression.reduce((holderTypeObject, field) => {
|
|
567
|
+
if (holderTypeObject && isMultiFieldAccess(field) && holderTypeObject.fieldObjectsByName.hasOwnProperty(field[0])) {
|
|
568
|
+
var _fieldObject$multiRel3;
|
|
569
|
+
const type = field[1];
|
|
570
|
+
const fieldObject = holderTypeObject.fieldObjectsByName[field[0]];
|
|
571
|
+
const nextTypeObject = (_fieldObject$multiRel3 = fieldObject.multiRelatedFieldObjects.find(f => f.holderType === type)) == null ? void 0 : _fieldObject$multiRel3.holderTypeObject;
|
|
572
|
+
if (!nextTypeObject) {
|
|
573
|
+
return null;
|
|
574
|
+
}
|
|
575
|
+
return nextTypeObject;
|
|
576
|
+
} else if (holderTypeObject && holderTypeObject.fieldObjectsByName.hasOwnProperty(field)) {
|
|
577
|
+
return holderTypeObject.fieldObjectsByName[field].typeObject;
|
|
578
|
+
} else {
|
|
579
|
+
return null;
|
|
580
|
+
}
|
|
581
|
+
}, typeObject);
|
|
535
582
|
return fieldTypeObject && expression;
|
|
536
583
|
},
|
|
537
584
|
visitOrderByExpression: orderByExpression => {
|
|
@@ -611,8 +658,22 @@ const getFieldAccessExpressionTypeObject = ({
|
|
|
611
658
|
const reduced = expression.reduce(({
|
|
612
659
|
currentTypeObject
|
|
613
660
|
}, fieldId, index) => {
|
|
614
|
-
|
|
615
|
-
|
|
661
|
+
if (currentTypeObject && isMultiFieldAccess(fieldId) && currentTypeObject.fieldObjectsById.hasOwnProperty(fieldId[0])) {
|
|
662
|
+
var _fieldObject$multiRel4;
|
|
663
|
+
const typeId = fieldId[1];
|
|
664
|
+
const fieldObject = currentTypeObject.fieldObjectsById[fieldId[0]];
|
|
665
|
+
const nextTypeObject = (_fieldObject$multiRel4 = fieldObject.multiRelatedFieldObjects.find(f => f.holderTypeObject.id === typeId)) == null ? void 0 : _fieldObject$multiRel4.holderTypeObject;
|
|
666
|
+
if (!nextTypeObject) {
|
|
667
|
+
return onFieldNotFound({
|
|
668
|
+
currentTypeObject,
|
|
669
|
+
fieldId
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
return {
|
|
673
|
+
currentTypeObject: nextTypeObject
|
|
674
|
+
};
|
|
675
|
+
} else if (currentTypeObject && currentTypeObject.fieldObjectsById.hasOwnProperty(fieldId)) {
|
|
676
|
+
const fieldObject = currentTypeObject.fieldObjectsById[fieldId];
|
|
616
677
|
if (returnRefTypeInsteadOfId && index === expression.length - 1 && fieldObject.isId) {
|
|
617
678
|
return {
|
|
618
679
|
currentTypeObject
|
package/lib/utils.js
CHANGED
|
@@ -74,7 +74,7 @@ const collectFieldExpressions = (memo, expression) => {
|
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
-
} else if (isFieldExpression(expression)) {
|
|
77
|
+
} else if (isFieldExpression(expression) || isMultiFieldExpression(expression)) {
|
|
78
78
|
memo.push(expression);
|
|
79
79
|
} else if (expression["q/from"] && isFieldExpression(expression["q/from"])) {
|
|
80
80
|
const innerMemo = [];
|
|
@@ -99,7 +99,6 @@ const createExpressionVisitor = visitor => {
|
|
|
99
99
|
visitFunctionCallExpression: ([fnName, ...args]) => [fnName, ...args.map(x => visitorWithDefault.visitExpression(x))],
|
|
100
100
|
visitFromRootFieldExpression: ([fromRootKeyword, ...rest]) => [fromRootKeyword, ...rest.map(x => visitorWithDefault.visitExpression(x))],
|
|
101
101
|
visitFieldExpression: expression => expression,
|
|
102
|
-
visitMultiFieldExpression: expression => expression,
|
|
103
102
|
visitOrderByExpression: orderByExpression => orderByExpression.map(x => {
|
|
104
103
|
const [fieldExpression, orderDir] = x;
|
|
105
104
|
const fieldExpressionNew = visitorWithDefault.visitExpression(fieldExpression);
|
|
@@ -137,10 +136,8 @@ const createExpressionVisitor = visitor => {
|
|
|
137
136
|
return visitorWithDefault.visitFromRootFieldExpression(expression, visitorDefault);
|
|
138
137
|
} else if (isFunctionCallExpression(expression)) {
|
|
139
138
|
return visitorWithDefault.visitFunctionCallExpression(expression, visitorDefault);
|
|
140
|
-
} else if (isFieldExpression(expression)) {
|
|
139
|
+
} else if (isFieldExpression(expression) || isMultiFieldExpression(expression)) {
|
|
141
140
|
return visitorWithDefault.visitFieldExpression(expression, visitorDefault);
|
|
142
|
-
} else if (isMultiFieldExpression(expression)) {
|
|
143
|
-
return visitorWithDefault.visitMultiFieldExpression(expression, visitorDefault);
|
|
144
141
|
} else if (isQueryExpression(expression)) {
|
|
145
142
|
return visitorWithDefault.visitQueryExpression(expression, visitorDefault);
|
|
146
143
|
} else {
|
|
@@ -168,6 +165,7 @@ exports.isDateRangeFunctionExpression = isDateRangeFunctionExpression;
|
|
|
168
165
|
exports.isFieldExpression = isFieldExpression;
|
|
169
166
|
exports.isFromRootFieldExpression = isFromRootFieldExpression;
|
|
170
167
|
exports.isFunctionCallExpression = isFunctionCallExpression;
|
|
168
|
+
exports.isMultiFieldAccess = isMultiFieldAccess;
|
|
171
169
|
exports.isMultiFieldExpression = isMultiFieldExpression;
|
|
172
170
|
exports.isNaryExpression = isNaryExpression;
|
|
173
171
|
exports.isQueryExpression = isQueryExpression;
|
package/lib/visitors.js
CHANGED
|
@@ -53,7 +53,6 @@ const createExpressionVisitor = visitor => {
|
|
|
53
53
|
visitFunctionCallExpression: ([fnName, ...args]) => [fnName, ...args.map(x => visitorWithDefault.visitExpression(x))],
|
|
54
54
|
visitFromRootFieldExpression: ([fromRootKeyword, ...rest]) => [fromRootKeyword, ...rest.map(x => visitorWithDefault.visitExpression(x))],
|
|
55
55
|
visitFieldExpression: expression => expression,
|
|
56
|
-
visitMultiFieldExpression: expression => expression,
|
|
57
56
|
visitOrderByExpression: orderByExpression => orderByExpression.map(x => {
|
|
58
57
|
const [fieldExpression, orderDir] = x;
|
|
59
58
|
const fieldExpressionNew = visitorWithDefault.visitExpression(fieldExpression);
|
|
@@ -91,10 +90,8 @@ const createExpressionVisitor = visitor => {
|
|
|
91
90
|
return visitorWithDefault.visitFromRootFieldExpression(expression, visitorDefault);
|
|
92
91
|
} else if (isFunctionCallExpression(expression)) {
|
|
93
92
|
return visitorWithDefault.visitFunctionCallExpression(expression, visitorDefault);
|
|
94
|
-
} else if (isFieldExpression(expression)) {
|
|
93
|
+
} else if (isFieldExpression(expression) || isMultiFieldExpression(expression)) {
|
|
95
94
|
return visitorWithDefault.visitFieldExpression(expression, visitorDefault);
|
|
96
|
-
} else if (isMultiFieldExpression(expression)) {
|
|
97
|
-
return visitorWithDefault.visitMultiFieldExpression(expression, visitorDefault);
|
|
98
95
|
} else if (isQueryExpression(expression)) {
|
|
99
96
|
return visitorWithDefault.visitQueryExpression(expression, visitorDefault);
|
|
100
97
|
} else {
|
|
@@ -126,7 +123,24 @@ const visitFieldExpressionForReplaceIdsWithNamesVisitor = ({
|
|
|
126
123
|
currentTypeObject,
|
|
127
124
|
fieldExpressionInNamesTerms
|
|
128
125
|
}, fieldId) => {
|
|
129
|
-
if (currentTypeObject && currentTypeObject.fieldObjectsById.hasOwnProperty(fieldId)) {
|
|
126
|
+
if (currentTypeObject && isMultiFieldAccess(fieldId) && currentTypeObject.fieldObjectsById.hasOwnProperty(fieldId[0])) {
|
|
127
|
+
var _fieldObject$multiRel;
|
|
128
|
+
const typeId = fieldId[1];
|
|
129
|
+
const fieldObject = currentTypeObject.fieldObjectsById[fieldId[0]];
|
|
130
|
+
const nextTypeObject = (_fieldObject$multiRel = fieldObject.multiRelatedFieldObjects.find(f => f.holderTypeObject.id === typeId)) == null ? void 0 : _fieldObject$multiRel.holderTypeObject;
|
|
131
|
+
if (!nextTypeObject) {
|
|
132
|
+
return onFieldNotFound({
|
|
133
|
+
currentTypeObject,
|
|
134
|
+
fieldExpressionInNamesTerms,
|
|
135
|
+
fieldId: fieldId,
|
|
136
|
+
expression
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
currentTypeObject: nextTypeObject,
|
|
141
|
+
fieldExpressionInNamesTerms: [...fieldExpressionInNamesTerms, [fieldObject.name, nextTypeObject.name]]
|
|
142
|
+
};
|
|
143
|
+
} else if (currentTypeObject && currentTypeObject.fieldObjectsById.hasOwnProperty(fieldId)) {
|
|
130
144
|
const fieldObject = currentTypeObject.fieldObjectsById[fieldId];
|
|
131
145
|
return {
|
|
132
146
|
currentTypeObject: fieldObject.typeObject,
|
|
@@ -201,7 +215,24 @@ const visitFieldExpressionForReplaceNamesWithIdsVisitor = ({
|
|
|
201
215
|
currentTypeObject,
|
|
202
216
|
fieldExpressionInIdsTerms
|
|
203
217
|
}, field) => {
|
|
204
|
-
if (currentTypeObject && currentTypeObject.fieldObjectsByName.hasOwnProperty(field)) {
|
|
218
|
+
if (currentTypeObject && isMultiFieldAccess(field) && currentTypeObject.fieldObjectsByName.hasOwnProperty(field[0])) {
|
|
219
|
+
var _fieldObject$multiRel2;
|
|
220
|
+
const type = field[1];
|
|
221
|
+
const fieldObject = currentTypeObject.fieldObjectsByName[field[0]];
|
|
222
|
+
const nextTypeObject = (_fieldObject$multiRel2 = fieldObject.multiRelatedFieldObjects.find(f => f.holderType === type)) == null ? void 0 : _fieldObject$multiRel2.holderTypeObject;
|
|
223
|
+
if (!nextTypeObject) {
|
|
224
|
+
return onFieldNotFound({
|
|
225
|
+
currentTypeObject,
|
|
226
|
+
fieldExpressionInIdsTerms,
|
|
227
|
+
field,
|
|
228
|
+
expression
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
currentTypeObject: nextTypeObject,
|
|
233
|
+
fieldExpressionInIdsTerms: [...fieldExpressionInIdsTerms, [fieldObject.id, nextTypeObject.id]]
|
|
234
|
+
};
|
|
235
|
+
} else if (currentTypeObject && currentTypeObject.fieldObjectsByName.hasOwnProperty(field)) {
|
|
205
236
|
const fieldObject = currentTypeObject.fieldObjectsByName[field];
|
|
206
237
|
return {
|
|
207
238
|
currentTypeObject: fieldObject.typeObject,
|
|
@@ -273,7 +304,22 @@ const deleteExpressionsWithNotFoundFieldsVisitor = typeObject => {
|
|
|
273
304
|
}
|
|
274
305
|
},
|
|
275
306
|
visitFieldExpression: expression => {
|
|
276
|
-
const fieldTypeObject = expression.reduce((holderTypeObject, field) =>
|
|
307
|
+
const fieldTypeObject = expression.reduce((holderTypeObject, field) => {
|
|
308
|
+
if (holderTypeObject && isMultiFieldAccess(field) && holderTypeObject.fieldObjectsByName.hasOwnProperty(field[0])) {
|
|
309
|
+
var _fieldObject$multiRel3;
|
|
310
|
+
const type = field[1];
|
|
311
|
+
const fieldObject = holderTypeObject.fieldObjectsByName[field[0]];
|
|
312
|
+
const nextTypeObject = (_fieldObject$multiRel3 = fieldObject.multiRelatedFieldObjects.find(f => f.holderType === type)) == null ? void 0 : _fieldObject$multiRel3.holderTypeObject;
|
|
313
|
+
if (!nextTypeObject) {
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
return nextTypeObject;
|
|
317
|
+
} else if (holderTypeObject && holderTypeObject.fieldObjectsByName.hasOwnProperty(field)) {
|
|
318
|
+
return holderTypeObject.fieldObjectsByName[field].typeObject;
|
|
319
|
+
} else {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
}, typeObject);
|
|
277
323
|
return fieldTypeObject && expression;
|
|
278
324
|
},
|
|
279
325
|
visitOrderByExpression: orderByExpression => {
|
|
@@ -353,8 +399,22 @@ const getFieldAccessExpressionTypeObject = ({
|
|
|
353
399
|
const reduced = expression.reduce(({
|
|
354
400
|
currentTypeObject
|
|
355
401
|
}, fieldId, index) => {
|
|
356
|
-
|
|
357
|
-
|
|
402
|
+
if (currentTypeObject && isMultiFieldAccess(fieldId) && currentTypeObject.fieldObjectsById.hasOwnProperty(fieldId[0])) {
|
|
403
|
+
var _fieldObject$multiRel4;
|
|
404
|
+
const typeId = fieldId[1];
|
|
405
|
+
const fieldObject = currentTypeObject.fieldObjectsById[fieldId[0]];
|
|
406
|
+
const nextTypeObject = (_fieldObject$multiRel4 = fieldObject.multiRelatedFieldObjects.find(f => f.holderTypeObject.id === typeId)) == null ? void 0 : _fieldObject$multiRel4.holderTypeObject;
|
|
407
|
+
if (!nextTypeObject) {
|
|
408
|
+
return onFieldNotFound({
|
|
409
|
+
currentTypeObject,
|
|
410
|
+
fieldId
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
return {
|
|
414
|
+
currentTypeObject: nextTypeObject
|
|
415
|
+
};
|
|
416
|
+
} else if (currentTypeObject && currentTypeObject.fieldObjectsById.hasOwnProperty(fieldId)) {
|
|
417
|
+
const fieldObject = currentTypeObject.fieldObjectsById[fieldId];
|
|
358
418
|
if (returnRefTypeInsteadOfId && index === expression.length - 1 && fieldObject.isId) {
|
|
359
419
|
return {
|
|
360
420
|
currentTypeObject
|