Skip to content

Commit

Permalink
fix: ingest WC as a simple object or number for w value (#2695)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nbbeeken committed Jan 15, 2021
1 parent 00d3f2a commit f5f9fd4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/connection_string.ts
Expand Up @@ -992,9 +992,16 @@ export const OPTIONS = {
...value
}
});
} else if (value === 'majority' || typeof value === 'number') {
return WriteConcern.fromOptions({
writeConcern: {
...options.writeConcern,
w: value
}
});
}

throw new MongoParseError(`WriteConcern must be an object, got ${JSON.stringify(value)}`);
throw new MongoParseError(`Invalid WriteConcern cannot parse: ${JSON.stringify(value)}`);
}
} as OptionDescriptor,
wtimeout: {
Expand Down
2 changes: 1 addition & 1 deletion src/mongo_client.ts
Expand Up @@ -171,7 +171,7 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
/** Allow a driver to force a Single topology type with a connection string containing one host */
directConnection?: boolean;

/** The write concern */
/** The write concern w value */
w?: W;
/** The write concern timeout */
wtimeoutMS?: number;
Expand Down
37 changes: 37 additions & 0 deletions test/functional/write_concern.test.js
Expand Up @@ -7,6 +7,9 @@ const generateTopologyTests = require('./spec-runner').generateTopologyTests;
const loadSpecTests = require('../spec').loadSpecTests;
const { withMonitoredClient } = require('./shared');

const mock = require('../tools/mock');
const { MongoClient } = require('../../src');

describe('Write Concern', function () {
describe('spec tests', function () {
const testContext = new TestRunnerContext();
Expand Down Expand Up @@ -58,4 +61,38 @@ describe('Write Concern', function () {
withMonitoredClient('insert', { queryOptions: { journal: true } }, journalOptionTest)
);
});

let server;
before(() => {
return mock.createServer().then(s => {
server = s;
});
});

after(() => mock.cleanup());

it('should pipe writeConcern from client down to API call', function () {
server.setMessageHandler(request => {
if (request.document && request.document.ismaster) {
return request.reply(mock.DEFAULT_ISMASTER);
}
expect(request.document.writeConcern).to.exist;
expect(request.document.writeConcern.w).to.equal('majority');
return request.reply({ ok: 1 });
});

const uri = `mongodb://${server.uri()}`;
const client = new MongoClient(uri, { writeConcern: 'majority' });
return client
.connect()
.then(() => {
const db = client.db('wc_test');
const collection = db.collection('wc');

return collection.insertMany([{ a: 2 }]);
})
.then(() => {
return client.close();
});
});
});

0 comments on commit f5f9fd4

Please sign in to comment.