From acf1868ba4bae95c48fd92bcd154cccb1d17232e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Thu, 19 Mar 2015 08:54:49 +0100 Subject: [PATCH] Support per-application registry of models --- lib/executor.js | 9 +++++---- test/executor.test.js | 26 +++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/executor.js b/lib/executor.js index a0d322a..5251d3f 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -168,23 +168,24 @@ function setupModels(app, instructions) { } function defineModels(app, instructions) { + var registry = app.registry || app.loopback; instructions.models.forEach(function(data) { var name = data.name; var model; if (!data.definition) { - model = app.loopback.getModel(name); + model = registry.getModel(name); if (!model) { throw new Error('Cannot configure unknown model ' + name); } debug('Configuring existing model %s', name); } else if (isBuiltinLoopBackModel(app, data)) { - model = app.loopback[name]; - assert(model, 'app.loopback[model] should have been defined'); + model = registry.getModel(name); + assert(model, 'Built-in model ' + name + ' should have been defined'); debug('Configuring built-in LoopBack model %s', name); } else { debug('Creating new model %s %j', name, data.definition); - model = app.loopback.createModel(data.definition); + model = registry.createModel(data.definition); if (data.sourceFile) { debug('Loading customization script %s', data.sourceFile); var code = require(data.sourceFile); diff --git a/test/executor.test.js b/test/executor.test.js index 4ae0a81..631de58 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -84,8 +84,8 @@ describe('executor', function() { boot.execute(app, dummyInstructions); assert(app.models); assert(app.models.User); - assert.equal(app.models.User, loopback.User, - 'Boot should not have extended loopback.User model'); + assert.equal(app.models.User, app.registry.getModel('User'), + 'Boot should not have extended built-in User model'); assertValidDataSource(app.models.User.dataSource); assert.isFunc(app.models.User, 'find'); assert.isFunc(app.models.User, 'create'); @@ -114,7 +114,8 @@ describe('executor', function() { expect(app.models.Customer).to.exist(); expect(app.models.Customer.settings._customized).to.be.equal('Customer'); - expect(loopback.User.settings._customized).to.equal('Base'); + var UserModel = app.registry.getModel('User'); + expect(UserModel.settings._customized).to.equal('Base'); }); it('defines model without attaching it', function() { @@ -177,6 +178,25 @@ describe('executor', function() { expect(app.models.Customer._modelsWhenAttached).to.include('UniqueName'); }); + it('defines models in the local app registry', function() { + app = loopback({ localRegistry: true }); + boot.execute(app, someInstructions({ + models: [ + { + name: 'LocalCustomer', + config: { dataSource: 'db' }, + definition: { name: 'LocalCustomer' }, + sourceFile: undefined + } + ] + })); + + expect(Object.keys(loopback.registry.modelBuilder.models), 'global models') + .to.not.contain('LocalCustomer'); + expect(Object.keys(app.registry.modelBuilder.models), 'local models') + .to.contain('LocalCustomer'); + }); + it('throws on bad require() call inside boot script', function() { var file = appdir.writeFileSync('boot/badScript.js', 'require("doesnt-exist"); module.exports = {};');