Skip to content

Commit

Permalink
Merge 1ee700f into 44f1270
Browse files Browse the repository at this point in the history
  • Loading branch information
okv committed May 22, 2020
2 parents 44f1270 + 1ee700f commit 1df7fa4
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 11 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ Defaults to `60 * 1000`. Note that current implementation uses on mongodb ttl in
Defaults to `false`.

* **errorHandler**: function -- function that will be called if error happened
during incr, decrement or resetKey methods. Defaults to `_.noop`
during incr, decrement or resetKey methods. Defaults to `_.noop`.

* **createTtlIndex**: boolean -- defines whether create ttl index (
on `expirationDate` field with `expireAfterSeconds: 0`) on collection
or not. Could be useful in situations when you don't want to create index
from the app e.g. due to restricted db permissions (see #15 for details).
Defaults to `true`.


## Methods
Expand Down
20 changes: 13 additions & 7 deletions lib/mongoStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var MongoStore = function(options) {
collectionName: 'expressRateRecords',
expireTimeMs: 60000,
resetExpireDateOnChange: false,
errorHandler: _.noop
errorHandler: _.noop,
createTtlIndex: true
}
);

Expand All @@ -33,7 +34,8 @@ var MongoStore = function(options) {

_(this).extend(
_(allOptions).pick(
'collection', 'expireTimeMs', 'resetExpireDateOnChange', 'errorHandler'
'collection', 'expireTimeMs', 'resetExpireDateOnChange',
'errorHandler', 'createTtlIndex'
)
);

Expand Down Expand Up @@ -99,11 +101,15 @@ MongoStore.prototype._getCollection = function(callback) {
}
},
function() {
self.collection.createIndex(
{expirationDate: 1},
{expireAfterSeconds: 0},
this.slot()
);
if (self.createTtlIndex) {
self.collection.createIndex(
{expirationDate: 1},
{expireAfterSeconds: 0},
this.slot()
);
} else {
this.pass(null);
}
},
function(err) {
self._collectionState = err ? 'notPrepared' : 'prepared';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var describeTitle = 'MongoStore.prototype._getCollection ' +
describe(describeTitle, function() {
var testData = {
mongoStoreContext: {
_collectionState: 'notPrepared'
_collectionState: 'notPrepared',
createTtlIndex: true
},
collectionCreateIndexError: new Error('test createIndex error')
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var describeTitle = 'MongoStore.prototype._getCollection ' +
describe(describeTitle, function() {
var testData = {
mongoStoreContext: {
_collectionState: 'notPrepared'
_collectionState: 'notPrepared',
createTtlIndex: true
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';

var expect = require('expect.js');
var Steppy = require('twostep').Steppy;
var rewire = require('rewire');
var _ = require('underscore');
var testUtils = require('./utils');

var MongoStore = rewire('../../../lib/mongoStore');

var describeTitle = 'MongoStore.prototype._getCollection ' +
'with "notPrepared" collectionState and disabled create ttl index option';
describe(describeTitle, function() {
var testData = {
mongoStoreContext: {
_collectionState: 'notPrepared',
createTtlIndex: false
}
};

var mocks = testUtils.getMocks();

var revertMocks;

before(function() {
revertMocks = MongoStore.__set__(
_(mocks).omit('_dynamic')
);
});

it('should return collection', function(done) {
Steppy(
function() {
MongoStore.prototype._getCollection.call(
_({}).extend(
testData.mongoStoreContext,
mocks._dynamic.mongoStoreContext
),
this.slot()
);
},
function(err, collection) {
expect(collection).eql(
mocks._dynamic.collection
);

this.pass(null);
},
done
);
});

it('setImmediate should not be called', function() {
expect(mocks.setImmediate.callCount).eql(0);
});

it('self._getCollection should not be called', function() {
expect(
mocks._dynamic.mongoStoreContext._getCollection.callCount
).eql(0);
});

it('_createCollection should be called', function() {
expect(
mocks._dynamic.mongoStoreContext._createCollection.callCount
).eql(1);

var createCollectionArgs = mocks._dynamic.mongoStoreContext
._createCollection.args[0];

expect(createCollectionArgs).length(1);
expect(createCollectionArgs[0]).a('function');
});

it(
'collection.createIndex should not be called',
function() {
expect(mocks._dynamic.collection.createIndex.callCount).eql(0);
}
);

after(function() {
revertMocks();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var describeTitle = 'MongoStore.prototype._getCollection ' +
describe(describeTitle, function() {
var testData = {
mongoStoreContext: {
_collectionState: 'notPrepared'
_collectionState: 'notPrepared',
createTtlIndex: true
}
};

Expand Down
1 change: 1 addition & 0 deletions test/mongoStore/constructor/withSuitableParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ describe('MongoStore with suitable params', function() {
expect(mongoStore.expireTimeMs).eql(60000);
expect(mongoStore.resetExpireDateOnChange).eql(false);
expect(mongoStore.errorHandler).eql(_.noop);
expect(mongoStore.createTtlIndex).equal(true);
});
});

0 comments on commit 1df7fa4

Please sign in to comment.