Skip to content

Instantly share code, notes, and snippets.

@JustinMorgan
Created April 5, 2015 02:24
Show Gist options
  • Save JustinMorgan/c1ae74b9f86cf2672e1e to your computer and use it in GitHub Desktop.
Save JustinMorgan/c1ae74b9f86cf2672e1e to your computer and use it in GitHub Desktop.
var mongoose = require("mongoose"),
connection = mongoose.connection,
gracefulExit = function() {
connection.close(function() {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
};
mongoose.connect(process.env.MONGO_URI);
connection.on('error', function(err) {
console.error('DB connection error', err);
}).once('open', function() {
console.log('DB open');
});
process.on('SIGINT', gracefulExit)
.on('SIGTERM', gracefulExit);
module.exports = function(name, dataStructure) {
var schema = new Schema(dataStructure);
return mongoose.model(name, schema);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment