diff --git a/lib/datasource.js b/lib/datasource.js index 71a9ab53..08c82644 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -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. * diff --git a/lib/model-builder.js b/lib/model-builder.js index 62aab2fe..95a7b4d6 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -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 */ diff --git a/test/datasource.test.js b/test/datasource.test.js index b878bbc3..22ab7d47 100644 --- a/test/datasource.test.js +++ b/test/datasource.test.js @@ -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'); + }); + }); });