Skip to content

Commit 5767fba

Browse files
Mike FowlerSimonSchick
authored andcommitted
fix(types): add Model#addScope overload
fix(types): add Model#addScope overload Adds the overloaded signature for `Model#addScope`, and some JSDocs for the method
1 parent bf4700b commit 5767fba

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

types/lib/model.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,16 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
17091709
options?: string | ScopeOptions | (string | ScopeOptions)[] | WhereAttributeHash
17101710
): M;
17111711

1712+
/**
1713+
* Add a new scope to the model
1714+
*
1715+
* This is especially useful for adding scopes with includes, when the model you want to
1716+
* include is not available at the time this model is defined. By default this will throw an
1717+
* error if a scope with that name already exists. Pass `override: true` in the options
1718+
* object to silence this error.
1719+
*/
17121720
public static addScope(name: string, scope: FindOptions, options?: AddScopeOptions): void;
1721+
public static addScope(name: string, scope: (...args: any[]) => FindOptions, options?: AddScopeOptions): void;
17131722

17141723
/**
17151724
* Search for multiple instances.

types/test/models/User.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
DataTypes,
77
FindOptions,
88
Model,
9-
ModelCtor
9+
ModelCtor,
10+
Op
1011
} from 'sequelize';
1112
import { sequelize } from '../connection';
1213

@@ -74,6 +75,22 @@ User.addHook('beforeFind', 'test', (options: FindOptions) => {
7475
return undefined;
7576
});
7677

78+
// Model#addScope
79+
User.addScope('withoutFirstName', {
80+
where: {
81+
firstName: {
82+
[Op.is]: null,
83+
},
84+
},
85+
});
86+
87+
User.addScope(
88+
'withFirstName',
89+
(firstName: string) => ({
90+
where: { firstName },
91+
}),
92+
);
93+
7794
// associate
7895
// it is important to import _after_ the model above is already exported so the circular reference works.
7996
import { UserGroup } from './UserGroup';

0 commit comments

Comments
 (0)