Skip definitions of built-in loopback models
LoopBack built-in models are special: they follow the loopback-boot structure and provide `common/models/{name}.json` files, but they are also automatically loaded (created) by loopback. This change modifies `executor` to recognize built-in models and do not redefine them.
This commit is contained in:
parent
011296d825
commit
26abb43ad4
|
@ -156,6 +156,10 @@ function defineModels(app, instructions) {
|
|||
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');
|
||||
debug('Configuring built-in LoopBack model %s', name);
|
||||
} else {
|
||||
debug('Creating new model %s %j', name, data.definition);
|
||||
model = app.loopback.createModel(data.definition);
|
||||
|
@ -176,6 +180,15 @@ function defineModels(app, instructions) {
|
|||
});
|
||||
}
|
||||
|
||||
function isBuiltinLoopBackModel(app, data) {
|
||||
// 1. Built-in models are exposed on the loopback object
|
||||
if (!app.loopback[data.name]) return false;
|
||||
|
||||
// 2. Built-in models have a script file `loopback/{facet}/models/{name}.js`
|
||||
return data.sourceFile &&
|
||||
/node_modules\/loopback\/[^\/]+\/models\/[^\/]+\.js$/.test(data.sourceFile);
|
||||
}
|
||||
|
||||
function forEachKeyedObject(obj, fn) {
|
||||
if(typeof obj !== 'object') return;
|
||||
|
||||
|
|
|
@ -160,6 +160,22 @@ describe('executor', function() {
|
|||
expect(actual).to.equal('not attached');
|
||||
});
|
||||
|
||||
it('skips definition of already defined LoopBack models', function() {
|
||||
var builtinModel = {
|
||||
name: 'User',
|
||||
definition: fs.readJsonFileSync(
|
||||
require.resolve('loopback/common/models/user.json')
|
||||
),
|
||||
config: { dataSource: 'db' },
|
||||
sourceFile: require.resolve('loopback/common/models/user.js')
|
||||
};
|
||||
builtinModel.definition.redefined = true;
|
||||
|
||||
boot.execute(app, someInstructions({ models: [ builtinModel ] }));
|
||||
|
||||
expect(app.models.User.settings.redefined, 'redefined').to.not.equal(true);
|
||||
});
|
||||
|
||||
describe('with boot and models files', function() {
|
||||
beforeEach(function() {
|
||||
process.bootFlags = process.bootFlags || [];
|
||||
|
|
|
@ -1,4 +1,10 @@
|
|||
{
|
||||
"_meta": {
|
||||
"sources": [
|
||||
"./models",
|
||||
"../../../node_modules/loopback/common/models"
|
||||
]
|
||||
},
|
||||
"Customer": {
|
||||
"dataSource": "db"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue