Skip to content

Commit

Permalink
Merge pull request #30 from joetinoco/clear-added
Browse files Browse the repository at this point in the history
Added 'clear()' to session store
  • Loading branch information
vkarpov15 committed Oct 7, 2016
2 parents 1b093fe + f64e73f commit 9a64af8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ module will manage the internal connection state for you.

```

**Optional:** To clear all sessions from the store, use `store.clear(callback);`.
The callback should be called as `callback(error)`.

#### It can store sessions for latest Express 3.x

If you're using Express 3.x, you need to pass the Express module itself
Expand Down
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ module.exports = function(connect) {
});
};

MongoDBStore.prototype.clear = function(callback) {
var _this = this;
if (!this.db) {
return this._emitter.once('connected', function() {
_this.clear.call(_this, callback);
});
}

this.db.collection(this.options.collection).
remove({}, function(error) {
if (error) {
var e = new Error('Error clearing all sessions: ' + error.message);
return _this._errorHandler(e, callback);
}
callback && callback();
});
};

MongoDBStore.prototype.set = function(id, session, callback) {
var _this = this;

Expand Down
9 changes: 8 additions & 1 deletion test/examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ describe('MongoDBStore', function() {
};
request(config, function(error, response, body) {
assert.ok(!response.headers['set-cookie']);
done();
store.clear(function(error) {
assert.ifError(error);
underlyingDb.collection('mySessions').count({}, function(error, count) {
assert.ifError(error);
assert.equal(0, count);
done();
});
});
});
});
});
Expand Down
29 changes: 29 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,33 @@ describe('connectMongoDBSession', function() {
});
});
});

describe('clear()', function(done){
var numIndexCalls;

beforeEach(function() {
numIndexCalls = 0;

db.ensureIndex.on('called', function(args) {
assert.equal(++numIndexCalls, 1);
assert.equal(args.index.expires, 1);
args.callback();
});
});

it('clears the session store', function(done) {
var SessionStore = connectMongoDBSession({ Store: StoreStub });

var session = new SessionStore();
db.remove.on('called', function(args) {
args.callback(null);
});

session.clear(function(error) {
assert.ifError(error);
done();
});
});
});

});

0 comments on commit 9a64af8

Please sign in to comment.