Remove "loopback.autoAttach()"

The method was deprecated since LoopBack 2.0, there is no need to keep
it around in 3.0.
This commit is contained in:
Miroslav Bajtoš 2016-01-25 10:36:51 +01:00 committed by Miroslav Bajtoš
parent 0e6db30640
commit 87bbf4502a
9 changed files with 20 additions and 171 deletions

View File

@ -41,25 +41,6 @@ module.exports = function(registry) {
require('../common/models/checkpoint.json'),
require('../common/models/checkpoint.js'));
/*!
* Automatically attach these models to dataSources
*/
var dataSourceTypes = {
DB: 'db',
MAIL: 'mail',
};
registry.Email.autoAttach = dataSourceTypes.MAIL;
registry.getModel('PersistedModel').autoAttach = dataSourceTypes.DB;
registry.User.autoAttach = dataSourceTypes.DB;
registry.AccessToken.autoAttach = dataSourceTypes.DB;
registry.Role.autoAttach = dataSourceTypes.DB;
registry.RoleMapping.autoAttach = dataSourceTypes.DB;
registry.ACL.autoAttach = dataSourceTypes.DB;
registry.Scope.autoAttach = dataSourceTypes.DB;
registry.Application.autoAttach = dataSourceTypes.DB;
function createModel(definitionJson, customizeFn) {
var Model = registry.createModel(definitionJson);
customizeFn(Model);

View File

@ -363,43 +363,6 @@ loopback.createDataSource = function(name, options) {
loopback.memory = function(name) {
return this.registry.memory.apply(this.registry, arguments);
};
/**
* Set the default `dataSource` for a given `type`.
* @param {String} type The datasource type.
* @param {Object|DataSource} dataSource The data source settings or instance
* @returns {DataSource} The data source instance.
*
* @header loopback.setDefaultDataSourceForType(type, dataSource)
*/
loopback.setDefaultDataSourceForType = function(type, dataSource) {
return this.registry.setDefaultDataSourceForType.apply(this.registry, arguments);
};
/**
* Get the default `dataSource` for a given `type`.
* @param {String} type The datasource type.
* @returns {DataSource} The data source instance
*/
loopback.getDefaultDataSourceForType = function(type) {
return this.registry.getDefaultDataSourceForType.apply(this.registry, arguments);
};
/**
* Attach any model that does not have a dataSource to
* the default dataSource for the type the Model requests
*/
loopback.autoAttach = function() {
return this.registry.autoAttach.apply(this.registry, arguments);
};
loopback.autoAttachModel = function(ModelCtor) {
return this.registry.autoAttachModel.apply(this.registry, arguments);
};
/*!
* Built in models / services
*/

View File

@ -1533,8 +1533,7 @@ module.exports = function(registry) {
attachRelatedModels(this);
}
// We have to attach related model whenever the datasource changes,
// this is a workaround for autoAttach called by loopback.createModel
// Re-attach related models whenever our datasource is changed.
var self = this;
this.on('dataSourceAttached', function() {
attachRelatedModels(self);

View File

@ -118,11 +118,6 @@ Registry.prototype.createModel = function(name, properties, options) {
this._defineRemoteMethods(model, options.methods);
// try to attach
try {
this.autoAttachModel(model);
} catch (e) {}
return model;
};
@ -368,7 +363,8 @@ Registry.prototype.createDataSource = function(name, options) {
};
if (ds.settings && ds.settings.defaultForType) {
this.setDefaultDataSourceForType(ds.settings.defaultForType, ds);
var msg = 'DataSource option "defaultForType" is no longer supported';
throw new Error(msg);
}
return ds;
@ -395,66 +391,3 @@ Registry.prototype.memory = function(name) {
return memory;
};
/**
* Set the default `dataSource` for a given `type`.
* @param {String} type The datasource type.
* @param {Object|DataSource} dataSource The data source settings or instance
* @returns {DataSource} The data source instance.
*
* @header loopback.setDefaultDataSourceForType(type, dataSource)
*/
Registry.prototype.setDefaultDataSourceForType = function(type, dataSource) {
var defaultDataSources = this.defaultDataSources;
if (!(dataSource instanceof DataSource)) {
dataSource = this.createDataSource(dataSource);
}
defaultDataSources[type] = dataSource;
return dataSource;
};
/**
* Get the default `dataSource` for a given `type`.
* @param {String} type The datasource type.
* @returns {DataSource} The data source instance
*/
Registry.prototype.getDefaultDataSourceForType = function(type) {
return this.defaultDataSources && this.defaultDataSources[type];
};
/**
* Attach any model that does not have a dataSource to
* the default dataSource for the type the Model requests
*/
Registry.prototype.autoAttach = function() {
var models = this.modelBuilder.models;
assert.equal(typeof models, 'object', 'Cannot autoAttach without a models object');
Object.keys(models).forEach(function(modelName) {
var ModelCtor = models[modelName];
// Only auto attach if the model doesn't have an explicit data source
if (ModelCtor && (!(ModelCtor.dataSource instanceof DataSource))) {
this.autoAttachModel(ModelCtor);
}
}, this);
};
Registry.prototype.autoAttachModel = function(ModelCtor) {
if (ModelCtor.autoAttach) {
var ds = this.getDefaultDataSourceForType(ModelCtor.autoAttach);
assert(
ds instanceof DataSource,
'cannot autoAttach model "' + ModelCtor.modelName +
'". No dataSource found of type ' + ModelCtor.autoAttach
);
ModelCtor.attachTo(ds);
}
};

View File

@ -37,7 +37,11 @@ describe('Email connector', function() {
describe('Email and SMTP', function() {
beforeEach(function() {
MyEmail = loopback.Email.extend('my-email');
loopback.autoAttach();
var ds = loopback.createDataSource('email', {
connector: loopback.Mail,
transports: [{ type: 'STUB' }],
});
MyEmail.attachTo(ds);
});
it('should have a send method', function() {

View File

@ -24,7 +24,6 @@ describe('loopback application', function() {
function setupAppWithStreamingMethod() {
app.dataSource('db', {
connector: loopback.Memory,
defaultForType: 'db',
});
var db = app.datasources.db;

View File

@ -53,8 +53,6 @@ describe('loopback', function() {
'ValidationError',
'application',
'arguments',
'autoAttach',
'autoAttachModel',
'bodyParser',
'caller',
'compress',
@ -73,7 +71,6 @@ describe('loopback', function() {
'faviconFile',
'findModel',
'getCurrentContext',
'getDefaultDataSourceForType',
'getModel',
'getModelByType',
'isBrowser',
@ -96,7 +93,6 @@ describe('loopback', function() {
'rest',
'runInContext',
'session',
'setDefaultDataSourceForType',
'static',
'status',
'template',
@ -158,32 +154,6 @@ describe('loopback', function() {
});
});
describe('loopback.autoAttach', function() {
it('doesn\'t overwrite model with datasource configured', function() {
var ds1 = loopback.createDataSource('db1', {
connector: loopback.Memory,
});
// setup default data sources
loopback.setDefaultDataSourceForType('db', ds1);
var ds2 = loopback.createDataSource('db2', {
connector: loopback.Memory,
});
var model1 = ds2.createModel('m1', {});
var model2 = loopback.createModel('m2');
model2.autoAttach = 'db';
// auto attach data sources to models
loopback.autoAttach();
assert(model1.dataSource === ds2);
assert(model2.dataSource === ds1);
});
});
describe('loopback.remoteMethod(Model, fn, [options]);', function() {
it('Setup a remote method.', function() {
var Product = loopback.createModel('product', { price: Number });
@ -370,6 +340,14 @@ describe('loopback', function() {
var db = loopback.createDataSource({ connector: loopback.Memory });
var model = loopback.Model.extend(uniqueModelName);
// This test used to work because User model was already attached
// by other tests via `loopback.autoAttach()`
// Now that autoAttach is gone, it turns out the tested functionality
// does not work exactly as intended. To keep this change narrowly
// focused on removing autoAttach, we are attaching the User model
// to simulate the old test setup.
loopback.User.attachTo(db);
loopback.configureModel(model, {
dataSource: db,
relations: {

View File

@ -5,6 +5,10 @@ var Application = loopback.Application;
describe('Application', function() {
var registeredApp = null;
before(function attachToMemory() {
Application.attachTo(loopback.memory());
});
it('honors `application.register` - promise variant', function(done) {
Application.register('rfeng', 'MyTestApp',
{ description: 'My test application' }, function(err, result) {

View File

@ -18,18 +18,6 @@ loopback.User.settings.saltWorkFactor = 4;
beforeEach(function() {
this.app = app = loopback();
// setup default data sources
loopback.setDefaultDataSourceForType('db', {
connector: loopback.Memory,
});
loopback.setDefaultDataSourceForType('mail', {
connector: loopback.Mail,
transports: [
{ type: 'STUB' },
],
});
});
assertValidDataSource = function(dataSource) {