Skip to content

Commit 85d8d09

Browse files
authored
feat!: Options object precedence over URI options (#2691)
1 parent 350d14f commit 85d8d09

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/connection_string.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,12 @@ export function parseOptions(
344344

345345
for (const key of allKeys) {
346346
const values = [];
347-
if (urlOptions.has(key)) {
348-
values.push(...urlOptions.get(key));
349-
}
350347
if (objectOptions.has(key)) {
351348
values.push(objectOptions.get(key));
352349
}
350+
if (urlOptions.has(key)) {
351+
values.push(...urlOptions.get(key));
352+
}
353353
if (DEFAULT_OPTIONS.has(key)) {
354354
values.push(DEFAULT_OPTIONS.get(key));
355355
}

src/mongo_client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ const kOptions = Symbol('options');
242242
* The **MongoClient** class is a class that allows for making Connections to MongoDB.
243243
* @public
244244
*
245+
* @remarks
246+
* The programmatically provided options take precedent over the URI options.
247+
*
245248
* @example
246249
* ```js
247250
* // Connect using a MongoClient instance
@@ -439,6 +442,9 @@ export class MongoClient extends EventEmitter {
439442
/**
440443
* Connect to MongoDB using a url
441444
*
445+
* @remarks
446+
* The programmatically provided options take precedent over the URI options.
447+
*
442448
* @see https://docs.mongodb.org/manual/reference/connection-string/
443449
*/
444450
static connect(url: string): Promise<MongoClient>;

test/unit/mongo_client_options.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ describe('MongoOptions', function () {
1616
expect(client.options).to.be.frozen;
1717
});
1818

19-
it('test simple', function () {
19+
it('programmatic options should override URI options', function () {
2020
const options = parseOptions('mongodb://localhost:27017/test?directConnection=true', {
2121
directConnection: false
2222
});
23-
expect(options.directConnection).to.be.true;
23+
expect(options.directConnection).to.be.false;
2424
expect(options.hosts).has.length(1);
2525
expect(options.dbName).to.equal('test');
2626
expect(options.prototype).to.not.exist;

0 commit comments

Comments
 (0)