@fibery/expression-utils 9.0.1 → 9.0.3
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/index.js +6 -0
- package/lib/expression-utils.js +145 -53
- package/lib/visitors.js +144 -52
- package/package.json +3 -3
- package/types.d.ts +17 -2
package/index.js
ADDED
package/lib/expression-utils.js
CHANGED
|
@@ -371,6 +371,131 @@ var utils = {
|
|
|
371
371
|
createExpressionVisitor: createExpressionVisitor
|
|
372
372
|
};
|
|
373
373
|
|
|
374
|
+
const visitFieldExpression = ({
|
|
375
|
+
expression: fieldExpression,
|
|
376
|
+
typeObject: initialTypeObject,
|
|
377
|
+
onField,
|
|
378
|
+
onFieldNotFound
|
|
379
|
+
}) => fieldExpression.reduce(({
|
|
380
|
+
typeObject,
|
|
381
|
+
expression
|
|
382
|
+
}, fieldAccess) => {
|
|
383
|
+
if (typeObject && isMultiFieldAccess(fieldAccess) && typeObject.fieldObjectsById.hasOwnProperty(fieldAccess[0])) {
|
|
384
|
+
var _fieldObject$multiRel;
|
|
385
|
+
const typeId = fieldAccess[1];
|
|
386
|
+
const fieldObject = typeObject.fieldObjectsById[fieldAccess[0]];
|
|
387
|
+
const nextTypeObject = (_fieldObject$multiRel = fieldObject.multiRelatedFieldObjects.find(f => f.holderTypeObject.id === typeId)) == null ? void 0 : _fieldObject$multiRel.holderTypeObject;
|
|
388
|
+
if (!nextTypeObject) {
|
|
389
|
+
const notFoundResult = onFieldNotFound({
|
|
390
|
+
currentTypeObject: typeObject,
|
|
391
|
+
expression: fieldExpression,
|
|
392
|
+
fieldAccess
|
|
393
|
+
});
|
|
394
|
+
return notFoundResult ? {
|
|
395
|
+
typeObject: notFoundResult.typeObject,
|
|
396
|
+
expression: [...expression, notFoundResult.fieldAccess]
|
|
397
|
+
} : {
|
|
398
|
+
typeObject,
|
|
399
|
+
expression
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
const onFieldResult = onField({
|
|
403
|
+
fieldAccess,
|
|
404
|
+
fieldObject,
|
|
405
|
+
typeObject: nextTypeObject
|
|
406
|
+
});
|
|
407
|
+
return onFieldResult ? {
|
|
408
|
+
typeObject: onFieldResult.typeObject,
|
|
409
|
+
expression: [...expression, onFieldResult.fieldAccess]
|
|
410
|
+
} : {
|
|
411
|
+
typeObject,
|
|
412
|
+
expression
|
|
413
|
+
};
|
|
414
|
+
} else if (typeObject && typeObject.fieldObjectsById.hasOwnProperty(fieldAccess)) {
|
|
415
|
+
const fieldObject = typeObject.fieldObjectsById[fieldAccess];
|
|
416
|
+
const onFieldResult = onField({
|
|
417
|
+
fieldAccess,
|
|
418
|
+
fieldObject,
|
|
419
|
+
typeObject: fieldObject.typeObject
|
|
420
|
+
});
|
|
421
|
+
return onFieldResult ? {
|
|
422
|
+
typeObject: onFieldResult.typeObject,
|
|
423
|
+
expression: [...expression, onFieldResult.fieldAccess]
|
|
424
|
+
} : {
|
|
425
|
+
typeObject,
|
|
426
|
+
expression
|
|
427
|
+
};
|
|
428
|
+
} else {
|
|
429
|
+
const notFoundResult = onFieldNotFound({
|
|
430
|
+
currentTypeObject: typeObject,
|
|
431
|
+
expression: fieldExpression,
|
|
432
|
+
fieldAccess
|
|
433
|
+
});
|
|
434
|
+
return notFoundResult ? {
|
|
435
|
+
typeObject: notFoundResult.typeObject,
|
|
436
|
+
expression: [...expression, notFoundResult.fieldAccess]
|
|
437
|
+
} : {
|
|
438
|
+
typeObject,
|
|
439
|
+
expression
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
}, {
|
|
443
|
+
typeObject: initialTypeObject,
|
|
444
|
+
expression: []
|
|
445
|
+
});
|
|
446
|
+
const defaultOnFieldNotFound = () => undefined;
|
|
447
|
+
const defaultOnField = () => undefined;
|
|
448
|
+
const fieldAccessVisitorTypeAware = ({
|
|
449
|
+
typeObject,
|
|
450
|
+
onField = defaultOnField,
|
|
451
|
+
onFieldNotFound = defaultOnFieldNotFound
|
|
452
|
+
}) => {
|
|
453
|
+
const visitor = createExpressionVisitor({
|
|
454
|
+
visitFieldExpression: expression => visitFieldExpression({
|
|
455
|
+
typeObject,
|
|
456
|
+
expression,
|
|
457
|
+
onField,
|
|
458
|
+
onFieldNotFound
|
|
459
|
+
}).expression,
|
|
460
|
+
visitQueryExpression: subQueryExpression => {
|
|
461
|
+
const {
|
|
462
|
+
"q/from": fromExpression,
|
|
463
|
+
"q/select": selectExpression,
|
|
464
|
+
"q/where": whereExpression,
|
|
465
|
+
"q/order-by": orderByExpression
|
|
466
|
+
} = subQueryExpression;
|
|
467
|
+
const fromVisitResult = visitFieldExpression({
|
|
468
|
+
typeObject,
|
|
469
|
+
expression: fromExpression,
|
|
470
|
+
onField,
|
|
471
|
+
onFieldNotFound
|
|
472
|
+
});
|
|
473
|
+
if (fromVisitResult != null && fromVisitResult.typeObject) {
|
|
474
|
+
const subQueryVisitor = fieldAccessVisitorTypeAware({
|
|
475
|
+
typeObject: fromVisitResult == null ? void 0 : fromVisitResult.typeObject,
|
|
476
|
+
onField,
|
|
477
|
+
onFieldNotFound
|
|
478
|
+
});
|
|
479
|
+
return {
|
|
480
|
+
...subQueryExpression,
|
|
481
|
+
...{
|
|
482
|
+
"q/from": fromVisitResult.expression,
|
|
483
|
+
"q/select": ___default["default"].isPlainObject(selectExpression) ? ___default["default"].mapValues(selectExpression, val => subQueryVisitor.visitExpression(val)) : subQueryVisitor.visitExpression(selectExpression)
|
|
484
|
+
},
|
|
485
|
+
...(whereExpression ? {
|
|
486
|
+
"q/where": subQueryVisitor.visitExpression(whereExpression)
|
|
487
|
+
} : null),
|
|
488
|
+
...(orderByExpression ? {
|
|
489
|
+
"q/order-by": subQueryVisitor.visitOrderByExpression(orderByExpression)
|
|
490
|
+
} : null)
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
return subQueryExpression;
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
return visitor;
|
|
497
|
+
};
|
|
498
|
+
|
|
374
499
|
const defaultIdsWithNamesOnFieldNotFound = ({
|
|
375
500
|
fieldExpressionInNamesTerms,
|
|
376
501
|
fieldId
|
|
@@ -423,13 +548,9 @@ const visitFieldExpressionForReplaceIdsWithNamesVisitor = ({
|
|
|
423
548
|
currentTypeObject: typeObject,
|
|
424
549
|
fieldExpressionInNamesTerms: []
|
|
425
550
|
});
|
|
426
|
-
const
|
|
551
|
+
const fieldExpressionVisitor = (typeObject, visitFieldExpression, replacedExpressionKey = "replacedExpression") => {
|
|
427
552
|
const visitor = createExpressionVisitor({
|
|
428
|
-
visitFieldExpression: expression =>
|
|
429
|
-
expression,
|
|
430
|
-
typeObject,
|
|
431
|
-
onFieldNotFound
|
|
432
|
-
}).fieldExpressionInNamesTerms,
|
|
553
|
+
visitFieldExpression: expression => visitFieldExpression(typeObject, expression)[replacedExpressionKey],
|
|
433
554
|
visitQueryExpression: subQueryExpression => {
|
|
434
555
|
const {
|
|
435
556
|
"q/from": fromExpression,
|
|
@@ -437,13 +558,9 @@ const replaceIdsWithNamesVisitor = (typeObject, onFieldNotFound = defaultIdsWith
|
|
|
437
558
|
"q/where": whereExpression,
|
|
438
559
|
"q/order-by": orderByExpression
|
|
439
560
|
} = subQueryExpression;
|
|
440
|
-
const subQueryTypeObject =
|
|
441
|
-
expression: fromExpression,
|
|
442
|
-
onFieldNotFound,
|
|
443
|
-
typeObject
|
|
444
|
-
}).currentTypeObject;
|
|
561
|
+
const subQueryTypeObject = visitFieldExpression(typeObject, fromExpression).currentTypeObject;
|
|
445
562
|
if (subQueryTypeObject) {
|
|
446
|
-
const subQueryVisitor =
|
|
563
|
+
const subQueryVisitor = fieldExpressionVisitor(subQueryTypeObject, visitFieldExpression, replacedExpressionKey);
|
|
447
564
|
return {
|
|
448
565
|
...subQueryExpression,
|
|
449
566
|
...{
|
|
@@ -463,6 +580,13 @@ const replaceIdsWithNamesVisitor = (typeObject, onFieldNotFound = defaultIdsWith
|
|
|
463
580
|
});
|
|
464
581
|
return visitor;
|
|
465
582
|
};
|
|
583
|
+
const replaceIdsWithNamesVisitor = (typeObject, onFieldNotFound = defaultIdsWithNamesOnFieldNotFound) => {
|
|
584
|
+
return fieldExpressionVisitor(typeObject, (typeObject, expression) => visitFieldExpressionForReplaceIdsWithNamesVisitor({
|
|
585
|
+
expression,
|
|
586
|
+
typeObject,
|
|
587
|
+
onFieldNotFound
|
|
588
|
+
}), "fieldExpressionInNamesTerms");
|
|
589
|
+
};
|
|
466
590
|
const defaultNamesWithIdsOnFieldNotFound = ({
|
|
467
591
|
fieldExpressionInIdsTerms,
|
|
468
592
|
field
|
|
@@ -474,8 +598,8 @@ const defaultNamesWithIdsOnFieldNotFound = ({
|
|
|
474
598
|
};
|
|
475
599
|
const visitFieldExpressionForReplaceNamesWithIdsVisitor = ({
|
|
476
600
|
expression,
|
|
477
|
-
|
|
478
|
-
|
|
601
|
+
typeObject,
|
|
602
|
+
onFieldNotFound
|
|
479
603
|
}) => expression.reduce(({
|
|
480
604
|
currentTypeObject,
|
|
481
605
|
fieldExpressionInIdsTerms
|
|
@@ -516,44 +640,11 @@ const visitFieldExpressionForReplaceNamesWithIdsVisitor = ({
|
|
|
516
640
|
fieldExpressionInIdsTerms: []
|
|
517
641
|
});
|
|
518
642
|
const replaceNamesWithIdsVisitor = (typeObject, onFieldNotFound = defaultNamesWithIdsOnFieldNotFound) => {
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
}).fieldExpressionInIdsTerms,
|
|
525
|
-
visitQueryExpression: subQueryExpression => {
|
|
526
|
-
const {
|
|
527
|
-
"q/from": fromExpression,
|
|
528
|
-
"q/select": selectExpression,
|
|
529
|
-
"q/where": whereExpression,
|
|
530
|
-
"q/order-by": orderByExpression
|
|
531
|
-
} = subQueryExpression;
|
|
532
|
-
const subQueryTypeObject = visitFieldExpressionForReplaceNamesWithIdsVisitor({
|
|
533
|
-
expression: fromExpression,
|
|
534
|
-
onFieldNotFound,
|
|
535
|
-
typeObject
|
|
536
|
-
}).currentTypeObject;
|
|
537
|
-
if (subQueryTypeObject) {
|
|
538
|
-
const subQueryVisitor = replaceNamesWithIdsVisitor(subQueryTypeObject, onFieldNotFound);
|
|
539
|
-
return {
|
|
540
|
-
...subQueryExpression,
|
|
541
|
-
...{
|
|
542
|
-
"q/from": visitor.visitFieldExpression(fromExpression),
|
|
543
|
-
"q/select": ___default["default"].isPlainObject(selectExpression) ? ___default["default"].mapValues(selectExpression, val => subQueryVisitor.visitExpression(val)) : subQueryVisitor.visitExpression(selectExpression)
|
|
544
|
-
},
|
|
545
|
-
...(whereExpression ? {
|
|
546
|
-
"q/where": subQueryVisitor.visitExpression(whereExpression)
|
|
547
|
-
} : null),
|
|
548
|
-
...(orderByExpression ? {
|
|
549
|
-
"q/order-by": subQueryVisitor.visitOrderByExpression(orderByExpression)
|
|
550
|
-
} : null)
|
|
551
|
-
};
|
|
552
|
-
}
|
|
553
|
-
return subQueryExpression;
|
|
554
|
-
}
|
|
555
|
-
});
|
|
556
|
-
return visitor;
|
|
643
|
+
return fieldExpressionVisitor(typeObject, (typeObject, expression) => visitFieldExpressionForReplaceNamesWithIdsVisitor({
|
|
644
|
+
expression,
|
|
645
|
+
typeObject,
|
|
646
|
+
onFieldNotFound
|
|
647
|
+
}), "fieldExpressionInIdsTerms");
|
|
557
648
|
};
|
|
558
649
|
const deleteExpressionsWithNotFoundFieldsVisitor = typeObject => {
|
|
559
650
|
const visitor = createExpressionVisitor({
|
|
@@ -810,7 +901,8 @@ var visitors = {
|
|
|
810
901
|
deleteExpressionsWithNotFoundFieldsVisitor: deleteExpressionsWithNotFoundFieldsVisitor,
|
|
811
902
|
expressionContainsAggregation: expressionContainsAggregation,
|
|
812
903
|
UNKNOWN_EXPRESSION_TYPE: UNKNOWN_EXPRESSION_TYPE,
|
|
813
|
-
getExpressionType: getExpressionType
|
|
904
|
+
getExpressionType: getExpressionType,
|
|
905
|
+
fieldAccessVisitorTypeAware: fieldAccessVisitorTypeAware
|
|
814
906
|
};
|
|
815
907
|
|
|
816
908
|
const getEntityQueryVariables = schema => {
|
package/lib/visitors.js
CHANGED
|
@@ -106,6 +106,131 @@ const createExpressionVisitor = visitor => {
|
|
|
106
106
|
return visitorWithDefault;
|
|
107
107
|
};
|
|
108
108
|
|
|
109
|
+
const visitFieldExpression = ({
|
|
110
|
+
expression: fieldExpression,
|
|
111
|
+
typeObject: initialTypeObject,
|
|
112
|
+
onField,
|
|
113
|
+
onFieldNotFound
|
|
114
|
+
}) => fieldExpression.reduce(({
|
|
115
|
+
typeObject,
|
|
116
|
+
expression
|
|
117
|
+
}, fieldAccess) => {
|
|
118
|
+
if (typeObject && isMultiFieldAccess(fieldAccess) && typeObject.fieldObjectsById.hasOwnProperty(fieldAccess[0])) {
|
|
119
|
+
var _fieldObject$multiRel;
|
|
120
|
+
const typeId = fieldAccess[1];
|
|
121
|
+
const fieldObject = typeObject.fieldObjectsById[fieldAccess[0]];
|
|
122
|
+
const nextTypeObject = (_fieldObject$multiRel = fieldObject.multiRelatedFieldObjects.find(f => f.holderTypeObject.id === typeId)) == null ? void 0 : _fieldObject$multiRel.holderTypeObject;
|
|
123
|
+
if (!nextTypeObject) {
|
|
124
|
+
const notFoundResult = onFieldNotFound({
|
|
125
|
+
currentTypeObject: typeObject,
|
|
126
|
+
expression: fieldExpression,
|
|
127
|
+
fieldAccess
|
|
128
|
+
});
|
|
129
|
+
return notFoundResult ? {
|
|
130
|
+
typeObject: notFoundResult.typeObject,
|
|
131
|
+
expression: [...expression, notFoundResult.fieldAccess]
|
|
132
|
+
} : {
|
|
133
|
+
typeObject,
|
|
134
|
+
expression
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const onFieldResult = onField({
|
|
138
|
+
fieldAccess,
|
|
139
|
+
fieldObject,
|
|
140
|
+
typeObject: nextTypeObject
|
|
141
|
+
});
|
|
142
|
+
return onFieldResult ? {
|
|
143
|
+
typeObject: onFieldResult.typeObject,
|
|
144
|
+
expression: [...expression, onFieldResult.fieldAccess]
|
|
145
|
+
} : {
|
|
146
|
+
typeObject,
|
|
147
|
+
expression
|
|
148
|
+
};
|
|
149
|
+
} else if (typeObject && typeObject.fieldObjectsById.hasOwnProperty(fieldAccess)) {
|
|
150
|
+
const fieldObject = typeObject.fieldObjectsById[fieldAccess];
|
|
151
|
+
const onFieldResult = onField({
|
|
152
|
+
fieldAccess,
|
|
153
|
+
fieldObject,
|
|
154
|
+
typeObject: fieldObject.typeObject
|
|
155
|
+
});
|
|
156
|
+
return onFieldResult ? {
|
|
157
|
+
typeObject: onFieldResult.typeObject,
|
|
158
|
+
expression: [...expression, onFieldResult.fieldAccess]
|
|
159
|
+
} : {
|
|
160
|
+
typeObject,
|
|
161
|
+
expression
|
|
162
|
+
};
|
|
163
|
+
} else {
|
|
164
|
+
const notFoundResult = onFieldNotFound({
|
|
165
|
+
currentTypeObject: typeObject,
|
|
166
|
+
expression: fieldExpression,
|
|
167
|
+
fieldAccess
|
|
168
|
+
});
|
|
169
|
+
return notFoundResult ? {
|
|
170
|
+
typeObject: notFoundResult.typeObject,
|
|
171
|
+
expression: [...expression, notFoundResult.fieldAccess]
|
|
172
|
+
} : {
|
|
173
|
+
typeObject,
|
|
174
|
+
expression
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}, {
|
|
178
|
+
typeObject: initialTypeObject,
|
|
179
|
+
expression: []
|
|
180
|
+
});
|
|
181
|
+
const defaultOnFieldNotFound = () => undefined;
|
|
182
|
+
const defaultOnField = () => undefined;
|
|
183
|
+
const fieldAccessVisitorTypeAware = ({
|
|
184
|
+
typeObject,
|
|
185
|
+
onField = defaultOnField,
|
|
186
|
+
onFieldNotFound = defaultOnFieldNotFound
|
|
187
|
+
}) => {
|
|
188
|
+
const visitor = createExpressionVisitor({
|
|
189
|
+
visitFieldExpression: expression => visitFieldExpression({
|
|
190
|
+
typeObject,
|
|
191
|
+
expression,
|
|
192
|
+
onField,
|
|
193
|
+
onFieldNotFound
|
|
194
|
+
}).expression,
|
|
195
|
+
visitQueryExpression: subQueryExpression => {
|
|
196
|
+
const {
|
|
197
|
+
"q/from": fromExpression,
|
|
198
|
+
"q/select": selectExpression,
|
|
199
|
+
"q/where": whereExpression,
|
|
200
|
+
"q/order-by": orderByExpression
|
|
201
|
+
} = subQueryExpression;
|
|
202
|
+
const fromVisitResult = visitFieldExpression({
|
|
203
|
+
typeObject,
|
|
204
|
+
expression: fromExpression,
|
|
205
|
+
onField,
|
|
206
|
+
onFieldNotFound
|
|
207
|
+
});
|
|
208
|
+
if (fromVisitResult != null && fromVisitResult.typeObject) {
|
|
209
|
+
const subQueryVisitor = fieldAccessVisitorTypeAware({
|
|
210
|
+
typeObject: fromVisitResult == null ? void 0 : fromVisitResult.typeObject,
|
|
211
|
+
onField,
|
|
212
|
+
onFieldNotFound
|
|
213
|
+
});
|
|
214
|
+
return {
|
|
215
|
+
...subQueryExpression,
|
|
216
|
+
...{
|
|
217
|
+
"q/from": fromVisitResult.expression,
|
|
218
|
+
"q/select": ___default["default"].isPlainObject(selectExpression) ? ___default["default"].mapValues(selectExpression, val => subQueryVisitor.visitExpression(val)) : subQueryVisitor.visitExpression(selectExpression)
|
|
219
|
+
},
|
|
220
|
+
...(whereExpression ? {
|
|
221
|
+
"q/where": subQueryVisitor.visitExpression(whereExpression)
|
|
222
|
+
} : null),
|
|
223
|
+
...(orderByExpression ? {
|
|
224
|
+
"q/order-by": subQueryVisitor.visitOrderByExpression(orderByExpression)
|
|
225
|
+
} : null)
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
return subQueryExpression;
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
return visitor;
|
|
232
|
+
};
|
|
233
|
+
|
|
109
234
|
const defaultIdsWithNamesOnFieldNotFound = ({
|
|
110
235
|
fieldExpressionInNamesTerms,
|
|
111
236
|
fieldId
|
|
@@ -158,13 +283,9 @@ const visitFieldExpressionForReplaceIdsWithNamesVisitor = ({
|
|
|
158
283
|
currentTypeObject: typeObject,
|
|
159
284
|
fieldExpressionInNamesTerms: []
|
|
160
285
|
});
|
|
161
|
-
const
|
|
286
|
+
const fieldExpressionVisitor = (typeObject, visitFieldExpression, replacedExpressionKey = "replacedExpression") => {
|
|
162
287
|
const visitor = createExpressionVisitor({
|
|
163
|
-
visitFieldExpression: expression =>
|
|
164
|
-
expression,
|
|
165
|
-
typeObject,
|
|
166
|
-
onFieldNotFound
|
|
167
|
-
}).fieldExpressionInNamesTerms,
|
|
288
|
+
visitFieldExpression: expression => visitFieldExpression(typeObject, expression)[replacedExpressionKey],
|
|
168
289
|
visitQueryExpression: subQueryExpression => {
|
|
169
290
|
const {
|
|
170
291
|
"q/from": fromExpression,
|
|
@@ -172,13 +293,9 @@ const replaceIdsWithNamesVisitor = (typeObject, onFieldNotFound = defaultIdsWith
|
|
|
172
293
|
"q/where": whereExpression,
|
|
173
294
|
"q/order-by": orderByExpression
|
|
174
295
|
} = subQueryExpression;
|
|
175
|
-
const subQueryTypeObject =
|
|
176
|
-
expression: fromExpression,
|
|
177
|
-
onFieldNotFound,
|
|
178
|
-
typeObject
|
|
179
|
-
}).currentTypeObject;
|
|
296
|
+
const subQueryTypeObject = visitFieldExpression(typeObject, fromExpression).currentTypeObject;
|
|
180
297
|
if (subQueryTypeObject) {
|
|
181
|
-
const subQueryVisitor =
|
|
298
|
+
const subQueryVisitor = fieldExpressionVisitor(subQueryTypeObject, visitFieldExpression, replacedExpressionKey);
|
|
182
299
|
return {
|
|
183
300
|
...subQueryExpression,
|
|
184
301
|
...{
|
|
@@ -198,6 +315,13 @@ const replaceIdsWithNamesVisitor = (typeObject, onFieldNotFound = defaultIdsWith
|
|
|
198
315
|
});
|
|
199
316
|
return visitor;
|
|
200
317
|
};
|
|
318
|
+
const replaceIdsWithNamesVisitor = (typeObject, onFieldNotFound = defaultIdsWithNamesOnFieldNotFound) => {
|
|
319
|
+
return fieldExpressionVisitor(typeObject, (typeObject, expression) => visitFieldExpressionForReplaceIdsWithNamesVisitor({
|
|
320
|
+
expression,
|
|
321
|
+
typeObject,
|
|
322
|
+
onFieldNotFound
|
|
323
|
+
}), "fieldExpressionInNamesTerms");
|
|
324
|
+
};
|
|
201
325
|
const defaultNamesWithIdsOnFieldNotFound = ({
|
|
202
326
|
fieldExpressionInIdsTerms,
|
|
203
327
|
field
|
|
@@ -209,8 +333,8 @@ const defaultNamesWithIdsOnFieldNotFound = ({
|
|
|
209
333
|
};
|
|
210
334
|
const visitFieldExpressionForReplaceNamesWithIdsVisitor = ({
|
|
211
335
|
expression,
|
|
212
|
-
|
|
213
|
-
|
|
336
|
+
typeObject,
|
|
337
|
+
onFieldNotFound
|
|
214
338
|
}) => expression.reduce(({
|
|
215
339
|
currentTypeObject,
|
|
216
340
|
fieldExpressionInIdsTerms
|
|
@@ -251,44 +375,11 @@ const visitFieldExpressionForReplaceNamesWithIdsVisitor = ({
|
|
|
251
375
|
fieldExpressionInIdsTerms: []
|
|
252
376
|
});
|
|
253
377
|
const replaceNamesWithIdsVisitor = (typeObject, onFieldNotFound = defaultNamesWithIdsOnFieldNotFound) => {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}).fieldExpressionInIdsTerms,
|
|
260
|
-
visitQueryExpression: subQueryExpression => {
|
|
261
|
-
const {
|
|
262
|
-
"q/from": fromExpression,
|
|
263
|
-
"q/select": selectExpression,
|
|
264
|
-
"q/where": whereExpression,
|
|
265
|
-
"q/order-by": orderByExpression
|
|
266
|
-
} = subQueryExpression;
|
|
267
|
-
const subQueryTypeObject = visitFieldExpressionForReplaceNamesWithIdsVisitor({
|
|
268
|
-
expression: fromExpression,
|
|
269
|
-
onFieldNotFound,
|
|
270
|
-
typeObject
|
|
271
|
-
}).currentTypeObject;
|
|
272
|
-
if (subQueryTypeObject) {
|
|
273
|
-
const subQueryVisitor = replaceNamesWithIdsVisitor(subQueryTypeObject, onFieldNotFound);
|
|
274
|
-
return {
|
|
275
|
-
...subQueryExpression,
|
|
276
|
-
...{
|
|
277
|
-
"q/from": visitor.visitFieldExpression(fromExpression),
|
|
278
|
-
"q/select": ___default["default"].isPlainObject(selectExpression) ? ___default["default"].mapValues(selectExpression, val => subQueryVisitor.visitExpression(val)) : subQueryVisitor.visitExpression(selectExpression)
|
|
279
|
-
},
|
|
280
|
-
...(whereExpression ? {
|
|
281
|
-
"q/where": subQueryVisitor.visitExpression(whereExpression)
|
|
282
|
-
} : null),
|
|
283
|
-
...(orderByExpression ? {
|
|
284
|
-
"q/order-by": subQueryVisitor.visitOrderByExpression(orderByExpression)
|
|
285
|
-
} : null)
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
return subQueryExpression;
|
|
289
|
-
}
|
|
290
|
-
});
|
|
291
|
-
return visitor;
|
|
378
|
+
return fieldExpressionVisitor(typeObject, (typeObject, expression) => visitFieldExpressionForReplaceNamesWithIdsVisitor({
|
|
379
|
+
expression,
|
|
380
|
+
typeObject,
|
|
381
|
+
onFieldNotFound
|
|
382
|
+
}), "fieldExpressionInIdsTerms");
|
|
292
383
|
};
|
|
293
384
|
const deleteExpressionsWithNotFoundFieldsVisitor = typeObject => {
|
|
294
385
|
const visitor = createExpressionVisitor({
|
|
@@ -541,6 +632,7 @@ const getExpressionType = ({
|
|
|
541
632
|
exports.UNKNOWN_EXPRESSION_TYPE = UNKNOWN_EXPRESSION_TYPE;
|
|
542
633
|
exports.deleteExpressionsWithNotFoundFieldsVisitor = deleteExpressionsWithNotFoundFieldsVisitor;
|
|
543
634
|
exports.expressionContainsAggregation = expressionContainsAggregation;
|
|
635
|
+
exports.fieldAccessVisitorTypeAware = fieldAccessVisitorTypeAware;
|
|
544
636
|
exports.getExpressionType = getExpressionType;
|
|
545
637
|
exports.replaceIdsWithNamesVisitor = replaceIdsWithNamesVisitor;
|
|
546
638
|
exports.replaceNamesWithIdsVisitor = replaceNamesWithIdsVisitor;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fibery/expression-utils",
|
|
3
|
-
"version": "9.0.
|
|
3
|
+
"version": "9.0.3",
|
|
4
4
|
"description": "utils for working with fibery api expressions",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./lib/expression-utils.js",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"jest": "27.5.1",
|
|
35
35
|
"jest-junit": "13.0.0",
|
|
36
36
|
"microbundle": "0.15.1",
|
|
37
|
-
"@fibery/
|
|
38
|
-
"@fibery/
|
|
37
|
+
"@fibery/babel-preset": "7.4.0",
|
|
38
|
+
"@fibery/eslint-config": "8.6.0"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"@fibery/schema": "10.2.1"
|
package/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
2
|
-
import {Schema, TypeObject} from "@fibery/schema";
|
|
2
|
+
import {Schema, TypeObject, FieldObject} from "@fibery/schema";
|
|
3
3
|
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
5
|
export type $TSFixMe = any;
|
|
@@ -37,9 +37,24 @@ declare module "@fibery/expression-utils" {
|
|
|
37
37
|
expression: $TSFixMe;
|
|
38
38
|
typeObject: TypeObject;
|
|
39
39
|
functionsMeta: Record<string, $TSFixMe>;
|
|
40
|
-
onFieldNotFound?: (input: {currentTypeObject: TypeObject; fieldId: string}) => {
|
|
40
|
+
onFieldNotFound?: (input: {currentTypeObject: TypeObject; fieldId: string}) => {
|
|
41
|
+
currentTypeObject: TypeObject;
|
|
42
|
+
};
|
|
41
43
|
returnRefTypeInsteadOfId?: boolean;
|
|
42
44
|
});
|
|
45
|
+
fieldAccessVisitorTypeAware: (input: {
|
|
46
|
+
typeObject: TypeObject;
|
|
47
|
+
onField?: (onField: {
|
|
48
|
+
fieldAccess: string | [string, string];
|
|
49
|
+
fieldObject: FieldObject;
|
|
50
|
+
typeObject: TypeObject;
|
|
51
|
+
}) => {typeObject: TypeObject; fieldAccess: string | [string, string]} | null;
|
|
52
|
+
onFieldNotFound?: (onFieldNotFound: {
|
|
53
|
+
fieldAccess: string | [string, string];
|
|
54
|
+
currentTypeObject: TypeObject;
|
|
55
|
+
expression: $TSFixMe;
|
|
56
|
+
}) => {typeObject: TypeObject; fieldAccess: string | [string, string]} | null;
|
|
57
|
+
}) => $TSFixMe;
|
|
43
58
|
};
|
|
44
59
|
export const utils: {
|
|
45
60
|
createExpressionVisitor: (visitor: $TSFixMe) => {visitExpression: (expression: $TSFixMe) => $TSFixMe};
|