From c2fe17c82c0c6900e462fecc347a34c4c6c351e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 21 Jul 2014 19:36:33 +0200 Subject: [PATCH 1/2] executor: implement `autoAttach` from loopback --- lib/executor.js | 27 +++++++++++++++------- test/browser.test.js | 3 +++ test/executor.test.js | 5 ++++ test/fixtures/browser-app/datasources.json | 10 ++++++++ 4 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/browser-app/datasources.json 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..cdf7818 100644 --- a/test/browser.test.js +++ b/test/browser.test.js @@ -64,6 +64,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" + } +} From f200867be48143e814653c2d4ecf85cf9cedd4dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 21 Jul 2014 20:31:37 +0200 Subject: [PATCH 2/2] test: increase test timeout This should prevent timeout failures on Jenkins CI. --- test/browser.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/browser.test.js b/test/browser.test.js index cdf7818..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');