Skip to content

Commit f5f9fd4

Browse files
authored
fix: ingest WC as a simple object or number for w value (#2695)
This reverts a change that now makes it possible to pass a writeConcern option that maps directly to 'majority' or a number for the w value NODE-2836
1 parent 00d3f2a commit f5f9fd4

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/connection_string.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,16 @@ export const OPTIONS = {
992992
...value
993993
}
994994
});
995+
} else if (value === 'majority' || typeof value === 'number') {
996+
return WriteConcern.fromOptions({
997+
writeConcern: {
998+
...options.writeConcern,
999+
w: value
1000+
}
1001+
});
9951002
}
9961003

997-
throw new MongoParseError(`WriteConcern must be an object, got ${JSON.stringify(value)}`);
1004+
throw new MongoParseError(`Invalid WriteConcern cannot parse: ${JSON.stringify(value)}`);
9981005
}
9991006
} as OptionDescriptor,
10001007
wtimeout: {

src/mongo_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
171171
/** Allow a driver to force a Single topology type with a connection string containing one host */
172172
directConnection?: boolean;
173173

174-
/** The write concern */
174+
/** The write concern w value */
175175
w?: W;
176176
/** The write concern timeout */
177177
wtimeoutMS?: number;

test/functional/write_concern.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const generateTopologyTests = require('./spec-runner').generateTopologyTests;
77
const loadSpecTests = require('../spec').loadSpecTests;
88
const { withMonitoredClient } = require('./shared');
99

10+
const mock = require('../tools/mock');
11+
const { MongoClient } = require('../../src');
12+
1013
describe('Write Concern', function () {
1114
describe('spec tests', function () {
1215
const testContext = new TestRunnerContext();
@@ -58,4 +61,38 @@ describe('Write Concern', function () {
5861
withMonitoredClient('insert', { queryOptions: { journal: true } }, journalOptionTest)
5962
);
6063
});
64+
65+
let server;
66+
before(() => {
67+
return mock.createServer().then(s => {
68+
server = s;
69+
});
70+
});
71+
72+
after(() => mock.cleanup());
73+
74+
it('should pipe writeConcern from client down to API call', function () {
75+
server.setMessageHandler(request => {
76+
if (request.document && request.document.ismaster) {
77+
return request.reply(mock.DEFAULT_ISMASTER);
78+
}
79+
expect(request.document.writeConcern).to.exist;
80+
expect(request.document.writeConcern.w).to.equal('majority');
81+
return request.reply({ ok: 1 });
82+
});
83+
84+
const uri = `mongodb://${server.uri()}`;
85+
const client = new MongoClient(uri, { writeConcern: 'majority' });
86+
return client
87+
.connect()
88+
.then(() => {
89+
const db = client.db('wc_test');
90+
const collection = db.collection('wc');
91+
92+
return collection.insertMany([{ a: 2 }]);
93+
})
94+
.then(() => {
95+
return client.close();
96+
});
97+
});
6198
});

0 commit comments

Comments
 (0)