Skip to content

Commit 99eb0df

Browse files
committed
deprecate top level write concern fields
1 parent a10171b commit 99eb0df

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

lib/db.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const legalOptionNames = [
7272
'wtimeout',
7373
'fsync',
7474
'j',
75+
'writeConcern',
7576
'readPreference',
7677
'readPreferenceTags',
7778
'native_parser',

lib/operations/connect.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Server = require('../topologies/server');
1414
const ServerSessionPool = require('../core').Sessions.ServerSessionPool;
1515
const emitDeprecationWarning = require('../utils').emitDeprecationWarning;
1616
const fs = require('fs');
17+
const WriteConcern = require('../write_concern');
1718
const BSON = require('../core/connection/utils').retrieveBSON();
1819
const CMAP_EVENT_NAMES = require('../cmap/events').CMAP_EVENT_NAMES;
1920

@@ -105,6 +106,7 @@ const validOptionNames = [
105106
'w',
106107
'wtimeout',
107108
'j',
109+
'writeConcern',
108110
'forceServerObjectId',
109111
'serializeFunctions',
110112
'ignoreUndefined',
@@ -300,9 +302,9 @@ function connect(mongoClient, url, options, callback) {
300302
delete _finalOptions.db_options.auth;
301303
}
302304

303-
// `journal` should be translated to `j` for the driver
304305
if (_finalOptions.journal != null) {
305-
_finalOptions.j = _finalOptions.journal;
306+
if (_finalOptions.writeConcern == null) _finalOptions.writeConcern = {};
307+
_finalOptions.writeConcern.j = _finalOptions.journal;
306308
_finalOptions.journal = undefined;
307309
}
308310

@@ -616,9 +618,12 @@ function createUnifiedOptions(finalOptions, options) {
616618
'mongos_options'
617619
];
618620
const noMerge = ['readconcern', 'compression', 'autoencryption'];
621+
const skip = ['w', 'wtimeout', 'j', 'fsync', 'writeConcern'];
619622

620623
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) {
622627
finalOptions[name] = options[name];
623628
} else if (childOptions.indexOf(name.toLowerCase()) !== -1) {
624629
finalOptions = mergeOptions(finalOptions, options[name], false);
@@ -636,6 +641,18 @@ function createUnifiedOptions(finalOptions, options) {
636641
}
637642
}
638643

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+
639656
return finalOptions;
640657
}
641658

@@ -760,12 +777,29 @@ function transformUrlOptions(_object) {
760777

761778
if (object.wTimeoutMS) {
762779
object.wtimeout = object.wTimeoutMS;
780+
object.wTimeoutMS = undefined;
781+
}
782+
783+
if (object.journal) {
784+
object.j = object.journal;
785+
object.journal = undefined;
763786
}
764787

765788
if (_object.srvHost) {
766789
object.srvHost = _object.srvHost;
767790
}
768791

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+
769803
return object;
770804
}
771805

lib/write_concern.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class WriteConcern {
6969
);
7070
}
7171

72+
console.warn(
73+
`Top-level use of w, wtimeout, j, and fsync is deprecated. Use writeConcern instead.`
74+
);
7275
return new WriteConcern(options.w, options.wtimeout, options.j, options.fsync);
7376
}
7477
}

test/functional/write_concern.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Write Concern', function() {
2626
function journalOptionTest(client, events, done) {
2727
expect(client).to.have.nested.property('s.options');
2828
const clientOptions = client.s.options;
29-
expect(clientOptions).to.containSubset({ j: true });
29+
expect(clientOptions).to.containSubset({ writeConcern: { j: true } });
3030
client
3131
.db('test')
3232
.collection('test')

0 commit comments

Comments
 (0)