Created
March 16, 2016 13:17
-
-
Save jmjuanes/56fc48e9346d18eaaad6 to your computer and use it in GitHub Desktop.
MongoDB Middleware
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
//Connection url | |
//mongodb://USERNAME:PASSWORD@DBHOST:DBPORT/DBNAME | |
//Import dependencies | |
var MongoClient = require('mongodb').MongoClient; | |
//Database | |
var db = null; | |
//Connect to database | |
exports.Connect = function(opt, callback) | |
{ | |
//Check the database status | |
if(db){ return callback(); } | |
//Create the url | |
var url = 'mongodb://' + opt.user + ':' + opt.pass + '@' + opt.host + ':' + opt.port + '/' + opt.db; | |
//Else, connect to database | |
MongoClient.connect(url, function(err, db){ | |
//Check for error | |
if(err) | |
{ | |
//Show error in console | |
console.error(err); | |
//Return with error | |
return callback(err); | |
} | |
//Save the database connection | |
db = db; | |
//Do the callback | |
callback(); | |
}); | |
}; | |
//Get the database | |
exports.Get = function(){ return db; }; | |
//Close the connection | |
exports.Close = function(callback){ | |
//Check the database status | |
if(state.db) | |
{ | |
//Close the connection to the database | |
state.db.close(function(err, result){ | |
//Destroy the database | |
db = null; | |
//Do the callback | |
callback(err); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment