Skip to content
Browse files

initial check-in

  • Loading branch information...
0 parents commit 4fbff4a63b42337089991598617aee21ec3023fd @donpark donpark committed
Showing with 245 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +34 −0 README.md
  3. +56 −0 index.coffee
  4. +135 −0 index.js
  5. +19 −0 package.json
1 .gitignore
@@ -0,0 +1 @@
+node_modules
34 README.md
@@ -0,0 +1,34 @@
+`session-mongoose` module is an implementation of `connect` session store using [Mongoose](http://mongoosejs.com).
+
+## Implementation Note:
+
+Uses [mongeese](https://github/donpark/mongeese) module to isolate session database from app's default Mongoose database.
+
+## Install
+
+ npm install session-mongoose
+
+## Usage
+
+Replace code requiring `mongoose` module like this:
+
+ var mongoose = require("mongoose");
+ mongoose.connect(...);
+
+with:
+
+ var mongoose = require("mongeese").create();
+ mongoose.connect(...);
+
+Result of `create` method call should look and behave exactly like `mongoose` module
+except each result can have its own *default connection*.
+
+If you need to handle multiple databases at the same time, following should suffice:
+
+ var logdb = require("mongeese").create();
+ var keydb = require("mongeese").create();
+
+ logdb.connect('mongodb://localhost/log');
+ keydb.connect('mongodb://localhost/key');
+
+That's it.
56 index.coffee
@@ -0,0 +1,56 @@
+mongoose = require('mongeese').create() # isolated Mongoose instance
+Schema = mongoose.Schema
+
+SessionSchema = new Schema({
+ sid: { type: String, required: true, unique: true }
+ data: { type: String, default: '{}' }
+ expires: { type: Date, index: true }
+})
+
+Session = mongoose.model('Session', SessionSchema)
+
+defaultCallback = (err) ->
+
+class SessionStore extends require('connect').session.Store
+ constructor: (options) ->
+ options?.expires ?= 60000
+ mongoose.connect options.url
+ setInterval (-> Session.remove { expires: { '$lte': new Date() }}, defaultCallback), options.expires
+
+ get: (sid, cb = defaultCallback) ->
+ Session.findOne { sid: sid }, (err, session) ->
+ if session?
+ try
+ cb null, JSON.parse session.data
+ catch err
+ cb err
+ else
+ cb err, session
+
+ set: (sid, data, cb = defaultCallback) ->
+ try
+ Session.update { sid: sid }, {
+ sid: sid
+ data: JSON.stringify data
+ expires: if data?.cookie?.expires? then data.cookie.expires else null
+ }, { upsert: true }, cb
+ catch err
+ cb err
+
+ destroy: (sid, cb = defaultCallback) ->
+ Session.remove { sid: sid }, cb
+
+ all: (cb = defaultCallback) ->
+ Session.find { expires: { '$gte': new Date() } }, [ 'sid' ], (err, sessions) ->
+ if sessions?
+ cb null, (session.sid for session in sessions)
+ else
+ cb err
+
+ clear: (cb = defaultCallback) ->
+ Session.drop cb
+
+ length: (cb = defaultCallback) ->
+ Session.count {}, cb
+
+module.exports = SessionStore
135 index.js
@@ -0,0 +1,135 @@
+(function() {
+ var Schema, Session, SessionSchema, SessionStore, defaultCallback, mongoose;
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
+ for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
+ function ctor() { this.constructor = child; }
+ ctor.prototype = parent.prototype;
+ child.prototype = new ctor;
+ child.__super__ = parent.prototype;
+ return child;
+ };
+ mongoose = require('mongeese').create();
+ Schema = mongoose.Schema;
+ SessionSchema = new Schema({
+ sid: {
+ type: String,
+ required: true,
+ unique: true
+ },
+ data: {
+ type: String,
+ "default": '{}'
+ },
+ expires: {
+ type: Date,
+ index: true
+ }
+ });
+ Session = mongoose.model('Session', SessionSchema);
+ defaultCallback = function(err) {};
+ SessionStore = (function() {
+ __extends(SessionStore, require('connect').session.Store);
+ function SessionStore(options) {
+ var _ref;
+ if (options != null) {
+ if ((_ref = options.expires) != null) {
+ _ref;
+ } else {
+ options.expires = 60000;
+ };
+ }
+ mongoose.connect(options.url);
+ setInterval((function() {
+ return Session.remove({
+ expires: {
+ '$lte': new Date()
+ }
+ }, defaultCallback);
+ }), options.expires);
+ }
+ SessionStore.prototype.get = function(sid, cb) {
+ if (cb == null) {
+ cb = defaultCallback;
+ }
+ return Session.findOne({
+ sid: sid
+ }, function(err, session) {
+ if (session != null) {
+ try {
+ return cb(null, JSON.parse(session.data));
+ } catch (err) {
+ return cb(err);
+ }
+ } else {
+ return cb(err, session);
+ }
+ });
+ };
+ SessionStore.prototype.set = function(sid, data, cb) {
+ var _ref;
+ if (cb == null) {
+ cb = defaultCallback;
+ }
+ try {
+ return Session.update({
+ sid: sid
+ }, {
+ sid: sid,
+ data: JSON.stringify(data),
+ expires: (data != null ? (_ref = data.cookie) != null ? _ref.expires : void 0 : void 0) != null ? data.cookie.expires : null
+ }, {
+ upsert: true
+ }, cb);
+ } catch (err) {
+ return cb(err);
+ }
+ };
+ SessionStore.prototype.destroy = function(sid, cb) {
+ if (cb == null) {
+ cb = defaultCallback;
+ }
+ return Session.remove({
+ sid: sid
+ }, cb);
+ };
+ SessionStore.prototype.all = function(cb) {
+ if (cb == null) {
+ cb = defaultCallback;
+ }
+ return Session.find({
+ expires: {
+ '$gte': new Date()
+ }
+ }, ['sid'], function(err, sessions) {
+ var session;
+ if (sessions != null) {
+ return cb(null, (function() {
+ var _i, _len, _results;
+ _results = [];
+ for (_i = 0, _len = sessions.length; _i < _len; _i++) {
+ session = sessions[_i];
+ _results.push(session.sid);
+ }
+ return _results;
+ })());
+ } else {
+ return cb(err);
+ }
+ });
+ };
+ SessionStore.prototype.clear = function(cb) {
+ if (cb == null) {
+ cb = defaultCallback;
+ }
+ return Session.drop(cb);
+ };
+ SessionStore.prototype.length = function(cb) {
+ if (cb == null) {
+ cb = defaultCallback;
+ }
+ return Session.count({}, cb);
+ };
+ return SessionStore;
+ })();
+ module.exports = SessionStore;
+}).call(this);
19 package.json
@@ -0,0 +1,19 @@
+{
+ "author": "Don Park <donpark@docuverse.com> (http://blog.docuverse.com)",
+ "name": "session-mongoose",
+ "description": "connect session store implementation using Mongoose",
+ "version": "0.0.1",
+ "main": "./index.js",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/donpark/session-mongoose.git"
+ },
+ "engines": {
+ "node": ">= v0.4.0"
+ },
+ "dependencies": {
+ "connect": ">= 1.0.0",
+ "mongeese": ">= 0.0.1"
+ },
+ "devDependencies": {}
+}

0 comments on commit 4fbff4a

Please sign in to comment.
Something went wrong with that request. Please try again.