Skip to content

Commit

Permalink
feat!: Options object precedence over URI options (#2691)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Jan 12, 2021
1 parent 350d14f commit 85d8d09
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
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

0 comments on commit 85d8d09

Please sign in to comment.