Skip to content

Instantly share code, notes, and snippets.

@jeduan
Created May 26, 2014 14:56
Show Gist options
  • Save jeduan/8c2a6835a158d7222182 to your computer and use it in GitHub Desktop.
Save jeduan/8c2a6835a158d7222182 to your computer and use it in GitHub Desktop.
db.js
var _ = require('underscore')
var config = require('../../config')
var mongoose = require('mongoose')
mongoose.connect(config.mongoUrl)
var db = mongoose.connection
mongoose.set('debug', !!config.mongoDebug)
db.on('error', function () {
return console.error.bind(console, '[mongo]: ')
})
db.once('open', function () {
console.log('Mongo connected to ' + config.mongoUrl)
})
var models = {},
loadedModels = []
exports.loadModels = function () {
_.each(_.toArray(arguments), function (modelName) {
if (! _.include(loadedModels, modelName)) {
var Model,
exported = require('../../models/' + modelName)
if (typeof exported === 'function') {
Model = exported
} else if (exported.model) {
Model = exported.model
}
if (Model) {
models[Model.modelName] = Model
loadedModels.push(modelName)
}
}
})
}
exports.model = function (name) {
return models[name]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment