Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mongo = require('mongodb')
- ,jqd = require('../jquery-jqd').jqd
- ,host = process.env['MONGO_NODE_DRIVER_HOST'] != null
- ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost'
- ,port = process.env['MONGO_NODE_DRIVER_PORT'] != null
- ? process.env['MONGO_NODE_DRIVER_PORT'] : mongo.Connection.DEFAULT_PORT
- ,db = null
- ,gConnection = null;
- /*
- args can be:
- 1) string: 'database name', [mongo.Server] // name of db, optional server, defaults to default port, localhost, autoreconnect
- 2) deferred: con // a deferred obj with a 'this' of the db connection
- 3) none: // assumed that a connection already exists
- */
- exports.mdb = function mdb(args){
- switch(arguments.length){
- case 1:
- if(typeof arguments[0] === 'string'){
- // check for connection, create default server otherwise
- if(db === null){
- db = new mongo.Db(arguments[0],
- new mongo.Server(host, port, {
- auto_reconnect: true
- }), { native_parser: true });
- gConnection = exports.mdb.connect();
- }
- } else if (typeof arguments[0].promise !== 'undefined'){
- // we have a promise, assume a connection
- gConnection = arguments[0];
- }
- break;
- case 2:
- // assume first param is string, second is mongo.Server instance
- db = new mongo.Db(arguments[0], arguments[1]);
- gConnection = exports.mdb.connect();
- break;
- default:
- // zero params, assume we have a deferred connection somewhere
- if(!gConnection) throw new Error('There is no MongoDb connection to use.');
- break;
- }
- var con = gConnection;
- return {
- connection: con
- ,collection: function(name){
- var colDfd = jqd.deferred()
- ,p = colDfd.promise();
- // make sure we have a connection, which we should regardless
- con.then(function(){
- this.collection(name, function(err, col){
- if(err){
- colDfd.reject();
- } else {
- colDfd.resolveWith(col, [col]);
- }
- });
- });
- return {
- collection: p // the collection's promise
- ,get: function(callback){
- return p.then(callback);
- }
- ,find: function(query){
- var dfd = jqd.deferred();
- p.then(function(){
- this.find(query, function(err, result){
- if(err){
- dfd.reject();
- } else {
- result.toArray(function(err, docs){
- if(err) dfd.reject();
- else dfd.resolveWith(docs, [docs]);
- });
- }
- });
- });
- return dfd.promise();
- }
- ,findOne: function(query){
- var dfd = jqd.deferred();
- p.then(function(){
- this.findOne(query, function(err, doc){
- if(err){
- dfd.reject();
- } else {
- dfd.resolveWith(doc, [doc]);
- }
- });
- });
- return dfd.promise();
- }
- ,findOneOrDie: function(query){
- var dfd = jqd.deferred();
- p.then(function(){
- this.findOne(query, function(err, doc){
- if(err || !doc){
- dfd.reject();
- } else {
- dfd.resolveWith(doc, [doc]);
- }
- });
- });
- return dfd.promise();
- }
- ,insert: function(docs){
- var dfd = jqd.deferred();
- p.then(function(){
- this.insert(docs, function(err, docs){
- if(err){
- dfd.reject(err);
- } else {
- dfd.resolveWith(docs, [err, docs]);
- }
- });
- });
- return dfd.promise();
- }
- ,findAndModify: function(options){
- var dfd = jqd.deferred();
- p.then(function(){
- this.findAndModify(
- options.query
- ,options.sort || []
- ,options.update
- ,options.options
- ,function(err, result){
- if(err) {
- dfd.rejectWith(err, [err, result]);
- } else {
- dfd.resolveWith(result, [err, result]);
- }
- });
- });
- return dfd.promise();
- }
- }
- }
- }
- }
- exports.mdb.connect = function connect(){
- return jqd.deferred(function(dfd){
- db.open(function(err, client){
- if(err){
- dfd.reject(err);
- } else {
- dfd.resolveWith(client, [client]);
- }
- });
- }).promise();
- }
- // attach mongo lib to expo
- exports.mdb.mongo = mongo;
- exports.express = function express(args){
- // start the connection
- var m = exports.mdb(args);
- exports.mdb.BSON = db.options.native_parser ? mongo.BSONNative : mongo.BSONPure;
- return function(req, res, next){
- if(!req.mdb) req.mdb = exports.mdb;
- next();
- }
- }
Add Comment
Please, Sign In to add comment