diff --git a/lib/executor.js b/lib/executor.js index f68d9b5..a767dd4 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -170,17 +170,28 @@ function tryRequire(modulePath) { } } -// Deprecated, will be removed soon function autoAttach(app) { - try { - app.loopback.autoAttach(); - } catch(e) { - if(e.name === 'AssertionError') { - console.warn(e); + var loopback = app.loopback; + var modelBuilder = loopback.modelBuilder || loopback.Model.modelBuilder; + var models = modelBuilder.models || {}; + + Object.keys(models).forEach(function(modelName) { + var ModelCtor = models[modelName]; + if (!ModelCtor || !ModelCtor.autoAttach) return; + + // Do not auto attach models with an explicit data source + if (ModelCtor.dataSource instanceof loopback.DataSource) return; + + var ds = loopback.getDefaultDataSourceForType(ModelCtor.autoAttach); + if (ds instanceof loopback.DataSource) { + ModelCtor.attachTo(ds); } else { - throw e; + console.warn( + 'Cannot autoAttach model "%s". No dataSource found of type %s.', + ModelCtor.modelName, + ModelCtor.autoAttach); } - } + }); } function runBootScripts(app, instructions) { diff --git a/test/browser.test.js b/test/browser.test.js index c2c61cd..e1e18db 100644 --- a/test/browser.test.js +++ b/test/browser.test.js @@ -7,6 +7,8 @@ var sandbox = require('./helpers/sandbox'); var vm = require('vm'); describe('browser support', function() { + this.timeout(10000); // 10s for Jenkins + it('has API for bundling and executing boot instructions', function(done) { var appDir = path.resolve(__dirname, './fixtures/browser-app'); @@ -64,6 +66,9 @@ function createBrowserLikeContext() { Int32Array: Int32Array, DataView: DataView, + // used by Memory connector + setTimeout: setTimeout, + // allow the browserified code to log messages // call `printContextLogs(context)` to print the accumulated messages console: { diff --git a/test/executor.test.js b/test/executor.test.js index 1c64e95..bcca462 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -67,6 +67,11 @@ describe('executor', function() { assert(app.dataSources.TheDb); }); + it('performs auto-attach', function() { + boot.execute(app, dummyInstructions); + assertValidDataSource(loopback.getModel('Email').dataSource); + }); + describe('with boot and models files', function() { beforeEach(function() { boot.execute(app, simpleAppInstructions()); diff --git a/test/fixtures/browser-app/datasources.json b/test/fixtures/browser-app/datasources.json new file mode 100644 index 0000000..7eb483d --- /dev/null +++ b/test/fixtures/browser-app/datasources.json @@ -0,0 +1,10 @@ +{ + "db": { + "connector": "memory", + "defaultForType": "db" + }, + "email": { + "connector": "memory", + "defaultForType": "mail" + } +}