11import type {
22 Collection , Db , MongoClientOptions , ClientSession , BulkWriteResult , Filter , UpdateFilter , OptionalUnlessRequiredId , UpdateResult ,
3- DeleteResult , InsertManyResult , InsertOneResult ,
3+ DeleteResult , InsertManyResult , InsertOneResult , TransactionOptions ,
44} from 'mongodb' ;
55import { MongoClient } from 'mongodb' ;
66import { ObjectId } from 'bson' ;
@@ -199,7 +199,7 @@ export class MongoConnection extends Connection {
199199 return this . runQuery < T , number > ( 'countDocuments' , collection , undefined , where , ctx ) ;
200200 }
201201
202- async transactional < T > ( cb : ( trx : Transaction < ClientSession > ) => Promise < T > , options : { isolationLevel ?: IsolationLevel ; ctx ?: Transaction < ClientSession > ; eventBroadcaster ?: TransactionEventBroadcaster } = { } ) : Promise < T > {
202+ async transactional < T > ( cb : ( trx : Transaction < ClientSession > ) => Promise < T > , options : { isolationLevel ?: IsolationLevel ; ctx ?: Transaction < ClientSession > ; eventBroadcaster ?: TransactionEventBroadcaster } & TransactionOptions = { } ) : Promise < T > {
203203 const session = await this . begin ( options ) ;
204204
205205 try {
@@ -215,15 +215,16 @@ export class MongoConnection extends Connection {
215215 }
216216 }
217217
218- async begin ( options : { isolationLevel ?: IsolationLevel ; ctx ?: ClientSession ; eventBroadcaster ?: TransactionEventBroadcaster } = { } ) : Promise < ClientSession > {
219- if ( ! options . ctx ) {
220- await options . eventBroadcaster ?. dispatchEvent ( EventType . beforeTransactionStart ) ;
221- }
218+ async begin ( options : { isolationLevel ?: IsolationLevel ; ctx ?: ClientSession ; eventBroadcaster ?: TransactionEventBroadcaster } & TransactionOptions = { } ) : Promise < ClientSession > {
219+ const { ctx, isolationLevel, eventBroadcaster, ...txOptions } = options ;
222220
223- const session = options . ctx || this . client . startSession ( ) ;
224- session . startTransaction ( ) ;
221+ if ( ! ctx ) {
222+ await eventBroadcaster ?. dispatchEvent ( EventType . beforeTransactionStart ) ;
223+ }
224+ const session = ctx || this . client . startSession ( ) ;
225+ session . startTransaction ( txOptions ) ;
225226 this . logQuery ( 'db.begin();' ) ;
226- await options . eventBroadcaster ?. dispatchEvent ( EventType . afterTransactionStart , session ) ;
227+ await eventBroadcaster ?. dispatchEvent ( EventType . afterTransactionStart , session ) ;
227228
228229 return session ;
229230 }
0 commit comments