@@ -263,20 +263,20 @@ COMPOSITE PK:
263
263
const asc = ( a : string , b : string ) : number => a . localeCompare ( b ) ;
264
264
const notNull = ( el : string | undefined ) : boolean => el != null ;
265
265
266
- function getNewFiltersMapKeysAndMandatoryPopulate < K extends object > (
266
+ function getNewFiltersAndMapKeys < K extends object > (
267
267
cur : FilterQueryDataloader < K > ,
268
268
meta : EntityMetadata < K > ,
269
269
entityName : string ,
270
- ) : Array < [ FilterQueryDataloader < K > , string , Set < string > ? ] > ;
271
- function getNewFiltersMapKeysAndMandatoryPopulate < K extends object > (
270
+ ) : Array < [ FilterQueryDataloader < K > , string ] > ;
271
+ function getNewFiltersAndMapKeys < K extends object > (
272
272
cur : FilterQueryDataloader < K > ,
273
273
meta : EntityMetadata < K > ,
274
- ) : [ FilterQueryDataloader < K > , string , string ? ] ;
275
- function getNewFiltersMapKeysAndMandatoryPopulate < K extends object > (
274
+ ) : [ FilterQueryDataloader < K > , string ] ;
275
+ function getNewFiltersAndMapKeys < K extends object > (
276
276
cur : FilterQueryDataloader < K > ,
277
277
meta : EntityMetadata < K > ,
278
278
entityName ?: string ,
279
- ) : [ FilterQueryDataloader < K > , string , string ? ] | Array < [ FilterQueryDataloader < K > , string , Set < string > ? ] > {
279
+ ) : [ FilterQueryDataloader < K > , string ] | Array < [ FilterQueryDataloader < K > , string ] > {
280
280
const PKs = getPKs ( cur , meta ) ;
281
281
if ( PKs != null ) {
282
282
const res : [ FilterQueryDataloader < K > , string ] = [
@@ -289,8 +289,6 @@ function getNewFiltersMapKeysAndMandatoryPopulate<K extends object>(
289
289
} else {
290
290
const newFilter : any = { } ;
291
291
const keys : string [ ] = [ ] ;
292
- const populateSet = new Set < string > ( ) ;
293
- let computedPopulate : string | undefined ;
294
292
if ( Array . isArray ( cur ) ) {
295
293
// COMPOSITE PKs like [{owner: 1, recipient: 2}, {recipient: 4, owner: 3}]
296
294
for ( const key of meta . primaryKeys ) {
@@ -308,42 +306,27 @@ function getNewFiltersMapKeysAndMandatoryPopulate<K extends object>(
308
306
// Using $or at the top level means that we can treat it as two separate queries and filter results from either of them
309
307
if ( key === "$or" && entityName != null ) {
310
308
return ( value as Array < FilterQueryDataloader < K > > )
311
- . map ( ( el ) => getNewFiltersMapKeysAndMandatoryPopulate ( el , meta , entityName ) )
309
+ . map ( ( el ) => getNewFiltersAndMapKeys ( el , meta , entityName ) )
312
310
. flat ( ) ;
313
311
}
314
312
const keyProp = meta . properties [ key as EntityKey < K > ] ;
315
313
if ( keyProp == null ) {
316
314
throw new Error ( `Cannot find properties for ${ key } ` ) ;
317
315
}
318
316
if ( keyProp . targetMeta == null ) {
319
- // Our current key might lead to a scalar (thus we don't need to populate anything)
320
- // or to an explicited PK like {id: 1, name: 'a'}
321
317
newFilter [ key ] = Array . isArray ( value ) ? value : [ value ] ;
322
318
keys . push ( key ) ;
323
319
} else {
324
- // Our current key points to either a Reference or a Collection
325
- const [ subFilter , subKey , furtherPop ] = getNewFiltersMapKeysAndMandatoryPopulate ( value , keyProp . targetMeta ) ;
320
+ const [ subFilter , subKey ] = getNewFiltersAndMapKeys ( value , keyProp . targetMeta ) ;
326
321
newFilter [ key ] = subFilter ;
327
322
keys . push ( `${ key } :${ subKey } ` ) ;
328
- // We need to populate all Collections and all the References
329
- // where we further match non-PKs properties
330
- if ( keyProp . ref !== true || ! isPK ( value , keyProp . targetMeta ) ) {
331
- computedPopulate = furtherPop == null ? `${ key } ` : `${ key } .${ furtherPop } ` ;
332
- if ( entityName != null ) {
333
- populateSet . add ( computedPopulate ) ;
334
- } else {
335
- // We return computedPopulate as the third element of the array
336
- }
337
- }
338
323
}
339
324
}
340
325
const res : [ FilterQueryDataloader < K > , string ] = [
341
326
newFilter ,
342
327
[ entityName , `{${ keys . sort ( asc ) . join ( "," ) } }` ] . filter ( notNull ) . join ( "|" ) ,
343
328
] ;
344
- return entityName == null
345
- ? [ ...res , computedPopulate ]
346
- : [ [ ...res , populateSet . size === 0 ? undefined : populateSet ] ] ;
329
+ return entityName == null ? res : [ res ] ;
347
330
}
348
331
}
349
332
}
@@ -386,19 +369,19 @@ function updateQueryFilter<K extends object, P extends string = never>(
386
369
}
387
370
388
371
// The least amount of populate necessary to map the dataloader results to their original queries
389
- export function getMandatoryPopulate < K extends object > (
372
+ function getMandatoryPopulate < K extends object > (
390
373
cur : FilterQueryDataloader < K > ,
391
374
meta : EntityMetadata < K > ,
392
375
) : string | undefined ;
393
- export function getMandatoryPopulate < K extends object > (
376
+ function getMandatoryPopulate < K extends object > (
394
377
cur : FilterQueryDataloader < K > ,
395
378
meta : EntityMetadata < K > ,
396
- populate : Set < any > ,
379
+ options : { populate ? : Set < any > } ,
397
380
) : void ;
398
- export function getMandatoryPopulate < K extends object > (
381
+ function getMandatoryPopulate < K extends object > (
399
382
cur : FilterQueryDataloader < K > ,
400
383
meta : EntityMetadata < K > ,
401
- populate ?: Set < any > ,
384
+ options ?: { populate ?: Set < any > } ,
402
385
) : any {
403
386
for ( const [ key , value ] of Object . entries ( cur ) ) {
404
387
const keyProp = meta . properties [ key as EntityKey < K > ] ;
@@ -410,12 +393,14 @@ export function getMandatoryPopulate<K extends object>(
410
393
// Our current key points to either a Reference or a Collection
411
394
// We need to populate all Collections
412
395
// We also need to populate References whenever we have to further match non-PKs properties
413
- const PKs = getPKs ( value , keyProp . targetMeta ) ;
414
- if ( keyProp . ref !== true || PKs == null ) {
415
- const furtherPopulate = getMandatoryPopulate ( value , keyProp . targetMeta ) ;
416
- const computedPopulate = furtherPopulate == null ? `${ key } ` : `${ key } .${ furtherPopulate } ` ;
417
- if ( populate != null ) {
418
- populate . add ( computedPopulate ) ;
396
+ if ( keyProp . ref !== true || ! isPK ( value , keyProp . targetMeta ) ) {
397
+ const furtherPop = getMandatoryPopulate ( value , keyProp . targetMeta ) ;
398
+ const computedPopulate = furtherPop == null ? `${ key } ` : `${ key } .${ furtherPop } ` ;
399
+ if ( options != null ) {
400
+ if ( options . populate == null ) {
401
+ options . populate = new Set ( ) ;
402
+ }
403
+ options . populate . add ( computedPopulate ) ;
419
404
} else {
420
405
return computedPopulate ;
421
406
}
@@ -439,13 +424,15 @@ export function groupFindQueriesByOpts(
439
424
const queriesMap = new Map < string , [ FilterQueryDataloader < any > , { populate ?: true | Set < any > } ?] > ( ) ;
440
425
for ( const dataloaderFind of dataloaderFinds ) {
441
426
const { entityName, meta, filter, options } = dataloaderFind ;
442
- const filtersAndKeys = getNewFiltersMapKeysAndMandatoryPopulate ( filter , meta , entityName ) ;
427
+ const filtersAndKeys = getNewFiltersAndMapKeys ( filter , meta , entityName ) ;
443
428
dataloaderFind . filtersAndKeys = [ ] ;
444
- filtersAndKeys . forEach ( ( [ newFilter , key , mandatoryPopulate ] ) => {
429
+ filtersAndKeys . forEach ( ( [ newFilter , key ] ) => {
445
430
dataloaderFind . filtersAndKeys ?. push ( { key, newFilter } ) ;
446
431
let queryMap = queriesMap . get ( key ) ;
447
432
if ( queryMap == null ) {
448
- queryMap = [ structuredClone ( newFilter ) , { ...( mandatoryPopulate != null && { populate : mandatoryPopulate } ) } ] ;
433
+ const queryMapOpts = { } ;
434
+ queryMap = [ structuredClone ( newFilter ) , queryMapOpts ] ;
435
+ getMandatoryPopulate ( newFilter , meta , queryMapOpts ) ;
449
436
updateQueryFilter ( queryMap , newFilter , options , true ) ;
450
437
queriesMap . set ( key , queryMap ) ;
451
438
} else {
0 commit comments