diff --git a/lib/executor.js b/lib/executor.js index ceef61f..61d38e8 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -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; diff --git a/test/executor.test.js b/test/executor.test.js index 4a63929..c4f248c 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -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 || []; diff --git a/test/fixtures/browser-app/model-config.json b/test/fixtures/browser-app/model-config.json index 3566c55..d895c8a 100644 --- a/test/fixtures/browser-app/model-config.json +++ b/test/fixtures/browser-app/model-config.json @@ -1,4 +1,10 @@ { + "_meta": { + "sources": [ + "./models", + "../../../node_modules/loopback/common/models" + ] + }, "Customer": { "dataSource": "db" }