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

Commit 01d4f8d

Browse files
authored
feat(COMPASS-4517): Expose connection options (#332)
* Return connection options from the connect method * Cover new return value with tests
1 parent c356eb1 commit 01d4f8d

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

lib/connect.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ const getStatusStateString = (evt) => {
114114
}
115115
};
116116

117+
const Tasks = {
118+
LoadSSLFiles: 'Load SSL files',
119+
CreateSSHTunnel: 'Create SSH Tunnel',
120+
ConnectToMongoDB: 'Connect to MongoDB'
121+
};
122+
117123
const getTasks = (model, setupListeners) => {
118124
const state = new EventEmitter();
119125
const tasks = {};
@@ -169,7 +175,7 @@ const getTasks = (model, setupListeners) => {
169175
* TODO (imlucas) dns.lookup() model.hostname and model.sshTunnelHostname to check for typos
170176
*/
171177
assign(tasks, {
172-
'Load SSL files': (cb) => {
178+
[Tasks.LoadSSLFiles]: (cb) => {
173179
const ctx = status('Load SSL files', cb);
174180

175181
if (!needToLoadSSLFiles(model)) {
@@ -183,7 +189,7 @@ const getTasks = (model, setupListeners) => {
183189
});
184190

185191
assign(tasks, {
186-
'Create SSH Tunnel': (cb) => {
192+
[Tasks.CreateSSHTunnel]: (cb) => {
187193
const ctx = status('Create SSH Tunnel', cb);
188194

189195
if (model.sshTunnel === 'NONE') {
@@ -195,7 +201,7 @@ const getTasks = (model, setupListeners) => {
195201
});
196202

197203
assign(tasks, {
198-
'Connect to MongoDB': (cb) => {
204+
[Tasks.ConnectToMongoDB]: (cb) => {
199205
const ctx = status('Connect to MongoDB');
200206

201207
// @note: Durran:
@@ -237,7 +243,7 @@ const getTasks = (model, setupListeners) => {
237243
});
238244
}
239245

240-
cb();
246+
cb(null, { url: model.driverUrlWithSsh, options: validOptions });
241247
});
242248
}
243249
});
@@ -303,7 +309,9 @@ const connect = (model, setupListeners, done) => {
303309

304310
logTaskStatus('Connecting...');
305311

306-
async.series(tasks, (err) => {
312+
async.series(tasks, (err, tasksArgs) => {
313+
const connectionOptions = tasksArgs[Tasks.ConnectToMongoDB];
314+
307315
if (err) {
308316
logTaskStatus('Error connecting:', err);
309317

@@ -312,7 +320,7 @@ const connect = (model, setupListeners, done) => {
312320

313321
logTaskStatus('Successfully connected');
314322

315-
return done(null, tasks.client);
323+
return done(null, tasks.client, connectionOptions);
316324
});
317325

318326
return tasks.state;

test/connect.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@ describe('connection model connector', () => {
2020
require('mongodb-runner/mocha/after')({ port: 27018, version: '4.0.0' })
2121
);
2222

23+
it('should return connection config when connected successfully', (done) => {
24+
Connection.from('mongodb://localhost:27018', (parseErr, model) => {
25+
if (parseErr) throw parseErr;
26+
27+
connect(
28+
model,
29+
setupListeners,
30+
(connectErr, client, { url, options }) => {
31+
if (connectErr) throw connectErr;
32+
33+
assert.strictEqual(
34+
url,
35+
'mongodb://localhost:27018/?readPreference=primary&ssl=false'
36+
);
37+
38+
assert.deepStrictEqual(options, {
39+
connectWithNoPrimary: true,
40+
readPreference: 'primary',
41+
useNewUrlParser: true,
42+
useUnifiedTopology: true
43+
});
44+
45+
client.close(true);
46+
done();
47+
}
48+
);
49+
});
50+
});
51+
2352
it('should connect to `localhost:27018 with model`', (done) => {
2453
Connection.from('mongodb://localhost:27018', (parseErr, model) => {
2554
assert.equal(parseErr, null);

0 commit comments

Comments
 (0)