@@ -14,6 +14,7 @@ const Server = require('../topologies/server');
14
14
const ServerSessionPool = require ( '../core' ) . Sessions . ServerSessionPool ;
15
15
const emitDeprecationWarning = require ( '../utils' ) . emitDeprecationWarning ;
16
16
const fs = require ( 'fs' ) ;
17
+ const WriteConcern = require ( '../write_concern' ) ;
17
18
const BSON = require ( '../core/connection/utils' ) . retrieveBSON ( ) ;
18
19
const CMAP_EVENT_NAMES = require ( '../cmap/events' ) . CMAP_EVENT_NAMES ;
19
20
@@ -105,6 +106,7 @@ const validOptionNames = [
105
106
'w' ,
106
107
'wtimeout' ,
107
108
'j' ,
109
+ 'writeConcern' ,
108
110
'forceServerObjectId' ,
109
111
'serializeFunctions' ,
110
112
'ignoreUndefined' ,
@@ -300,9 +302,9 @@ function connect(mongoClient, url, options, callback) {
300
302
delete _finalOptions . db_options . auth ;
301
303
}
302
304
303
- // `journal` should be translated to `j` for the driver
304
305
if ( _finalOptions . journal != null ) {
305
- _finalOptions . j = _finalOptions . journal ;
306
+ if ( _finalOptions . writeConcern == null ) _finalOptions . writeConcern = { } ;
307
+ _finalOptions . writeConcern . j = _finalOptions . journal ;
306
308
_finalOptions . journal = undefined ;
307
309
}
308
310
@@ -616,9 +618,12 @@ function createUnifiedOptions(finalOptions, options) {
616
618
'mongos_options'
617
619
] ;
618
620
const noMerge = [ 'readconcern' , 'compression' , 'autoencryption' ] ;
621
+ const skip = [ 'w' , 'wtimeout' , 'j' , 'fsync' , 'writeConcern' ] ;
619
622
620
623
for ( const name in options ) {
621
- if ( noMerge . indexOf ( name . toLowerCase ( ) ) !== - 1 ) {
624
+ if ( skip . indexOf ( name . toLowerCase ( ) ) !== - 1 ) {
625
+ continue ;
626
+ } else if ( noMerge . indexOf ( name . toLowerCase ( ) ) !== - 1 ) {
622
627
finalOptions [ name ] = options [ name ] ;
623
628
} else if ( childOptions . indexOf ( name . toLowerCase ( ) ) !== - 1 ) {
624
629
finalOptions = mergeOptions ( finalOptions , options [ name ] , false ) ;
@@ -636,6 +641,18 @@ function createUnifiedOptions(finalOptions, options) {
636
641
}
637
642
}
638
643
644
+ // Handle write concern keys separately, since `options` may have the keys at the top level or
645
+ // under `options.writeConcern`. The final merged keys will be under `finalOptions.writeConcern`.
646
+ // `fromOptions` will warn if `options` is using deprecated write concern options
647
+ const optionsWriteConcern = WriteConcern . fromOptions ( options ) ;
648
+ if ( optionsWriteConcern ) {
649
+ finalOptions . writeConcern = Object . assign (
650
+ { } ,
651
+ finalOptions . writeConcern ,
652
+ WriteConcern . fromOptions ( options )
653
+ ) ;
654
+ }
655
+
639
656
return finalOptions ;
640
657
}
641
658
@@ -760,12 +777,29 @@ function transformUrlOptions(_object) {
760
777
761
778
if ( object . wTimeoutMS ) {
762
779
object . wtimeout = object . wTimeoutMS ;
780
+ object . wTimeoutMS = undefined ;
781
+ }
782
+
783
+ if ( object . journal ) {
784
+ object . j = object . journal ;
785
+ object . journal = undefined ;
763
786
}
764
787
765
788
if ( _object . srvHost ) {
766
789
object . srvHost = _object . srvHost ;
767
790
}
768
791
792
+ // Any write concern options from the URL will be top-level, so we manually
793
+ // move them options under `object.writeConcern`
794
+ const wcKeys = [ 'w' , 'wtimeout' , 'j' , 'fsync' ] ;
795
+ for ( const key of wcKeys ) {
796
+ if ( object [ key ] !== undefined ) {
797
+ if ( object . writeConcern === undefined ) object . writeConcern = { } ;
798
+ object . writeConcern [ key ] = object [ key ] ;
799
+ object [ key ] = undefined ;
800
+ }
801
+ }
802
+
769
803
return object ;
770
804
}
771
805
0 commit comments