Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'clear()' to session store #30

Merged
merged 2 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});

});