Skip to content

feat!: Options object precedence over URI options #2691

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 1 commit into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,12 @@ export function parseOptions(

for (const key of allKeys) {
const values = [];
if (urlOptions.has(key)) {
values.push(...urlOptions.get(key));
}
if (objectOptions.has(key)) {
values.push(objectOptions.get(key));
}
if (urlOptions.has(key)) {
values.push(...urlOptions.get(key));
}
if (DEFAULT_OPTIONS.has(key)) {
values.push(DEFAULT_OPTIONS.get(key));
}
Expand Down
6 changes: 6 additions & 0 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ const kOptions = Symbol('options');
* The **MongoClient** class is a class that allows for making Connections to MongoDB.
* @public
*
* @remarks
* The programmatically provided options take precedent over the URI options.
*
* @example
* ```js
* // Connect using a MongoClient instance
Expand Down Expand Up @@ -439,6 +442,9 @@ export class MongoClient extends EventEmitter {
/**
* Connect to MongoDB using a url
*
* @remarks
* The programmatically provided options take precedent over the URI options.
*
* @see https://docs.mongodb.org/manual/reference/connection-string/
*/
static connect(url: string): Promise<MongoClient>;
Expand Down
4 changes: 2 additions & 2 deletions test/unit/mongo_client_options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ describe('MongoOptions', function () {
expect(client.options).to.be.frozen;
});

it('test simple', function () {
it('programmatic options should override URI options', function () {
const options = parseOptions('mongodb://localhost:27017/test?directConnection=true', {
directConnection: false
});
expect(options.directConnection).to.be.true;
expect(options.directConnection).to.be.false;
expect(options.hosts).has.length(1);
expect(options.dbName).to.equal('test');
expect(options.prototype).to.not.exist;
Expand Down