@@ -1244,6 +1244,137 @@ describe('MongoDB Compatibility Tests', () => {
12441244 } ) ;
12451245 } ) ;
12461246
1247+ describe ( 'Batch Handle Operations' , ( ) => {
1248+ it ( 'should pass null to handle for non-existing documents' , async ( ) => {
1249+ const collection = pongoDb . collection < User > ( 'batchHandleCollection' ) ;
1250+ const nonExistingId = uuid ( ) ;
1251+
1252+ let receivedDoc : User | null = undefined as unknown as User | null ;
1253+ await collection . handle ( [ nonExistingId ] , ( existing ) => {
1254+ receivedDoc = existing ;
1255+ return null ;
1256+ } ) ;
1257+
1258+ assert . strictEqual ( receivedDoc , null ) ;
1259+ } ) ;
1260+
1261+ it ( 'should insert new documents for non-existing ids' , async ( ) => {
1262+ const collection = pongoDb . collection < User > ( 'batchHandleCollection' ) ;
1263+ const id1 = uuid ( ) ;
1264+ const id2 = uuid ( ) ;
1265+
1266+ const results = await collection . handle ( [ id1 , id2 ] , ( _existing ) => ( {
1267+ name : 'Batch User' ,
1268+ age : 10 ,
1269+ } ) ) ;
1270+
1271+ assert ( results . every ( ( r ) => r . successful ) ) ;
1272+
1273+ const doc1 = await collection . findOne ( { _id : id1 } ) ;
1274+ const doc2 = await collection . findOne ( { _id : id2 } ) ;
1275+
1276+ assert . strictEqual ( doc1 ?. name , 'Batch User' ) ;
1277+ assert . strictEqual ( doc2 ?. name , 'Batch User' ) ;
1278+ } ) ;
1279+
1280+ it ( 'should replace existing documents' , async ( ) => {
1281+ const collection = pongoDb . collection < User > ( 'batchHandleCollection' ) ;
1282+
1283+ const doc : User = { name : 'Original' , age : 1 } ;
1284+ const insertResult = await collection . insertOne ( doc ) ;
1285+ const id = insertResult . insertedId ! ;
1286+
1287+ const results = await collection . handle ( [ id ] , ( existing ) =>
1288+ existing ? { ...existing , age : 99 } : null ,
1289+ ) ;
1290+
1291+ assert ( results [ 0 ] ?. successful ) ;
1292+
1293+ const updated = await collection . findOne ( { _id : id } ) ;
1294+ assert . strictEqual ( updated ?. age , 99 ) ;
1295+ assert . strictEqual ( updated ?. _version , 2n ) ;
1296+ } ) ;
1297+
1298+ it ( 'should delete existing documents when handler returns null' , async ( ) => {
1299+ const collection = pongoDb . collection < User > ( 'batchHandleCollection' ) ;
1300+
1301+ const doc1 : User = { name : 'ToDelete1' , age : 1 } ;
1302+ const doc2 : User = { name : 'ToDelete2' , age : 2 } ;
1303+ const r1 = await collection . insertOne ( doc1 ) ;
1304+ const r2 = await collection . insertOne ( doc2 ) ;
1305+
1306+ const results = await collection . handle (
1307+ [ r1 . insertedId ! , r2 . insertedId ! ] ,
1308+ ( ) => null ,
1309+ ) ;
1310+
1311+ assert ( results . every ( ( r ) => r . successful ) ) ;
1312+ assert ( results . every ( ( r ) => r . document === null ) ) ;
1313+
1314+ assert . strictEqual (
1315+ await collection . findOne ( { _id : r1 . insertedId ! } ) ,
1316+ null ,
1317+ ) ;
1318+ assert . strictEqual (
1319+ await collection . findOne ( { _id : r2 . insertedId ! } ) ,
1320+ null ,
1321+ ) ;
1322+ } ) ;
1323+
1324+ it ( 'should do nothing for unchanged documents' , async ( ) => {
1325+ const collection = pongoDb . collection < User > ( 'batchHandleCollection' ) ;
1326+
1327+ const doc : User = { name : 'Unchanged' , age : 5 } ;
1328+ const insertResult = await collection . insertOne ( doc ) ;
1329+ const id = insertResult . insertedId ! ;
1330+
1331+ const results = await collection . handle ( [ id ] , ( existing ) => existing ) ;
1332+
1333+ assert ( results [ 0 ] ?. successful ) ;
1334+
1335+ const found = await collection . findOne ( { _id : id } ) ;
1336+ assert . strictEqual ( found ?. _version , 1n ) ;
1337+ } ) ;
1338+
1339+ it ( 'should preserve result order matching input id order' , async ( ) => {
1340+ const collection = pongoDb . collection < User > ( 'batchHandleCollection' ) ;
1341+
1342+ const ids = [ uuid ( ) , uuid ( ) , uuid ( ) ] ;
1343+
1344+ const results = await collection . handle ( ids , ( _existing ) => ( {
1345+ name : 'Ordered' ,
1346+ age : 0 ,
1347+ } ) ) ;
1348+
1349+ assert . strictEqual ( results . length , 3 ) ;
1350+ for ( let i = 0 ; i < ids . length ; i ++ ) {
1351+ assert . strictEqual ( results [ i ] ?. document ?. _id , ids [ i ] ) ;
1352+ }
1353+ } ) ;
1354+
1355+ it ( 'should make changes when handler modifies existing documents' , async ( ) => {
1356+ const collection = pongoDb . collection < User > ( 'batchHandleCollection' ) ;
1357+
1358+ const doc : User = { name : 'Mutable' , age : 10 } ;
1359+ const insertResult = await collection . insertOne ( doc ) ;
1360+ const id = insertResult . insertedId ! ;
1361+
1362+ const results = await collection . handle ( [ id ] , ( existing ) => {
1363+ if ( ! existing ) return null ;
1364+ return { ...existing , name : 'Modified' , age : existing . age * 2 } ;
1365+ } ) ;
1366+
1367+ assert ( results [ 0 ] ?. successful ) ;
1368+ assert . strictEqual ( results [ 0 ] ?. document ?. name , 'Modified' ) ;
1369+ assert . strictEqual ( results [ 0 ] ?. document ?. age , 20 ) ;
1370+
1371+ const persisted = await collection . findOne ( { _id : id } ) ;
1372+ assert . strictEqual ( persisted ?. name , 'Modified' ) ;
1373+ assert . strictEqual ( persisted ?. age , 20 ) ;
1374+ assert . strictEqual ( persisted ?. _version , 2n ) ;
1375+ } ) ;
1376+ } ) ;
1377+
12471378 describe ( 'No filter' , ( ) => {
12481379 it ( 'should filter and count without filter specified' , async ( ) => {
12491380 const pongoCollection = pongoDb . collection < User > ( 'nofilter' ) ;
0 commit comments