Created
April 11, 2011 13:27
-
-
Save xjamundx/913508 to your computer and use it in GitHub Desktop.
Simple MongoDB Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////// | |
// Simple MongoDB Wrapper // | |
// Written to use with MongoHQ.com // | |
// By Jamund Ferguson // | |
// April 9, 2011 | |
//////////////////////////////////////// | |
var mongodb = require('mongodb'), | |
db; | |
// inititalize the db | |
exports.init = function(options) { | |
db = new mongodb.Db(options.name, new mongodb.Server(options.host, options.port, {auto_reconnect:true}), {}); | |
db.open(function(err, p_client) { | |
if (typeof options.user !== 'string' || typeof options.pass !== 'string') return; | |
db.authenticate(options.user, options.pass, function(err) { | |
if (err) console.log(err); | |
}); | |
}); | |
return this; | |
} | |
// register a collection for use | |
exports.collection = function(collection) { | |
db.collection(collection, function(err, col) { | |
exports[collection] = col; | |
}); | |
} | |
// convenient access to make ObjectIDs | |
exports.ObjectID = mongodb.BSONPure.ObjectID; | |
// convenient access to the native driver | |
exports.db = db; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment