不要怂,就是干,撸起袖子干!

Commit 5767fbac by Mike Fowler Committed by Simon Schick

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 bf4700b0
Showing with 27 additions and 1 deletions
...@@ -1709,7 +1709,16 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1709,7 +1709,16 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
options?: string | ScopeOptions | (string | ScopeOptions)[] | WhereAttributeHash options?: string | ScopeOptions | (string | ScopeOptions)[] | WhereAttributeHash
): M; ): M;
/**
* Add a new scope to the model
*
* This is especially useful for adding scopes with includes, when the model you want to
* include is not available at the time this model is defined. By default this will throw an
* error if a scope with that name already exists. Pass `override: true` in the options
* object to silence this error.
*/
public static addScope(name: string, scope: FindOptions, options?: AddScopeOptions): void; public static addScope(name: string, scope: FindOptions, options?: AddScopeOptions): void;
public static addScope(name: string, scope: (...args: any[]) => FindOptions, options?: AddScopeOptions): void;
/** /**
* Search for multiple instances. * Search for multiple instances.
......
...@@ -6,7 +6,8 @@ import { ...@@ -6,7 +6,8 @@ import {
DataTypes, DataTypes,
FindOptions, FindOptions,
Model, Model,
ModelCtor ModelCtor,
Op
} from 'sequelize'; } from 'sequelize';
import { sequelize } from '../connection'; import { sequelize } from '../connection';
...@@ -74,6 +75,22 @@ User.addHook('beforeFind', 'test', (options: FindOptions) => { ...@@ -74,6 +75,22 @@ User.addHook('beforeFind', 'test', (options: FindOptions) => {
return undefined; return undefined;
}); });
// Model#addScope
User.addScope('withoutFirstName', {
where: {
firstName: {
[Op.is]: null,
},
},
});
User.addScope(
'withFirstName',
(firstName: string) => ({
where: { firstName },
}),
);
// associate // associate
// it is important to import _after_ the model above is already exported so the circular reference works. // it is important to import _after_ the model above is already exported so the circular reference works.
import { UserGroup } from './UserGroup'; import { UserGroup } from './UserGroup';
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!