Skip to content

Commit

Permalink
Add session store for connect
Browse files Browse the repository at this point in the history
  • Loading branch information
arronzhang committed Dec 15, 2011
1 parent 62037c8 commit eac2b1c
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 15 deletions.
6 changes: 6 additions & 0 deletions lib/mongoq.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ mongoq.collection = collection;

mongoq.cursor = cursor;

/**
* Link to SessionStore
* @api public
*/

mongoq.SessionStore = require("./session");

/**
* Connnect to mongodb
Expand Down
148 changes: 148 additions & 0 deletions lib/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* connect session store
*/

/**
* Module dependencies
*/

var Store;
try{
Store = require('connect').session.Store;
}catch(e){
try{
Store = require('express').session.Store;
}catch(e){
Store = loop;
}
}

function loop () {
}

/**
* Initialize SessionStore with the given `options`.
*
* @param {Object} options
* @api public
*/

var SessionStore = module.exports = function SessionStore( col, options ) {

options = options || {};
Store.call( this, options );

this.collection = col;
this.capped = col._options.capped;
};

/**
* Inherit from `Store`.
*/

SessionStore.prototype.__proto__ = Store.prototype;

/**
* Auto clear expired data if the collection is not capped
*
* @param {Number} interval second
* @api public
*
*/

SessionStore.prototype.autoClear = function( interval ) {
var self = this;
setInterval(function() {
!self.capped && self.collection.remove( { expires: {$lte: new Date()} } );
}, clear_interval * 1000);
};

/**
* Attempt to fetch session by the given `sid`.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/

SessionStore.prototype.get = function(sid, fn) {
var self = this;
self.collection.findOne({_id: sid})
.done( function(sess) {
if (sess) {
if ( !sess.expires || new Date < sess.expires ) {
fn && fn( null, JSON.parse( sess.session ) );
} else {
self.destroy( sid, fn );
}
} else {
fn && fn();
}
} )
.fail( fn );
};

/**
* Commit the given `sess` object associated with the given `sid`.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} fn
* @api public
*/

SessionStore.prototype.set = function( sid, session, fn ) {
this.collection.update(
{ _id: sid }
, {
_id: sid
, session: JSON.stringify( session )
, expires: session && session.cookie && session.cookie.expires ?
new Date( session.cookie.expires ) : new Date("3040-1-1")
}
, { upsert: true, safe: true }
, function( err ) {
(fn || loop)( err );
}
);
};

/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @api public
*/

SessionStore.prototype.destroy = function( sid, fn ) {
this.capped ?
this.collection.update( {_id: sid}, { $set: { expires: new Date("2000-1-1") } }, {}, function( err ) {
(fn || loop)( err );
} ) :
this.collection.remove( {_id: sid}, function( err ) {
(fn || loop)( err );
} );
};

/**
* Fetch number of sessions.
*
* @param {Function} fn
* @api public
*/

SessionStore.prototype.length = function( fn ) {
this.collection.count({ expires: { $gt: new Date() } }, fn || loop );
};

/**
* Clear all sessions.
*
* @param {Function} fn
* @api public
*/

SessionStore.prototype.clear = function(fn) {
this.collection.drop( fn || loop );
};

31 changes: 16 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
{
"name": "mongoq"
, "version": "0.2.2"
, "description": "Use mongoDB like this: require('mongoq')('testdb').collection('users').find(function(err, cursor){});"
, "keywords": ["mongodb", "mongoq", "data", "datastore", "nosql"]
, "author" : "Hidden <zzdhidden@gmail.com>"
, "repository" : {
"type" : "git",
"url" : "http://github.com/zzdhidden/mongoq.git"
, "version": "0.2.3"
, "description": "Use mongoDB like this: require('mongoq')('testdb').collection('users').find(function(err, cursor){});"
, "keywords": ["mongodb", "mongoq", "data", "datastore", "nosql"]
, "author" : "Hidden <zzdhidden@gmail.com>"
, "repository" : {
"type" : "git",
"url" : "http://github.com/zzdhidden/mongoq.git"
}
, "dependencies": {
"mongodb" : ">=0.9.1"
, "jquery-deferred": ">=0.2.0"
, "dependencies": {
"mongodb" : ">=0.9.1"
, "jquery-deferred": ">=0.2.0"
}
, "devDependencies": {
"mocha" : "*"
, "should": ">=0.2.1"
, "devDependencies": {
"mocha" : "*"
, "connect": "*"
, "should": ">=0.2.1"
}
, "main": "index"
, "engines": { "node": ">=0.4.0" }
, "main": "index"
, "engines": { "node": ">=0.4.0" }
}
71 changes: 71 additions & 0 deletions test/session.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var mongoq = require('../index.js')
, should = require('should');

describe("session", function() {

var db = mongoq("mongoqTest");

it("should work with normal collection", function( done ) {
var sessions = db.collection("sessions");

var store = new mongoq.SessionStore( sessions );

store.clear( function(err) {
store.set( "1", {id: 1 }, function( err ) {
should.not.exist( err );
store.set( "1", {id: 2}, function( err ) {
should.not.exist( err );
store.get( "1", function(err, sess) {
sess.should.eql( { id: 2 } );
store.destroy("1", function(err, c) {
should.not.exist( err );
should.not.exist( c );
store.get( "1", function(err, sess) {
should.not.exist( sess );
store.length( function(err, l) {
l.should.equal( 0 );
db.close();
done();
} );
});
});
});
} );
} );
} );
});

it("should work with capped collection", function( done ) {
var sessions2 = db.collection("sessions2", {
capped: true
, max: 4096
, size: 4096 * 8192
, autoIndexId: true
});

var store = new mongoq.SessionStore( sessions2 );
store.clear( function(err) {
store.set( "1", {id: 1 }, function( err ) {
should.not.exist( err );
store.set( "1", {id: 2}, function( err ) {
should.not.exist( err );
store.get( "1", function(err, sess) {
sess.should.eql( { id: 2 } );
store.destroy("1", function(err, c) {
should.not.exist( err );
should.not.exist( c );
store.get( "1", function(err, sess) {
should.not.exist( sess );
store.length( function(err, l) {
l.should.equal( 0 );
done();
});
});
});
});
} );
} );
} );
});
});

0 comments on commit eac2b1c

Please sign in to comment.