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

Commit 29fa8fc

Browse files
authored
feat: Expose connection params of the native client through the data service COMPASS-4517 (#277)
* feat(COMPASS-4517): Expose connection params of the native client through the data service * refactor: Change the name from connectionParams to connectionOptions for consistency
1 parent a203094 commit 29fa8fc

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

lib/data-service.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class DataService extends EventEmitter {
2727
.on('topologyDescriptionChanged', (evt) => this.emit('topologyDescriptionChanged', evt));
2828
}
2929

30+
getConnectionOptions() {
31+
return this.client.connectionOptions;
32+
}
33+
3034
/**
3135
* Get the kitchen sink information about a collection.
3236
*

lib/native-client.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class NativeClient extends EventEmitter {
8888
constructor(model) {
8989
super();
9090
this.model = model;
91+
this.connectionOptions = null;
9192
}
9293

9394
/**
@@ -98,25 +99,34 @@ class NativeClient extends EventEmitter {
9899
*/
99100
connect(done) {
100101
debug('connecting...');
102+
103+
this.connectionOptions = null;
101104
this.isWritable = false;
102105
this.isMongos = false;
103-
connect(this.model, this.setupListeners.bind(this), err => {
104-
if (err) {
105-
return done(this._translateMessage(err));
106-
}
107106

108-
this.isWritable = this.client.isWritable;
109-
this.isMongos = this.client.isMongos;
107+
connect(
108+
this.model,
109+
this.setupListeners.bind(this),
110+
(err, _client, connectionOptions) => {
111+
if (err) {
112+
return done(this._translateMessage(err));
113+
}
110114

111-
debug('connected!', {
112-
isWritable: this.isWritable,
113-
isMongos: this.isMongos
114-
});
115+
this.connectionOptions = connectionOptions;
115116

116-
this.client.on('status', evt => this.emit('status', evt));
117-
this.database = this.client.db(this.model.ns || ADMIN);
118-
done(null, this);
119-
});
117+
this.isWritable = this.client.isWritable;
118+
this.isMongos = this.client.isMongos;
119+
120+
debug('connected!', {
121+
isWritable: this.isWritable,
122+
isMongos: this.isMongos
123+
});
124+
125+
this.client.on('status', (evt) => this.emit('status', evt));
126+
this.database = this.client.db(this.model.ns || ADMIN);
127+
done(null, this);
128+
}
129+
);
120130
return this;
121131
}
122132

test/native-client.test.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,23 @@ describe('NativeClient', function() {
4040

4141
/*
4242
* pretends to be a connection-model providing every function call
43-
* required in NativeClient#connect, but returns topology of our choice
43+
* required in NativeClient#connect, but returns topology and connection
44+
* params of our choice
4445
*/
45-
function mockedConnectionModel(topologyDescription) {
46+
function mockedConnectionModel(topologyDescription, connectionOptions) {
4647
const _topologyDescription =
4748
topologyDescription || mockedTopologyDescription();
4849

50+
const _connectionOptions = connectionOptions || {
51+
url: 'mongodb://127.0.0.1:27018/data-service?readPreference=primary&ssl=false',
52+
options: {
53+
connectWithNoPrimary: true,
54+
readPreference: 'primary',
55+
useNewUrlParser: true,
56+
useUnifiedTopology: true
57+
}
58+
};
59+
4960
return {
5061
connect(_model, setupListeners, cb) {
5162
const mockedClient = new EventEmitter();
@@ -54,7 +65,7 @@ describe('NativeClient', function() {
5465
mockedClient.emit('topologyDescriptionChanged', {
5566
newDescription: _topologyDescription
5667
});
57-
cb(null, mockedClient);
68+
cb(null, mockedClient, _connectionOptions);
5869
}
5970
};
6071
}
@@ -63,6 +74,31 @@ describe('NativeClient', function() {
6374
mock.stop('mongodb-connection-model');
6475
});
6576

77+
it('sets .connectionOptions after successful connection', function(done) {
78+
mock(
79+
'mongodb-connection-model',
80+
mockedConnectionModel()
81+
);
82+
83+
var MockedNativeClient = mock.reRequire('../lib/native-client');
84+
var mockedClient = new MockedNativeClient(helper.connection);
85+
86+
expect(mockedClient.connectionOptions).to.be.null;
87+
88+
mockedClient.connect(function() {
89+
expect(mockedClient.connectionOptions).to.deep.equal({
90+
url: 'mongodb://127.0.0.1:27018/data-service?readPreference=primary&ssl=false',
91+
options: {
92+
connectWithNoPrimary: true,
93+
readPreference: 'primary',
94+
useNewUrlParser: true,
95+
useUnifiedTopology: true
96+
}
97+
});
98+
done();
99+
});
100+
});
101+
66102
it('sets .isMongos to true when topology is sharded', function(done) {
67103
mock(
68104
'mongodb-connection-model',

0 commit comments

Comments
 (0)