Commit b0ca4781 authored by Hervé Ménager's avatar Hervé Ménager

remove mongoose wrapper

parent 3ebec2a5
var mongoose = require('mongoose');
var _ = require('underscore');
var preloaded = {}; //preloaded data
var connected = function(connectedCall, connectionString, preloads) {
mongoose.connection.on("open", function(ref) {
console.log("Connected to " + connectionString + " DB!");
if (preloads) { //run preloads
preloaded = _.mapObject(preloads, function(loadFn, name) {
console.log('preloading ' + name);
return loadFn(function(result) {
preloaded[name] = result;
console.log('preloading ' + name + ' finished.');
});
});
}
return connectedCall();
});
// If the connection throws an error
mongoose.connection.on("error", function(err) {
console.error('Failed to connect to DB ' + connectionString + ' on startup ', err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function() {
console.log('Mongoose default connection to DB :' + connectionString + ' disconnected');
});
var gracefulExit = function() {
mongoose.connection.close(function() {
console.log('Mongoose default connection with DB :' + connectionString + ' is disconnected through app termination');
process.exit(0);
});
};
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', gracefulExit).on('SIGTERM', gracefulExit);
try {
console.log("Trying to connect to DB " + connectionString);
mongoose.connect(connectionString);
} catch (err) {
console.log("Server initialization failed ", err.message);
}
};
var getPreloaded = function(name) {
return preloaded[name];
};
module.exports = {
'connected': connected,
'getPreloaded': getPreloaded
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment