Created
April 7, 2011 08:13
-
-
Save arunoda/907310 to your computer and use it in GitHub Desktop.
MongoDB Helper using QBox
This file contains hidden or 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
var mongo = require("./mongoHelper").connect("localhost", 27017, "test"); | |
mongo.collection("collname", function(coll) { | |
}); |
This file contains hidden or 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
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