Skip to content

Commit

Permalink
feat: backport findModel to v6 (#16705)
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos Bardasano <cbardasano@alaudaingenieria.es>
Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 12, 2023
1 parent 6c03176 commit 5bfbb99
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/model-manager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class ModelManager {
public addModel<T extends ModelType>(model: T): T;
public removeModel(model: ModelType): void;
public getModel(against: unknown, options?: { attribute?: string }): typeof Model;
public findModel(callback: (model: typeof Model) => boolean): typeof Model | undefined

/**
* Returns an array that lists every model, sorted in order
Expand Down
4 changes: 4 additions & 0 deletions src/model-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class ModelManager {
return this.models.find(model => model[options.attribute] === against);
}

findModel(callback) {
return this.models.find(callback);
}

get all() {
return this.models;
}
Expand Down
11 changes: 11 additions & 0 deletions test/integration/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
});
});

describe('modelManager', () => {
it('allows to find a model using a callback', function() {
const project = this.sequelize.define('Project', {
name: DataTypes.STRING
});

const model = this.sequelize.modelManager.findModel(m => m.name.toLowerCase() === 'project');
expect(model).to.equal(project);
});
});

describe('set', () => {
it('should be configurable with global functions', function() {
const defaultSetterMethod = sinon.spy(),
Expand Down

0 comments on commit 5bfbb99

Please sign in to comment.