@@ -248,10 +248,15 @@ function getNewFiltersAndMapKeys<K extends object>(
248
248
}
249
249
}
250
250
251
+ // The purpose of this function on a freshly created query map is just to add populate options
252
+ // to the query map. A brand new query map already contains an array with the current element
253
+ // as its sole value so there is no need to update it, otherwise we would get the cur element twice.
254
+ // TODO: use Sets to avoid duplicates even in subsequent updates.
251
255
function updateQueryFilter < K extends object , P extends string = never > (
252
256
[ acc , accOptions ] : [ FilterQueryDataloader < K > , { populate ?: true | Set < any > } ?] ,
253
257
cur : FilterQueryDataloader < K > ,
254
258
options ?: Pick < FindOptions < K , P > , "populate" > ,
259
+ newQueryMap ?: boolean ,
255
260
) : void {
256
261
if ( options ?. populate != null && accOptions != null && accOptions . populate !== true ) {
257
262
if ( Array . isArray ( options . populate ) && options . populate [ 0 ] === "*" ) {
@@ -266,13 +271,15 @@ function updateQueryFilter<K extends object, P extends string = never>(
266
271
}
267
272
}
268
273
}
269
- for ( const [ key , value ] of Object . entries ( acc ) ) {
270
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
271
- const curValue = ( cur as Record < string , any [ ] > ) [ key ] ! ;
272
- if ( Array . isArray ( value ) ) {
273
- value . push ( ...curValue . reduce < any [ ] > ( ( acc , cur ) => acc . concat ( cur ) , [ ] ) ) ;
274
- } else {
275
- updateQueryFilter ( [ value ] , curValue ) ;
274
+ if ( newQueryMap !== true ) {
275
+ for ( const [ key , value ] of Object . entries ( acc ) ) {
276
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
277
+ const curValue = ( cur as Record < string , any [ ] > ) [ key ] ! ;
278
+ if ( Array . isArray ( value ) ) {
279
+ value . push ( ...curValue . reduce < any [ ] > ( ( acc , cur ) => acc . concat ( cur ) , [ ] ) ) ;
280
+ } else {
281
+ updateQueryFilter ( [ value ] , curValue ) ;
282
+ }
276
283
}
277
284
}
278
285
}
@@ -299,6 +306,7 @@ export function groupFindQueriesByOpts(
299
306
let queryMap = queriesMap . get ( key ) ;
300
307
if ( queryMap == null ) {
301
308
queryMap = [ structuredClone ( newFilter ) , { } ] ;
309
+ updateQueryFilter ( queryMap , newFilter , options , true ) ;
302
310
queriesMap . set ( key , queryMap ) ;
303
311
} else {
304
312
updateQueryFilter ( queryMap , newFilter , options ) ;
0 commit comments