feat: remove a Model from all registries

Add API allowing consumers (e.g. LoopBack) to remove a Model from all
juggler registries:
 - ModelBuilder's models
 - ModelBuilder's definitions
 - Connector registry of models
This commit is contained in:
Miroslav Bajtoš 2018-04-16 13:28:17 +02:00
parent 01427b1755
commit 5af4c42728
No known key found for this signature in database
GPG Key ID: 6F2304BA9361C7E3
3 changed files with 52 additions and 0 deletions

View File

@ -800,6 +800,16 @@ DataSource.prototype.define = function defineClass(className, properties, settin
return modelClass;
};
/**
* Remove a model from the registry.
*
* @param {String} modelName
*/
DataSource.prototype.deleteModelByName = function(modelName) {
this.modelBuilder.deleteModelByName(modelName);
delete this.connector._models[modelName];
};
/**
* Mixin DataAccessObject methods.
*

View File

@ -806,6 +806,16 @@ ModelBuilder.prototype.copyModel = function copyModel(Master) {
return Slave;
};
/**
* Remove a model from the registry.
*
* @param {String} modelName
*/
ModelBuilder.prototype.deleteModelByName = function(modelName) {
delete this.models[modelName];
delete this.definitions[modelName];
};
/*!
* Define hidden property
*/

View File

@ -154,4 +154,36 @@ describe('DataSource', function() {
dataSource.name.should.equal('myDataSource');
dataSource.connector.should.equal(mockConnector);
});
describe('deleteModelByName()', () => {
it('removes the model from ModelBuilder registry', () => {
const ds = new DataSource('ds', {connector: 'memory'});
ds.createModel('TestModel');
Object.keys(ds.modelBuilder.models)
.should.containEql('TestModel');
Object.keys(ds.modelBuilder.definitions)
.should.containEql('TestModel');
ds.deleteModelByName('TestModel');
Object.keys(ds.modelBuilder.models)
.should.not.containEql('TestModel');
Object.keys(ds.modelBuilder.definitions)
.should.not.containEql('TestModel');
});
it('removes the model from connector registry', () => {
const ds = new DataSource('ds', {connector: 'memory'});
ds.createModel('TestModel');
Object.keys(ds.connector._models)
.should.containEql('TestModel');
ds.deleteModelByName('TestModel');
Object.keys(ds.connector._models)
.should.not.containEql('TestModel');
});
});
});