@@ -562,28 +562,44 @@ export class MemoryDriver implements Driver {
562562 return logicGroups [ 0 ] . conditions [ 0 ] ;
563563 }
564564
565- // Multiple groups or conditions
566- const finalConditions : Record < string , any > [ ] = [ ] ;
565+ // If there's only one group with multiple conditions, use its logic operator
566+ if ( logicGroups . length === 1 ) {
567+ const group = logicGroups [ 0 ] ;
568+ if ( group . logic === 'or' ) {
569+ return { $or : group . conditions } ;
570+ } else {
571+ return { $and : group . conditions } ;
572+ }
573+ }
574+
575+ // Multiple groups - flatten all conditions and determine the top-level operator
576+ const allConditions : Record < string , any > [ ] = [ ] ;
567577 for ( const group of logicGroups ) {
568578 if ( group . conditions . length === 0 ) continue ;
569579
570580 if ( group . conditions . length === 1 ) {
571- finalConditions . push ( group . conditions [ 0 ] ) ;
581+ allConditions . push ( group . conditions [ 0 ] ) ;
572582 } else {
573583 if ( group . logic === 'or' ) {
574- finalConditions . push ( { $or : group . conditions } ) ;
584+ allConditions . push ( { $or : group . conditions } ) ;
575585 } else {
576- finalConditions . push ( { $and : group . conditions } ) ;
586+ allConditions . push ( { $and : group . conditions } ) ;
577587 }
578588 }
579589 }
580590
581- if ( finalConditions . length === 0 ) {
591+ if ( allConditions . length === 0 ) {
582592 return { } ;
583- } else if ( finalConditions . length === 1 ) {
584- return finalConditions [ 0 ] ;
593+ } else if ( allConditions . length === 1 ) {
594+ return allConditions [ 0 ] ;
585595 } else {
586- return { $and : finalConditions } ;
596+ // Determine top-level operator: use OR if any non-empty group has OR logic
597+ const hasOrLogic = logicGroups . some ( g => g . logic === 'or' && g . conditions . length > 0 ) ;
598+ if ( hasOrLogic ) {
599+ return { $or : allConditions } ;
600+ } else {
601+ return { $and : allConditions } ;
602+ }
587603 }
588604 }
589605
0 commit comments