Permalink
Browse files

Merge pull request #150 from lusever/connect-mongodb

Replace support/connect-mongodb.js to the npm version
  • Loading branch information...
2 parents 23dbf75 + 2ffb715 commit f30b8003cd6069afa6a9e8ada5e04393a1e6baf4 @cliftonc committed Jun 8, 2012
Showing with 6 additions and 227 deletions.
  1. +1 −3 app.js
  2. +1 −3 lib/core/Module.js
  3. +3 −3 lib/core/Storage.js
  4. +1 −1 package.json
  5. +0 −217 support/connect-mongodb.js
View
4 app.js
@@ -13,14 +13,12 @@ var rootpath = process.cwd() + '/',
path = require('path'),
fs = require('fs'),
express = require('express'),
- mongoose = require('mongoose'),
nodepath = require('path'),
stylus = require('stylus'),
colors = require('colors'),
calipso = require(path.join(rootpath, 'lib/calipso')),
translate = require(path.join(rootpath, 'i18n/translate')),
- logo = require(path.join(rootpath, 'logo')),
- mongoStore = require(path.join(rootpath, 'support/connect-mongodb'));
+ logo = require(path.join(rootpath, 'logo'));
// Local App Variables
var path = rootpath,
View
@@ -1,8 +1,6 @@
var app, rootpath = process.cwd() + '/',
path = require('path'),
- calipso = require(path.join('..', 'calipso')),
- mongoStore = require(path.join(rootpath, 'support/connect-mongodb')),
- mongoose = require('mongoose');
+ calipso = require(path.join('..', 'calipso'));
/**
* Route all of the modules based on the module event model
View
@@ -9,7 +9,7 @@
var rootpath = process.cwd(),
path = require('path'),
events = require('events'),
- mongoStore = require(path.join(rootpath, 'support/connect-mongodb')),
+ mongoStore = require('connect-mongodb'),
mongoose = require('mongoose'),
calipso = require(path.join('..', 'calipso'));
@@ -68,8 +68,8 @@ Storage.prototype.mongoConnect = function(dbUri, checkInstalling, next) {
foundMiddleware = true;
mw = calipso.lib.express.session({
secret: calipso.config.get('session:secret'),
- store: mongoStore({
- url: calipso.config.get('database:uri')
+ store: new mongoStore({
+ db: calipso.db.db
})
});
mw.tag = 'session';
View
@@ -20,8 +20,8 @@
"stylus": "0.23.x",
"underscore":"1.3.x",
"mongoose": "2.5.x",
+ "connect-mongodb": "1.1.x",
"winston": "0.5.x",
- "mongodb": "0.9.x",
"qs": "0.4.x",
"request":"2.9.x",
"pool":"0.4.x",
View
@@ -1,217 +0,0 @@
-/*!
- * Connect - Mongo
- * Copyright(c) 2010 Vladimir Dronnikov <dronnikov@gmail.com>
- * Mantained by Pau Ramon Revilla <masylum@gmail.com>
- * MIT Licensed
- */
-
-/**
- * Module dependencies.
- */
-
-var Store = require('connect').session.Store,
- mongo = require('mongodb'),
-
- defaults = {host: '127.0.0.1', port: 27017, dbname: '/dev', collection: 'sessions'},
-
- getConnectionURL = function (options) {
-
- var url = 'mongo://';
-
- if (options.username) {
- url += options.username;
- if (options.password) {
- url += ':' + options.password;
- } else {
- throw Error('Username set without password. Need both to make a connection');
- }
- url += '@';
- }
-
- url += options.host || defaults.host;
- url += options.port ? ':' + options.port : ':' + defaults.port;
- url += options.dbname ? '/' + options.dbname : defaults.dbname;
-
- // delete this options so we don't send them to Server
- ['username', 'password', 'host', 'port', 'dbname'].forEach(function (attr) {
- delete options[attr];
- });
-
- if (options.url) {
- return options.url;
- } else {
- return url;
- }
- },
-
- parseConnectionURL = function (url) {
- var config = require('url').parse(url),
- auth = null;
-
- if (!config.protocol.match(/^mongo/)) {
- throw new Error("URL must be in the format mongo://user:pass@host:port/dbname");
- }
-
- if (config.auth) {
- auth = config.auth.split(':', 2);
- }
-
- return {
- host: config.hostname || defaults.host,
- port: config.port || defaults.port,
- dbname: config.pathname.replace(/^\//, '') || defaults.dbname,
- username: auth && auth[0],
- password: auth && auth[1]
- };
- };
-
-
-/**
- * Initialize MongoStore with the given `options`.
- *
- * @param {Object} options
- * @api public
- */
-
-module.exports = function (options) {
-
- options = options || {};
-
- var MONGOSTORE = Store.prototype,
-
- _collection = null,
- _url = getConnectionURL(options),
- _details = parseConnectionURL(_url), // mongodb 0.7.9 parser is broken, this fixes it
- _db = new mongo.Db(_details.dbname, new mongo.Server(_details.host, _details.port, options)),
-
- _default = function (callback) {
- callback = callback || function () { };
- return callback;
- },
-
- _getCollection = function (_db) {
- _db.collection(options.collection || defaults.collection, function (err, col) {
- if (err) {
- throw err;
- }
- _collection = col;
- });
- };
-
- if (options.reapInterval !== -1) {
- setInterval(function () {
- _collection.remove({expires: {'$lte': Date.now()}}, function () { });
- }, options.reapInterval || 60000, this); // every minute
- }
-
-
- _db.open(function (err) {
- if (err) {
- throw Error("Error connecting to " + _url);
- }
-
- if (_details.username && _details.password) {
- _db.authenticate(_details.username, _details.password, function () {
- _getCollection(_db);
- });
- } else {
- _getCollection(_db);
- }
- });
-
- Store.call(MONGOSTORE, options);
-
- /**
- * Attempt to fetch session by the given `sid`.
- *
- * @param {String} sid
- * @param {Function} cb
- * @api public
- */
-
- MONGOSTORE.get = function (sid, cb) {
- _default(cb);
- _collection.findOne({_id: sid}, function (err, data) {
- try {
- if (data) {
- cb(null, JSON.parse(data.session.toString()));
- } else {
- cb();
- }
- } catch (exc) {
- cb(exc);
- }
- });
- };
-
-
- /**
- * Commit the given `sess` object associated with the given `sid`.
- *
- * @param {String} sid
- * @param {Session} sess
- * @param {Function} cb
- * @api public
- */
-
- MONGOSTORE.set = function (sid, sess, cb) {
- _default(cb);
- try {
- var update = {_id: sid, session: JSON.stringify(sess)};
- if (sess && sess.cookie && sess.cookie.expires) {
- update.expires = Date.parse(sess.cookie.expires);
- }
-
- _collection.update({_id: sid}, update, {upsert: true}, function (err, data) {
- cb.apply(this, arguments);
- });
- } catch (exc) {
- cb(exc);
- }
- };
-
- /**
- * Destroy the session associated with the given `sid`.
- *
- * @param {String} sid
- * @api public
- */
-
- MONGOSTORE.destroy = function (sid, cb) {
- _collection.remove({_id: sid}, _default(cb));
- };
-
- /**
- * Fetch number of sessions.
- *
- * @param {Function} cb
- * @api public
- */
-
- MONGOSTORE.length = function (cb) {
- _collection.count({}, _default(cb));
- };
-
- /**
- * Clear all sessions.
- *
- * @param {Function} cb
- * @api public
- */
-
- MONGOSTORE.clear = function (cb) {
- _collection.drop(_default(cb));
- };
-
- /**
- * Get the collection
- *
- * @param
- * @api public
- */
- MONGOSTORE.getCollection = function () {
- return _collection;
- };
-
- return MONGOSTORE;
-};

0 comments on commit f30b800

Please sign in to comment.