Skip to content

Commit 71a489f

Browse files
Copilothotlong
andcommitted
Fix MongoDB driver bulk operations and transaction support
- Fix updateMany to return object with modifiedCount property - Fix deleteMany to return object with deletedCount property - Add support for 'transaction' option key in CRUD operations for TCK compatibility - Maintain backward compatibility with 'session' option key Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 4ecbb67 commit 71a489f

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

packages/drivers/mongo/src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,10 @@ export class MongoDriver implements Driver {
420420
}
421421

422422
// Pass session for transactional operations only if it exists
423-
const result = options?.session
424-
? await collection.insertOne(mongoDoc, { session: options.session })
423+
// Support both 'transaction' and 'session' option keys for compatibility
424+
const session = options?.transaction || options?.session;
425+
const result = session
426+
? await collection.insertOne(mongoDoc, { session })
425427
: await collection.insertOne(mongoDoc);
426428

427429
// Return API format document (convert _id to id)
@@ -456,8 +458,10 @@ export class MongoDriver implements Driver {
456458

457459
// Use findOneAndUpdate to return the updated document
458460
const mongoOptions: FindOneAndUpdateOptions = { returnDocument: 'after' };
459-
if (options?.session) {
460-
mongoOptions.session = options.session;
461+
// Support both 'transaction' and 'session' option keys for compatibility
462+
const session = options?.transaction || options?.session;
463+
if (session) {
464+
mongoOptions.session = session;
461465
}
462466

463467
const result = await collection.findOneAndUpdate(
@@ -473,8 +477,10 @@ export class MongoDriver implements Driver {
473477
async delete(objectName: string, id: string | number, options?: any) {
474478
const collection = await this.getCollection(objectName);
475479
// Pass session for transactional operations only if it exists
476-
const result = options?.session
477-
? await collection.deleteOne({ _id: this.normalizeId(id) }, { session: options.session })
480+
// Support both 'transaction' and 'session' option keys for compatibility
481+
const session = options?.transaction || options?.session;
482+
const result = session
483+
? await collection.deleteOne({ _id: this.normalizeId(id) }, { session })
478484
: await collection.deleteOne({ _id: this.normalizeId(id) });
479485
return result.deletedCount;
480486
}
@@ -519,14 +525,14 @@ export class MongoDriver implements Driver {
519525
const update = isAtomic ? updateData : { $set: updateData };
520526

521527
const result = await collection.updateMany(filter, update);
522-
return result.modifiedCount;
528+
return { modifiedCount: result.modifiedCount };
523529
}
524530

525531
async deleteMany(objectName: string, filters: any, options?: any): Promise<any> {
526532
const collection = await this.getCollection(objectName);
527533
const filter = this.mapFilters(filters);
528534
const result = await collection.deleteMany(filter);
529-
return result.deletedCount;
535+
return { deletedCount: result.deletedCount };
530536
}
531537

532538
async aggregate(objectName: string, pipeline: any[], options?: any): Promise<any[]> {

0 commit comments

Comments
 (0)