Merge pull request #1759 from strongloop/feat/reset-datasource
feat: add DataSource.deleteAllModels() API
This commit is contained in:
commit
6478c73dde
|
@ -844,6 +844,16 @@ DataSource.prototype.deleteModelByName = function(modelName) {
|
||||||
delete this.connector._models[modelName];
|
delete this.connector._models[modelName];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all models from the registry, but keep the connector instance
|
||||||
|
* (including the pool of database connections).
|
||||||
|
*/
|
||||||
|
DataSource.prototype.deleteAllModels = function() {
|
||||||
|
for (const m in this.modelBuilder.models) {
|
||||||
|
this.deleteModelByName(m);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mixin DataAccessObject methods.
|
* Mixin DataAccessObject methods.
|
||||||
*
|
*
|
||||||
|
|
|
@ -517,6 +517,34 @@ describe('DataSource', function() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('deleteAllModels', () => {
|
||||||
|
it('removes all model definitions', () => {
|
||||||
|
const ds = new DataSource({connector: 'memory'});
|
||||||
|
ds.define('Category');
|
||||||
|
ds.define('Product');
|
||||||
|
|
||||||
|
Object.keys(ds.modelBuilder.definitions)
|
||||||
|
.should.deepEqual(['Category', 'Product']);
|
||||||
|
Object.keys(ds.modelBuilder.models)
|
||||||
|
.should.deepEqual(['Category', 'Product']);
|
||||||
|
Object.keys(ds.connector._models)
|
||||||
|
.should.deepEqual(['Category', 'Product']);
|
||||||
|
|
||||||
|
ds.deleteAllModels();
|
||||||
|
|
||||||
|
Object.keys(ds.modelBuilder.definitions).should.be.empty();
|
||||||
|
Object.keys(ds.modelBuilder.models).should.be.empty();
|
||||||
|
Object.keys(ds.connector._models).should.be.empty();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('preserves the connector instance', () => {
|
||||||
|
const ds = new DataSource({connector: 'memory'});
|
||||||
|
const connector = ds.connector;
|
||||||
|
ds.deleteAllModels();
|
||||||
|
ds.connector.should.equal(connector);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function givenMockConnector(props) {
|
function givenMockConnector(props) {
|
||||||
|
|
|
@ -110,6 +110,19 @@ export declare class DataSource extends EventEmitter {
|
||||||
|
|
||||||
getModel(modelName: string): ModelBaseClass | undefined;
|
getModel(modelName: string): ModelBaseClass | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a model from the registry.
|
||||||
|
*
|
||||||
|
* @param modelName
|
||||||
|
*/
|
||||||
|
deleteModelByName(modelName: string): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all models from the registry, but keep the connector instance
|
||||||
|
* (including the pool of database connections).
|
||||||
|
*/
|
||||||
|
deleteAllModels(): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attach an existing model to a data source.
|
* Attach an existing model to a data source.
|
||||||
* This will mixin all of the data access object functions (DAO) into your
|
* This will mixin all of the data access object functions (DAO) into your
|
||||||
|
|
Loading…
Reference in New Issue