@@ -21,6 +21,8 @@ import {
21
21
} from './decorators' ;
22
22
import {
23
23
ChangeStreamOptions ,
24
+ ClientSessionOptions ,
25
+ CommandOperationOptions ,
24
26
Document ,
25
27
generateUri ,
26
28
ListDatabasesOptions ,
@@ -451,22 +453,37 @@ export default class Mongo extends ShellApiClass {
451
453
452
454
@topologies ( [ Topologies . ReplSet ] )
453
455
startSession ( options : Document = { } ) : Session {
454
- const driverOptions = { } ;
455
- if ( options === undefined ) {
456
- return new Session ( this , driverOptions , this . _serviceProvider . startSession ( driverOptions ) ) ;
456
+ const allTransactionOptions = [
457
+ 'readConcern' , 'writeConcern' , 'readPreference' , 'maxCommitTimeMS'
458
+ ] as const ;
459
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
460
+ function assertAllTransactionOptionsUsed ( _options : ( typeof allTransactionOptions ) [ number ] ) {
461
+ // These typechecks might look weird, but will tell us if we are missing
462
+ // support for a newly introduced driver option when it is being added
463
+ // to the driver API.
464
+ }
465
+ assertAllTransactionOptionsUsed ( '' as Exclude < keyof TransactionOptions , keyof CommandOperationOptions > ) ;
466
+ const defaultTransactionOptions : TransactionOptions = { } ;
467
+ for ( const key of allTransactionOptions ) {
468
+ if ( typeof options [ key ] !== 'undefined' ) {
469
+ defaultTransactionOptions [ key ] = options [ key ] ;
470
+ }
471
+ }
472
+
473
+ const allSessionOptions = [ 'causalConsistency' , 'snapshot' ] as const ;
474
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
475
+ function assertAllSessionOptionsUsed ( _options : ( typeof allSessionOptions ) [ number ] | 'defaultTransactionOptions' ) { }
476
+ assertAllSessionOptionsUsed ( '' as keyof ClientSessionOptions ) ;
477
+ const driverOptions : ClientSessionOptions = { } ;
478
+ if ( Object . keys ( defaultTransactionOptions ) . length > 0 ) {
479
+ driverOptions . defaultTransactionOptions = defaultTransactionOptions ;
480
+ }
481
+ for ( const key of allSessionOptions ) {
482
+ if ( typeof options [ key ] !== 'undefined' ) {
483
+ driverOptions [ key ] = options [ key ] ;
484
+ }
457
485
}
458
- const defaultTransactionOptions = { } as TransactionOptions ;
459
486
460
- // Only include option if not undef
461
- Object . assign ( defaultTransactionOptions ,
462
- options . readConcern && { readConcern : options . readConcern } ,
463
- options . writeConcern && { writeConcern : options . writeConcern } ,
464
- options . readPreference && { readPreference : options . readPreference }
465
- ) ;
466
- Object . assign ( driverOptions ,
467
- Object . keys ( defaultTransactionOptions ) . length > 0 && { defaultTransactionOptions : defaultTransactionOptions } ,
468
- options . causalConsistency !== undefined && { causalConsistency : options . causalConsistency }
469
- ) ;
470
487
return new Session ( this , driverOptions , this . _serviceProvider . startSession ( driverOptions ) ) ;
471
488
}
472
489
0 commit comments