From 7318acb75e63c309480516810fd1136bf1fc9913 Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Wed, 1 May 2013 12:34:31 -0700 Subject: [PATCH] Merge in models and data sources built from config to the app. --- lib/middleware/configure.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/middleware/configure.js b/lib/middleware/configure.js index ba5d7a0a..28083e5a 100644 --- a/lib/middleware/configure.js +++ b/lib/middleware/configure.js @@ -17,12 +17,32 @@ module.exports = configure; function configure(root) { var moduleLoader = configure.createModuleLoader(root); + var app = this; process.__asteroidCache = {}; return function configureMiddleware(req, res, next) { - req.modules = res.modules = moduleLoader; - moduleLoader.load(next); + var modules = req.modules = res.modules = moduleLoader; + moduleLoader.load(function (err) { + if(err) { + next(err); + } else { + var models = modules.instanceOf('ModelConfiguration'); + var dataSources = modules.instanceOf('DataSource'); + + // define models from config + models.forEach(function (model) { + app.models[model.options.name] = model.ModelCtor; + }); + + // define data sources from config + dataSources.forEach(function (dataSource) { + app.dataSources[dataSources.options.name] = dataSource; + }); + + next(); + } + }); } }