Last active
December 14, 2015 10:38
-
-
Save jfromaniello/5073219 to your computer and use it in GitHub Desktop.
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 url = require('url'), | |
parsedUrl = url.parse(process.env.DB || 'mongodb://localhost/mdocs'); | |
var dbSettings = { | |
host: parsedUrl.hostname, | |
port: parseInt(parsedUrl.port || 27017, 10), | |
name: parsedUrl.pathname.substr(1), | |
user: parsedUrl.auth ? parsedUrl.auth.split(':')[0] : null, | |
password: parsedUrl.auth ? parsedUrl.auth.split(':')[1] : null | |
}; | |
module.exports = function gtDb(callback) { | |
var mongoserver = new mongodb.Server(dbSettings.host, dbSettings.port, { | |
auto_reconnect: true, | |
socketOptions: { keepAlive: 300 }, | |
poolSize: 10 | |
}); | |
var dbConnector = new mongodb.Db(dbSettings.name, mongoserver, {safe: true}); | |
dbConnector.open(function(err, db){ | |
if ( err ) { | |
console.log('error connecting to the db, exiting'); | |
return callback(err); | |
} | |
if(dbSettings.user && dbSettings.password){ | |
console.log("authenticating to mongodb"); | |
db.authenticate(dbSettings.user, dbSettings.password, function(err){ | |
if(err){ | |
console.log('database authentication fail'); | |
return callback(err); | |
} | |
return callback(null, db); | |
}); | |
}else{ | |
return callback(null, db); | |
} | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment