Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions lib/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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: {
Expand Down
5 changes: 5 additions & 0 deletions test/executor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/browser-app/datasources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"db": {
"connector": "memory",
"defaultForType": "db"
},
"email": {
"connector": "memory",
"defaultForType": "mail"
}
}