Skip to content

Instantly share code, notes, and snippets.

@arunoda
Created April 7, 2011 08:13
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 arunoda/907310 to your computer and use it in GitHub Desktop.
Save arunoda/907310 to your computer and use it in GitHub Desktop.
MongoDB Helper using QBox
var mongo = require("./mongoHelper").connect("localhost", 27017, "test");
mongo.collection("collname", function(coll) {
});
var qbox = require('qbox');
var mongo = require('mongodb');
exports.connect = function(host, port, database) {
return new MongoHelper(host, port, database);
};
function MongoHelper(host, port, database) {
var $ = qbox.create();
var db = new mongo.Db(database, new mongo.Server(host, port), {});
db.open(function() {
console.info("Connected to Mongo db: " + database + " @ host: " + host +" port: " + port);
$.start();
});
this.collection = function(name, callback) {
$.ready(function() {
db.collection(name, afterCollConnected);
function afterCollConnected (err, coll) {
if(err) {
console.error("Connecting to collection:routes failed /n" + JSON.stringify(err));
throw new Error("Connecting to collection:routes failed /n" + JSON.stringify(err));
} else {
console.info("Connected to collection: " + name);
callback(coll);
}
}
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment