Permalink
Browse files

Add session store for connect

  • Loading branch information...
1 parent 62037c8 commit eac2b1c4984eba02672283c5e91256d855ca5ed5 @zzdhidden committed Dec 15, 2011
Showing with 241 additions and 15 deletions.
  1. +6 −0 lib/mongoq.js
  2. +148 −0 lib/session.js
  3. +16 −15 package.json
  4. +71 −0 test/session.test.js
View
@@ -74,6 +74,12 @@ mongoq.collection = collection;
mongoq.cursor = cursor;
+/**
+* Link to SessionStore
+* @api public
+*/
+
+mongoq.SessionStore = require("./session");
/**
* Connnect to mongodb
View
@@ -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 );
+};
+
View
@@ -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" }
}
View
@@ -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.