Skip to content
Merged
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
10 changes: 4 additions & 6 deletions common/models/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,10 @@ module.exports = function(Role) {
});

function isUserClass(modelClass) {
if (modelClass) {
return modelClass === loopback.User ||
modelClass.prototype instanceof loopback.User;
} else {
return false;
}
if (!modelClass) return false;
var User = modelClass.modelBuilder.models.User;
if (!User) return false;
return modelClass == User || modelClass.prototype instanceof User;
}

/*!
Expand Down
19 changes: 0 additions & 19 deletions lib/builtin-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 0 additions & 37 deletions lib/loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
3 changes: 1 addition & 2 deletions lib/persisted-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
71 changes: 2 additions & 69 deletions lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
};
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,10 @@
"depd": "loopback-datasource-juggler/lib/browser.depd.js",
"bcrypt": false
},
"config": {
"ci": {
"debug": "*,-mocha:*,-eslint:*"
}
},
"license": "MIT"
}
6 changes: 5 additions & 1 deletion test/email.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('loopback application', function() {
function setupAppWithStreamingMethod() {
app.dataSource('db', {
connector: loopback.Memory,
defaultForType: 'db',
});
var db = app.datasources.db;

Expand Down
38 changes: 8 additions & 30 deletions test/loopback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ describe('loopback', function() {
'ValidationError',
'application',
'arguments',
'autoAttach',
'autoAttachModel',
'bodyParser',
'caller',
'compress',
Expand All @@ -73,7 +71,6 @@ describe('loopback', function() {
'faviconFile',
'findModel',
'getCurrentContext',
'getDefaultDataSourceForType',
'getModel',
'getModelByType',
'isBrowser',
Expand All @@ -96,7 +93,6 @@ describe('loopback', function() {
'rest',
'runInContext',
'session',
'setDefaultDataSourceForType',
'static',
'status',
'template',
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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: {
Expand Down
4 changes: 4 additions & 0 deletions test/model.application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading