Created
May 26, 2014 14:56
-
-
Save jeduan/8c2a6835a158d7222182 to your computer and use it in GitHub Desktop.
db.js
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 _ = 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