Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add DataSource.deleteAllModels() API #1759

Merged
merged 1 commit into from
Jul 18, 2019
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: 10 additions & 0 deletions lib/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,16 @@ DataSource.prototype.deleteModelByName = function(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.
*
Expand Down
28 changes: 28 additions & 0 deletions test/datasource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions types/datasource.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ export declare class DataSource extends EventEmitter {

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.
* This will mixin all of the data access object functions (DAO) into your
Expand Down