Support per-application registry of models

This commit is contained in:
Miroslav Bajtoš 2015-03-19 08:54:49 +01:00
parent 1b9f137062
commit acf1868ba4
2 changed files with 28 additions and 7 deletions

View File

@ -168,23 +168,24 @@ function setupModels(app, instructions) {
} }
function defineModels(app, instructions) { function defineModels(app, instructions) {
var registry = app.registry || app.loopback;
instructions.models.forEach(function(data) { instructions.models.forEach(function(data) {
var name = data.name; var name = data.name;
var model; var model;
if (!data.definition) { if (!data.definition) {
model = app.loopback.getModel(name); model = registry.getModel(name);
if (!model) { if (!model) {
throw new Error('Cannot configure unknown model ' + name); throw new Error('Cannot configure unknown model ' + name);
} }
debug('Configuring existing model %s', name); debug('Configuring existing model %s', name);
} else if (isBuiltinLoopBackModel(app, data)) { } else if (isBuiltinLoopBackModel(app, data)) {
model = app.loopback[name]; model = registry.getModel(name);
assert(model, 'app.loopback[model] should have been defined'); assert(model, 'Built-in model ' + name + ' should have been defined');
debug('Configuring built-in LoopBack model %s', name); debug('Configuring built-in LoopBack model %s', name);
} else { } else {
debug('Creating new model %s %j', name, data.definition); debug('Creating new model %s %j', name, data.definition);
model = app.loopback.createModel(data.definition); model = registry.createModel(data.definition);
if (data.sourceFile) { if (data.sourceFile) {
debug('Loading customization script %s', data.sourceFile); debug('Loading customization script %s', data.sourceFile);
var code = require(data.sourceFile); var code = require(data.sourceFile);

View File

@ -84,8 +84,8 @@ describe('executor', function() {
boot.execute(app, dummyInstructions); boot.execute(app, dummyInstructions);
assert(app.models); assert(app.models);
assert(app.models.User); assert(app.models.User);
assert.equal(app.models.User, loopback.User, assert.equal(app.models.User, app.registry.getModel('User'),
'Boot should not have extended loopback.User model'); 'Boot should not have extended built-in User model');
assertValidDataSource(app.models.User.dataSource); assertValidDataSource(app.models.User.dataSource);
assert.isFunc(app.models.User, 'find'); assert.isFunc(app.models.User, 'find');
assert.isFunc(app.models.User, 'create'); assert.isFunc(app.models.User, 'create');
@ -114,7 +114,8 @@ describe('executor', function() {
expect(app.models.Customer).to.exist(); expect(app.models.Customer).to.exist();
expect(app.models.Customer.settings._customized).to.be.equal('Customer'); 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() { it('defines model without attaching it', function() {
@ -177,6 +178,25 @@ describe('executor', function() {
expect(app.models.Customer._modelsWhenAttached).to.include('UniqueName'); 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() { it('throws on bad require() call inside boot script', function() {
var file = appdir.writeFileSync('boot/badScript.js', var file = appdir.writeFileSync('boot/badScript.js',
'require("doesnt-exist"); module.exports = {};'); 'require("doesnt-exist"); module.exports = {};');