Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit bbb576b

Browse files
authored
fix: set directConnection true on direct connections COMPASS-4534 (#337)
1 parent c011ba1 commit bbb576b

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

lib/connect.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ const getTasks = (model, setupListeners) => {
215215

216216
validOptions.useNewUrlParser = true;
217217
validOptions.useUnifiedTopology = true;
218+
if (
219+
model.directConnection === undefined &&
220+
model.hosts.length === 1 &&
221+
(model.replicaSet === undefined || model.replicaSet === '')
222+
) {
223+
// Previous to the node driver 3.6.3, directConnection was
224+
// set to true under these conditions. In 3.6.3 this defaulting
225+
// behavior was removed and now we add it. COMPASS-4534
226+
// https://github.com/mongodb/node-mongodb-native/commit/f8fd310a11a91db82f1c0ddc57482b8edabc231b
227+
validOptions.directConnection = true;
228+
}
218229

219230
const mongoClient = new MongoClient(model.driverUrlWithSsh, validOptions);
220231

lib/model.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ const CONNECTION_STRING_OPTIONS = {
169169
'pythonLegacy'
170170
],
171171
default: undefined
172-
}
172+
},
173+
directConnection: { type: 'boolean', default: undefined }
173174
};
174175

175176
assign(props, CONNECTION_STRING_OPTIONS);

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"async": "^3.1.0",
2626
"debug": "^4.1.1",
2727
"lodash": "^4.17.15",
28-
"mongodb": "^3.6.1",
28+
"mongodb": "^3.6.3",
2929
"raf": "^3.4.1",
3030
"ssh2": "^0.8.7",
3131
"storage-mixin": "^3.3.4"

test/connect.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('connection model connector', () => {
3737

3838
assert.deepStrictEqual(options, {
3939
connectWithNoPrimary: true,
40+
directConnection: true,
4041
readPreference: 'primary',
4142
useNewUrlParser: true,
4243
useUnifiedTopology: true

test/parse-uri-components.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,39 @@ describe('connection model partser should parse URI components such as', () => {
548548
}
549549
);
550550
});
551+
552+
it('defaults directConnection undefined', (done) => {
553+
Connection.from(
554+
'mongodb://localhost:27017',
555+
(error, result) => {
556+
expect(error).to.not.exist;
557+
expect(result.directConnection).to.be.equal(undefined);
558+
done();
559+
}
560+
);
561+
});
562+
563+
it('saves directConnection true', (done) => {
564+
Connection.from(
565+
'mongodb://localhost:27017/?directConnection=true',
566+
(error, result) => {
567+
expect(error).to.not.exist;
568+
expect(result.directConnection).to.be.equal(true);
569+
done();
570+
}
571+
);
572+
});
573+
574+
it('saves directConnection false', (done) => {
575+
Connection.from(
576+
'mongodb://localhost:27017/?directConnection=false',
577+
(error, result) => {
578+
expect(error).to.not.exist;
579+
expect(result.directConnection).to.be.equal(false);
580+
done();
581+
}
582+
);
583+
});
551584
});
552585

553586
describe('miscellaneous configuration', () => {

0 commit comments

Comments
 (0)