Skip to content

Commit 617d9de

Browse files
authored
fix: restore auto direct connection behavior (#2719)
This was removed prematurely in the 3.6.3 release. NODE-2966
1 parent 8082c89 commit 617d9de

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

lib/core/uri_parser.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,15 @@ function parseConnectionString(uri, options, callback) {
694694
return callback(new MongoParseError('directConnection option requires exactly one host'));
695695
}
696696

697+
// NOTE: this behavior will go away in v4.0, we will always auto discover there
698+
if (
699+
parsedOptions.directConnection == null &&
700+
hosts.length === 1 &&
701+
parsedOptions.replicaSet == null
702+
) {
703+
parsedOptions.directConnection = true;
704+
}
705+
697706
const result = {
698707
hosts: hosts,
699708
auth: auth.db || auth.username ? auth : null,

test/functional/bulk.test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,9 +1687,10 @@ describe('Bulk', function() {
16871687
);
16881688
}
16891689
})
1690-
.finally(() => {
1691-
return client.close();
1692-
});
1690+
.then(
1691+
() => client.close(),
1692+
() => client.close()
1693+
);
16931694
});
16941695

16951696
it('should respect toBSON conversion when checking for atomic operators', function() {
@@ -1721,8 +1722,9 @@ describe('Bulk', function() {
17211722
expect.fail(); // shouldn't throw any error
17221723
}
17231724
})
1724-
.finally(() => {
1725-
return client.close();
1726-
});
1725+
.then(
1726+
() => client.close(),
1727+
() => client.close()
1728+
);
17271729
});
17281730
});

test/functional/mongo_client_options.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ describe('MongoClient Options', function() {
168168
}
169169
});
170170

171+
it('should directConnect when no replicaSet name is specified', {
172+
metadata: { requires: { topology: 'replicaset' } },
173+
test: function(done) {
174+
const urlNoReplicaSet = this.configuration
175+
.url()
176+
// strip the replicaSet parameter from the url if present
177+
.replace(/([&?])replicaSet=.+?[&$]/, '$1')
178+
// reduce down to a single host if multiple are provided
179+
.replace(new RegExp('(^mongodb://[^,]+)[^/]+'), '$1');
180+
const client = this.configuration.newClient(urlNoReplicaSet);
181+
client.connect(err => {
182+
expect(err).to.not.exist;
183+
expect(client.s.options.directConnection).to.be.true;
184+
client.close(done);
185+
});
186+
}
187+
});
188+
171189
/**
172190
* @ignore
173191
*/

test/functional/sharding_connection.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ describe('Sharding (Connection)', function() {
1313
it('Should use sharded topology', {
1414
metadata: { requires: { topology: 'sharded' } },
1515
test: function() {
16-
const client = this.configuration.newClient({}, { useUnifiedTopology: true });
16+
const client = this.configuration.newClient(
17+
{},
18+
// note: auto-discovery will be the default behavior in 4.0,
19+
// for now we must supply directConnection: false
20+
{ useUnifiedTopology: true, directConnection: false }
21+
);
1722
return withClient(client, (client, done) => {
1823
expect(client).to.exist;
1924
expect(client)

0 commit comments

Comments
 (0)