-
-
Save dobbbri/bdba02d4ab96c2dd891b1ccb90b413fb to your computer and use it in GitHub Desktop.
Creating models dynamically
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 mongoose = require('mongoose'); | |
var BeerSchema = new mongoose.Schema({ | |
id: { type: Number, min: 0}, | |
name: { type: String, default: '' }, | |
description: { type: String, default: '' }, | |
abv: { type: Number, min: 0}, | |
category: { type: String, default: ''}, | |
created_at: { type: Date, default: Date.now }, | |
updated_at: { type: Date, default: Date.now } | |
}); | |
exports.BeerSchema = BeerSchema; | |
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
// Bring Mongoose into the app | |
var mongoose = require( 'mongoose' ); | |
// Build the connection string | |
var dbURI = 'mongodb://localhost/mongoose-best-practices'; | |
// Create the database connection | |
mongoose.connect(dbURI); | |
// CONNECTION EVENTS | |
// When successfully connected | |
mongoose.connection.on('connected', function () { | |
console.log('Mongoose default connection open to ' + dbURI); | |
}); | |
// If the connection throws an error | |
mongoose.connection.on('error',function (err) { | |
console.log('Mongoose default connection error: ' + err); | |
}); | |
// When the connection is disconnected | |
mongoose.connection.on('disconnected', function () { | |
console.log('Mongoose default connection disconnected'); | |
}); | |
// When the connection is open | |
mongoose.connection.on('open', function () { | |
console.log('Mongoose default connection is open'); | |
}); | |
// If the Node process ends, close the Mongoose connection | |
process.on('SIGINT', function() { | |
mongoose.connection.close(function () { | |
console.log('Mongoose default connection disconnected through app termination'); | |
process.exit(0); | |
}); | |
}); | |
// BRING IN YOUR SCHEMAS & MODELS | |
require('models/indexSchemas'); | |
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
// Dependencies | |
var fs = require('fs') | |
, inflection = require('inflection') | |
, mongoose = require('mongoose'); | |
// Lookup models directory | |
fs.readdirSync(__dirname).forEach(function (file){ | |
var filePath = __dirname + '/models/' + file; | |
// Match all .js files but this | |
if (fs.statSync(filePath).isFile() && file != 'index.js' && /.*.js/.test(file)) { | |
// Inflect the model name | |
var modelName = inflection.camelize(file.replace('.js', '').replace('-', '_')); | |
// Load the model | |
var modelSchema = require(filePath)[modelName + 'Schema']; | |
if (typeof modelSchema != 'undefined') { | |
mongoose.model(modelName, modelSchema); | |
console.log('Loaded model "%s" from file "%s"', modelName, file); | |
} | |
else { | |
console.error('Schema for model "%s" not found in file "%s"', modelName, file); | |
} | |
}; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment