Last active
March 13, 2016 21:12
-
-
Save vdeturckheim/2b2b4391fac17bacf4ff to your computer and use it in GitHub Desktop.
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
'use strict'; | |
const Mongoose = require('mongoose'); | |
const validateOptions = require('./lib').validateOptions; | |
const builConnectionString = require('./lib').builConnectionString; | |
module.exports.register = function (server, options, next) { | |
const validation = validateOptions(options); | |
if (validation.error) { | |
return next(validation.error); | |
} | |
options = validation.value; | |
const logMe = function (toLog){ | |
server.log(options.logTags, toLog); | |
}; | |
const connectionString = builConnectionString(options); | |
if (options.promise) { | |
let promise = global.Promise; | |
if (options.promise !== 'native') { | |
try { | |
logMe(`trying to use promises implemented by ${options.promise}`); | |
promise = require(options.promise); | |
} | |
catch (err) { | |
return next(err); | |
} | |
} | |
else { | |
logMe('using default promises'); | |
} | |
Mongoose.promise = promise; | |
} | |
Mongoose.set('debug' , options.debug); | |
// see http://mongoosejs.com/docs/api.html#connection_Connection-readyState | |
if (Mongoose.connection.readyState === 0) { | |
// do not show user's credentials here !! | |
logMe(`Connecting to database ${options.hostname}:${options.port}/${options.database}`); | |
Mongoose.connect(connectionString, (err) => { | |
// if no error is returned, it will go alright | |
return next(err); | |
}); | |
} | |
else { | |
return next(`Connection to mongodb was in status ${Mongoose.connection.readyState} which is not 'disconnected'`); | |
} | |
}; | |
module.exports.register.attributes = { | |
pkg: require('./package.json') | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment