@@ -332,10 +332,10 @@ export const createAbilityBuilder = <
332332 queryFilters ?: DrizzleQueryFunctionInput < DB , TableName > ,
333333 ) {
334334 const internalTransformer = (
335- queryFilters ?: DrizzleQueryFunctionInput < DB , TableName > ,
335+ filters ?: DrizzleQueryFunctionInput < DB , TableName > ,
336336 ) => {
337337 const limit = lazy ( ( ) => {
338- let limit = queryFilters ?. limit as number | undefined ;
338+ let limit = filters ?. limit as number | undefined ;
339339
340340 if (
341341 defaultLimit &&
@@ -351,7 +351,7 @@ export const createAbilityBuilder = <
351351 // we acutally need to define multiple return objects since we do not want to use delete for
352352 // performance reasons and an undefined columns field on a drizzle filter will prevent any
353353 // column from being selected at all
354- if ( queryFilters ?. columns ) {
354+ if ( filters ?. columns ) {
355355 return {
356356 /**
357357 * Query filters for the drizzle query API.
@@ -367,8 +367,8 @@ export const createAbilityBuilder = <
367367 * For find first calls
368368 */
369369 single : {
370- where : queryFilters ?. where ,
371- columns : queryFilters ?. columns ,
370+ where : filters ?. where ,
371+ columns : filters ?. columns ,
372372 } as Pick <
373373 NonNullable <
374374 NonNullable <
@@ -381,8 +381,8 @@ export const createAbilityBuilder = <
381381 * For find many calls
382382 */
383383 many : {
384- where : queryFilters ?. where ,
385- columns : queryFilters ?. columns ,
384+ where : filters ?. where ,
385+ columns : filters ?. columns ,
386386 get limit ( ) {
387387 return limit ( ) ;
388388 } ,
@@ -416,10 +416,10 @@ export const createAbilityBuilder = <
416416 */
417417 sql : {
418418 get where ( ) {
419- return queryFilters ?. where
419+ return filters ?. where
420420 ? relationsFilterToSQL (
421421 tableSchema as any ,
422- queryFilters . where ,
422+ filters . where ,
423423 )
424424 : undefined ;
425425 } ,
@@ -441,7 +441,7 @@ export const createAbilityBuilder = <
441441 * For find first calls
442442 */
443443 single : {
444- where : queryFilters ?. where ,
444+ where : filters ?. where ,
445445 } as Pick <
446446 NonNullable <
447447 NonNullable <
@@ -454,7 +454,7 @@ export const createAbilityBuilder = <
454454 * For find many calls
455455 */
456456 many : {
457- where : queryFilters ?. where ,
457+ where : filters ?. where ,
458458 get limit ( ) {
459459 return limit ( ) ;
460460 } ,
@@ -488,10 +488,10 @@ export const createAbilityBuilder = <
488488 */
489489 sql : {
490490 get where ( ) {
491- return queryFilters ?. where
491+ return filters ?. where
492492 ? relationsFilterToSQL (
493493 tableSchema as any ,
494- queryFilters . where ,
494+ filters . where ,
495495 )
496496 : undefined ;
497497 } ,
@@ -505,19 +505,17 @@ export const createAbilityBuilder = <
505505 /**
506506 * Merges the current query filters with the provided filters for this call only
507507 */
508- function merge <
509- P extends NonNullable < DrizzleQueryFunctionInput < DB , TableName > > [ ] ,
510- > ( ... p : P ) {
511- const merged = mergeFilters ( ret . query . many , ... p ) ;
508+ function merge (
509+ p : NonNullable < DrizzleQueryFunctionInput < DB , TableName > > ,
510+ ) {
511+ const merged = mergeFilters ( ret . query . many , p ) ;
512512 return internalTransformer ( merged ) ;
513513 }
514514
515515 ( ret as any ) . merge = merge ;
516516
517517 return ret as typeof ret & {
518- merge : typeof merge <
519- NonNullable < DrizzleQueryFunctionInput < DB , TableName > > [ ]
520- > ;
518+ merge : typeof merge ;
521519 } ;
522520 }
523521
@@ -571,53 +569,14 @@ export const createAbilityBuilder = <
571569 return transformToResponse ( blockEverythingFilter ) ;
572570 }
573571
574- let highestLimit : number | undefined ;
575- for ( let i = 0 ; i < allQueryFilters . length ; i ++ ) {
576- const conditionObject = allQueryFilters [ i ] ;
577- if ( conditionObject ?. limit ) {
578- if (
579- highestLimit === undefined ||
580- ( conditionObject . limit as number ) > highestLimit
581- ) {
582- highestLimit = conditionObject . limit as number ;
583- }
584- }
585- }
586-
587- let allowedColumns : Set < string > | undefined ;
588- for ( let i = 0 ; i < allQueryFilters . length ; i ++ ) {
589- const conditionObject = allQueryFilters [ i ] ;
590- if ( conditionObject ?. columns ) {
591- if ( allowedColumns === undefined ) {
592- allowedColumns = new Set (
593- Object . keys ( conditionObject . columns ) ,
594- ) ;
595- } else {
596- const fields = Object . keys ( conditionObject . columns ) ;
597- for ( let i = 0 ; i < fields . length ; i ++ ) {
598- allowedColumns . add ( fields [ i ] ) ;
599- }
600- }
601- }
602- }
572+ const mergedFilters =
573+ allQueryFilters . length === 1
574+ ? allQueryFilters [ 0 ]
575+ : allQueryFilters . reduce ( ( a , b ) => {
576+ return mergeFilters ( a , b ) ;
577+ } , { } ) ;
603578
604- const accumulatedWhereConditions = allQueryFilters
605- . filter ( ( o ) => o ?. where )
606- . map ( ( o ) => o ! . where ) ;
607-
608- return transformToResponse ( {
609- where :
610- accumulatedWhereConditions . length > 0
611- ? { OR : accumulatedWhereConditions }
612- : undefined ,
613- columns : allowedColumns
614- ? allowedColumns . values ( ) . reduce ( ( prev , curr ) => {
615- prev [ curr ] = true ;
616- return prev ;
617- } , { } as any )
618- : undefined ,
619- limit : highestLimit ,
620- } as any ) ;
579+ return transformToResponse ( mergedFilters as any ) ;
621580 } ,
622581 } ;
623582 } ,
0 commit comments