Skip to content

Instantly share code, notes, and snippets.

@JonDum
Created September 16, 2011 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonDum/1223278 to your computer and use it in GitHub Desktop.
Save JonDum/1223278 to your computer and use it in GitHub Desktop.
Simple helper & Express/Connect session_store implementation for Mongolian.
var Mongolian = require("mongolian");
bson = require('mongodb').BSONPure;
/**
* DatabaseHelper defines database collections and handles connect to the database.
*
* Usage:
*
* var db = require('DatabaseHelper.js');
*
* db.myCollection.find(...);
* db.myCollection.save(...);
*
*/
/////////////////////////////////////////////////////////////////////////
//++//
//++++//
//++//
//++++// Constructor
//++//
//++++//
//++//
//////////////////////////////////////////////////////////////////////////
function DatabaseHelper() {};
//----------------------------------
// Private members
//----------------------------------
// Create a server instance with default host and port, or provide your own configuraiton
var server = new Mongolian();
// store all collections in this database
var db = server.db('my_database');
// define another db if you want
var dev_db = server.db('dev_db');
//----------------------------------
// Public Members
//----------------------------------
// Define some collections
// This is nice because if you accidentally mistype a collection, you'll
// get a javascript exception instead of stealth-saving to the wrong
DatabaseHelper.prototype.my_collection = db.collection('my_collection');
DatabaseHelper.prototype.users = db.collection('users');
DatabaseHelper.prototype.posts = db.collection('posts');
DatabaseHelper.prototype.comments = db.collection('comments');
DatabaseHelper.prototype.whatever = db.collection('whatever');
DatabaseHelper.prototype.anotherDB = dev_db.collection('myCollection');
//----------------------------------
// Session Store
//----------------------------------
// Usage: app.use(express.session({secret: 'your secret here', store: db.session_store}));
var sessions = db.collection('sessions');
DatabaseHelper.prototype.session_store = {
get: function(session_id, callback) {
sessions.findOne({_id: session_id}, function(error, result) {
if(result)
callback(null, JSON.parse(result.toString()));
else if(error)
callback(error);
});
},
set: function(session_id, session, callback) {
var update = {_id: session_id, session: JSON.stringify(session)};
if(session && session.cookie && session.cookie.expires)
update.expires = Date.parse(session.cookie.expires);
sessions.update({_id: session_id}, update, true, function(error, result)
{
callback(error, result);
});
},
destroy: function(session_id, callback) {
sessions.remove({_id: session_id}, callback);
},
length: function(callback) {
sessions.count({}, callback)
},
clear: function(callback) {
db.sessions.drop(callback);
}
};
//----------------------------------
// Misc.
//----------------------------------
//helpers for converting to BSON types, avoid making a collection of the same name
DatabaseHelper.prototype.Long = bson.Long;
DatabaseHelper.prototype.ObjectId = bson.ObjectID;
DatabaseHelper.prototype.Timestamp = bson.Timestamp;
DatabaseHelper.prototype.Binary = bson.Binary;
DatabaseHelper.prototype.DBRef = bson.DBRef;
DatabaseHelper.prototype.Code = bson.Code;
module.exports = new DatabaseHelper();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment