Skip to content

fix: ingest WC as a simple object or number for w value #2695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/connection_string.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
39 changes: 39 additions & 0 deletions test/functional/write_concern.test.js
Original file line number Diff line number Diff line change
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,40 @@ describe('Write Concern', function () {
withMonitoredClient('insert', { queryOptions: { journal: true } }, journalOptionTest)
);
});

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

after(() => {
mock.cleanup();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also return (or just remove the curly braces)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooo nice catch thanks!!

});

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();
});
});
});