Merge pull request #112 from strongloop/feature/app-registries
Support per-application registry of models
This commit is contained in:
commit
2e9f001100
|
@ -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);
|
||||
|
|
|
@ -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 = {};');
|
||||
|
|
Loading…
Reference in New Issue