diff --git a/lib/application.js b/lib/application.js index d59cdc2e..8a93d8ff 100644 --- a/lib/application.js +++ b/lib/application.js @@ -221,8 +221,16 @@ app.boot = function(options) { app.model(key, obj); }); - // attach models to dataSources by type - loopback.autoAttach(); + // try to attach models to dataSources by type + try { + require('./loopback').autoAttach(); + } catch(e) { + if(e.name === 'AssertionError') { + console.warn(e); + } else { + throw e; + } + } // require directories var requiredModels = requireDir(path.join(appRootDir, 'models')); diff --git a/lib/loopback.js b/lib/loopback.js index 5204ec21..a8b77c42 100644 --- a/lib/loopback.js +++ b/lib/loopback.js @@ -97,8 +97,8 @@ loopback.createDataSource = function (name, options) { return ModelCtor; }; - if(options && options.defaultForType) { - loopback.setDefaultDataSourceForType(options.defaultForType, ds); + if(ds.settings && ds.settings.defaultForType) { + loopback.setDefaultDataSourceForType(ds.settings.defaultForType, ds); } return ds; @@ -116,7 +116,9 @@ loopback.createModel = function (name, properties, options) { var model = loopback.Model.extend(name, properties, options); // try to attach - loopback.autoAttachModel(model); + try { + loopback.autoAttachModel(model); + } catch(e) {} return model; } @@ -228,8 +230,9 @@ loopback.autoAttachModel = function(ModelCtor) { if(ModelCtor.autoAttach) { var ds = loopback.getDefaultDataSourceForType(ModelCtor.autoAttach); - assert(ds instanceof DataSource, 'cannot autoAttach model "' + ModelCtor.modelName - + '". No dataSource found of type ' + ModelCtor.attachTo); + assert(ds instanceof DataSource, 'cannot autoAttach model "' + + ModelCtor.modelName + + '". No dataSource found of type ' + ModelCtor.autoAttach); ModelCtor.attachTo(ds); }