1
- import { createComparatorFromSort , type Filter } from '@rocket.chat/mongo-adapter' ;
1
+ import { createComparatorFromSort , createPredicateFromFilter , type Filter } from '@rocket.chat/mongo-adapter' ;
2
2
import { Meteor } from 'meteor/meteor' ;
3
3
import type { CountDocumentsOptions , UpdateFilter } from 'mongodb' ;
4
4
import type { StoreApi , UseBoundStore } from 'zustand' ;
@@ -80,9 +80,7 @@ export class LocalCollection<T extends { _id: string }> {
80
80
for ( const query of this . queries ) {
81
81
if ( query . dirty ) continue ;
82
82
83
- const matchResult = query . matcher . documentMatches ( doc ) ;
84
-
85
- if ( matchResult . result ) {
83
+ if ( query . predicate ( doc ) ) {
86
84
if ( query . cursor . skip || query . cursor . limit ) {
87
85
queriesToRecompute . add ( query ) ;
88
86
} else {
@@ -109,9 +107,7 @@ export class LocalCollection<T extends { _id: string }> {
109
107
for await ( const query of this . queries ) {
110
108
if ( query . dirty ) continue ;
111
109
112
- const matchResult = query . matcher . documentMatches ( doc ) ;
113
-
114
- if ( matchResult . result ) {
110
+ if ( query . predicate ( doc ) ) {
115
111
if ( query . cursor . skip || query . cursor . limit ) {
116
112
queriesToRecompute . add ( query ) ;
117
113
} else {
@@ -211,11 +207,11 @@ export class LocalCollection<T extends { _id: string }> {
211
207
}
212
208
213
209
private prepareRemove ( selector : Filter < T > ) {
214
- const matcher = new Matcher ( selector ) ;
210
+ const predicate = createPredicateFromFilter ( selector ) ;
215
211
const remove = new Set < T > ( ) ;
216
212
217
213
this . _eachPossiblyMatchingDoc ( selector , ( doc ) => {
218
- if ( matcher . documentMatches ( doc ) . result ) {
214
+ if ( predicate ( doc ) ) {
219
215
remove . add ( doc ) ;
220
216
}
221
217
} ) ;
@@ -227,7 +223,7 @@ export class LocalCollection<T extends { _id: string }> {
227
223
for ( const query of this . queries ) {
228
224
if ( query . dirty ) continue ;
229
225
230
- if ( query . matcher . documentMatches ( removeDoc ) . result ) {
226
+ if ( query . predicate ( removeDoc ) ) {
231
227
if ( query . cursor . skip || query . cursor . limit ) {
232
228
queriesToRecompute . add ( query ) ;
233
229
} else {
@@ -689,7 +685,7 @@ export class LocalCollection<T extends { _id: string }> {
689
685
if ( query . dirty ) continue ;
690
686
691
687
if ( query . ordered ) {
692
- matchedBefore . set ( query , query . matcher . documentMatches ( doc ) . result ) ;
688
+ matchedBefore . set ( query , query . predicate ( doc ) ) ;
693
689
} else {
694
690
matchedBefore . set ( query , query . results . has ( doc . _id ) ) ;
695
691
}
@@ -712,8 +708,7 @@ export class LocalCollection<T extends { _id: string }> {
712
708
for ( const query of this . queries ) {
713
709
if ( query . dirty ) continue ;
714
710
715
- const afterMatch = query . matcher . documentMatches ( doc ) ;
716
- const after = afterMatch . result ;
711
+ const after = query . predicate ( doc ) ;
717
712
const before = matchedBefore . get ( query ) ;
718
713
719
714
if ( query . cursor . skip || query . cursor . limit ) {
@@ -744,8 +739,7 @@ export class LocalCollection<T extends { _id: string }> {
744
739
for await ( const query of this . queries ) {
745
740
if ( query . dirty ) continue ;
746
741
747
- const afterMatch = query . matcher . documentMatches ( doc ) ;
748
- const after = afterMatch . result ;
742
+ const after = query . predicate ( doc ) ;
749
743
const before = matchedBefore . get ( query ) ;
750
744
751
745
if ( query . cursor . skip || query . cursor . limit ) {
@@ -939,21 +933,18 @@ export class LocalCollection<T extends { _id: string }> {
939
933
modifier = clone ( modifier ) ;
940
934
941
935
const isModifier = isOperatorObject ( modifier ) ;
942
- const newDoc = isModifier ? clone ( doc ) : ( modifier as T ) ;
943
936
944
937
if ( isModifier ) {
945
- for ( const operator of Object . keys ( modifier ) ) {
946
- const setOnInsert = options . isInsert && operator === '$setOnInsert' ;
947
- const modFunc = MODIFIERS [ ( setOnInsert ? '$set' : operator ) as keyof typeof MODIFIERS ] ;
948
- const operand = modifier [ operator ] ;
938
+ const newDoc = clone ( doc ) ;
939
+
940
+ for ( const [ operator , operand ] of Object . entries ( modifier ) ) {
941
+ const modFunc = MODIFIERS [ options . isInsert && operator === '$setOnInsert' ? '$set' : ( operator as keyof typeof MODIFIERS ) ] ;
949
942
950
943
if ( ! modFunc ) {
951
944
throw new MinimongoError ( `Invalid modifier specified ${ operator } ` ) ;
952
945
}
953
946
954
- for ( const keypath of Object . keys ( operand ) ) {
955
- const arg = operand [ keypath ] ;
956
-
947
+ for ( const [ keypath , arg ] of Object . entries ( operand ) ) {
957
948
if ( keypath === '' ) {
958
949
throw new MinimongoError ( 'An empty update path is not valid.' ) ;
959
950
}
@@ -981,15 +972,17 @@ export class LocalCollection<T extends { _id: string }> {
981
972
`_id: "${ newDoc . _id } "` ,
982
973
) ;
983
974
}
984
- } else {
985
- if ( doc . _id && modifier . _id && doc . _id !== modifier . _id ) {
986
- throw new MinimongoError ( `The _id field cannot be changed from {_id: "${ doc . _id } "} to {_id: "${ modifier . _id } "}` ) ;
987
- }
988
975
989
- assertHasValidFieldNames ( modifier ) ;
976
+ return newDoc ;
977
+ }
978
+
979
+ if ( doc . _id && modifier . _id && doc . _id !== modifier . _id ) {
980
+ throw new MinimongoError ( `The _id field cannot be changed from {_id: "${ doc . _id } "} to {_id: "${ modifier . _id } "}` ) ;
990
981
}
991
982
992
- return newDoc ;
983
+ assertHasValidFieldNames ( modifier ) ;
984
+
985
+ return modifier as T ;
993
986
}
994
987
995
988
private _removeFromResults ( query : Query < T > , doc : T ) {
@@ -1205,7 +1198,7 @@ const MODIFIERS = {
1205
1198
throw new MinimongoError ( '$rename target field invalid' , { field } ) ;
1206
1199
}
1207
1200
1208
- target2 [ keyparts . pop ( ) as string ] = object ;
1201
+ target2 [ keyparts . pop ( ) as keyof typeof target2 ] = object ;
1209
1202
} ,
1210
1203
$set ( target : Record < string , unknown > , field : string , arg : any ) {
1211
1204
if ( target !== Object ( target ) ) {
@@ -1410,9 +1403,9 @@ const MODIFIERS = {
1410
1403
1411
1404
let out ;
1412
1405
if ( arg != null && typeof arg === 'object' && ! Array . isArray ( arg ) ) {
1413
- const matcher = new Matcher ( arg ) ;
1406
+ const predicate = createPredicateFromFilter ( arg ) ;
1414
1407
1415
- out = toPull . filter ( ( element ) => ! matcher . documentMatches ( element ) . result ) ;
1408
+ out = toPull . filter ( ( element ) => ! predicate ( element ) ) ;
1416
1409
} else {
1417
1410
out = toPull . filter ( ( element ) => ! _f . _equal ( element , arg ) ) ;
1418
1411
}
@@ -1479,7 +1472,7 @@ function assertIsValidFieldName(key: string) {
1479
1472
}
1480
1473
1481
1474
function findModTarget (
1482
- doc : Record < string , any > ,
1475
+ doc : Record < string , any > | unknown [ ] ,
1483
1476
keyparts : ( number | string ) [ ] ,
1484
1477
options : {
1485
1478
noCreate ?: boolean ;
@@ -1562,9 +1555,7 @@ function findModTarget(
1562
1555
}
1563
1556
}
1564
1557
1565
- if ( last ) {
1566
- return doc as { [ index : string | number ] : any } ;
1567
- }
1558
+ if ( last ) return doc ;
1568
1559
1569
1560
doc = doc [ keypart as keyof typeof doc ] ;
1570
1561
}
0 commit comments