Skip to content

Commit 97f6986

Browse files
authored
Merge pull request #23 from 2do2go/feature/support-connection-options
Add connection options support
2 parents 9ca6468 + 43e83b9 commit 97f6986

File tree

5 files changed

+120
-13
lines changed

5 files changed

+120
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Required if collection hasn't been set.
4848

4949
* **collection**: object -- mongodb collection instance. Required if uri hasn't been set.
5050

51+
* **connectionOptions**: object -- mongodb connection options. Allows to pass additional connection options to mongodb. The default connection options are `useUnifiedTopology: true`, `useNewUrlParser: true`.
52+
5153
* **expireTimeMs**: integer -- time period, in milliseconds, after which record will be reset (deleted).
5254
Defaults to `60 * 1000`. Notice that current implementation uses on mongodb ttl indexes - background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task. See [mongodb ttl indexes doc](https://docs.mongodb.com/v3.6/core/index-ttl/#timing-of-the-delete-operation) for more information.
5355

lib/mongoStore.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var MongoStore = function(options) {
3535
_(this).extend(
3636
_(allOptions).pick(
3737
'collection', 'expireTimeMs', 'resetExpireDateOnChange',
38-
'errorHandler', 'createTtlIndex'
38+
'errorHandler', 'createTtlIndex', 'connectionOptions'
3939
)
4040
);
4141

@@ -46,24 +46,27 @@ MongoStore.prototype._createCollection = function(callback) {
4646
var self = this;
4747
Steppy(
4848
function() {
49-
var connectOptions = {
50-
useNewUrlParser: true,
51-
useUnifiedTopology: true
52-
};
49+
var connectionOptions = _({}).defaults(
50+
self.connectionOptions,
51+
{
52+
useNewUrlParser: true,
53+
useUnifiedTopology: true
54+
}
55+
);
5356

5457
if (self.dbOptions.user && self.dbOptions.password) {
5558
var dbName = _(self.dbOptions.uri.split('/')).last();
5659

57-
connectOptions.authSource = self.dbOptions.authSource || dbName;
58-
connectOptions.auth = {
60+
connectionOptions.authSource = self.dbOptions.authSource || dbName;
61+
connectionOptions.auth = {
5962
user: self.dbOptions.user,
6063
password: self.dbOptions.password
6164
};
6265
}
6366

6467
MongoClient.connect(
6568
self.dbOptions.uri,
66-
connectOptions,
69+
connectionOptions,
6770
this.slot()
6871
);
6972
},

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
"mongodb"
2727
],
2828
"dependencies": {
29+
"mongodb": ">= 3.3.4 < 4.0.0",
2930
"twostep": "0.4.2",
30-
"underscore": "1.9.1",
31-
"mongodb": ">= 3.3.4 < 4.0.0"
31+
"underscore": "1.11.0"
3232
},
3333
"devDependencies": {
3434
"coveralls": "3.1.0",
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict';
2+
3+
var expect = require('expect.js');
4+
var Steppy = require('twostep').Steppy;
5+
var rewire = require('rewire');
6+
var _ = require('underscore');
7+
var testUtils = require('./utils');
8+
9+
var MongoStore = rewire('../../../lib/mongoStore');
10+
11+
var describeTitle = 'MongoClient.prototype._createCollection ' +
12+
'with connection options';
13+
describe(describeTitle, function() {
14+
var testData = testUtils.getTestData();
15+
testData.mongoStoreContext.dbOptions.user = 'testUser';
16+
testData.mongoStoreContext.dbOptions.password = 'testPassword';
17+
testData.mongoStoreContext.dbOptions.authSource = 'testAuthSource';
18+
testData.mongoStoreContext.connectionOptions = {
19+
useUnifiedTopology: false,
20+
ssl: true,
21+
sslCA: Buffer.alloc(5)
22+
};
23+
24+
var mocks = testUtils.getMocks(testData);
25+
26+
var revertMocks;
27+
28+
before(function() {
29+
revertMocks = MongoStore.__set__(
30+
_(mocks).omit('_dynamic')
31+
);
32+
});
33+
34+
it('should set collection to mongoStore context', function(done) {
35+
Steppy(
36+
function() {
37+
MongoStore.prototype._createCollection.call(
38+
testData.mongoStoreContext,
39+
this.slot()
40+
);
41+
},
42+
function() {
43+
expect(testData.mongoStoreContext).keys('collection');
44+
expect(
45+
testData.mongoStoreContext.collection
46+
).eql(testData.db.collectionResult);
47+
48+
this.pass(null);
49+
},
50+
done
51+
);
52+
});
53+
54+
it('MongoClient.connect should be called with uri and options', function() {
55+
expect(mocks.MongoClient.connect.callCount).eql(1);
56+
57+
var MongoClientConnectArgs = mocks.MongoClient.connect.args[0];
58+
59+
expect(
60+
_(MongoClientConnectArgs).initial()
61+
).eql([
62+
testData.mongoStoreContext.dbOptions.uri,
63+
{
64+
useNewUrlParser: true,
65+
useUnifiedTopology: false,
66+
authSource: testData.mongoStoreContext.dbOptions.authSource,
67+
auth: {
68+
user: testData.mongoStoreContext.dbOptions.user,
69+
password: testData.mongoStoreContext.dbOptions.password
70+
},
71+
ssl: testData.mongoStoreContext.connectionOptions.ssl,
72+
sslCA: testData.mongoStoreContext.connectionOptions.sslCA
73+
}
74+
]);
75+
76+
expect(
77+
_(MongoClientConnectArgs).last()
78+
).a('function');
79+
});
80+
81+
it('client.db should be called', function() {
82+
expect(mocks._dynamic.client.db.callCount).eql(1);
83+
84+
var clientDbArgs = mocks._dynamic.client.db.args[0];
85+
86+
expect(clientDbArgs).eql([]);
87+
});
88+
89+
it('db.collection should be called with collection name', function() {
90+
expect(mocks._dynamic.db.collection.callCount).eql(1);
91+
92+
var dbCollectionArgs = mocks._dynamic.db.collection.args[0];
93+
94+
expect(dbCollectionArgs).eql([
95+
testData.mongoStoreContext.dbOptions.collectionName
96+
]);
97+
});
98+
99+
after(function() {
100+
revertMocks();
101+
});
102+
});

0 commit comments

Comments
 (0)