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

Commit ba9a2f65 by Eike Lurz Committed by Pedro Augusto de Paula Barbosa

fix(types): adds 'hooks' to CreateOptions (#11736)

1 parent 91153938
import { ColumnOptions, Model, ModelCtor } from '../model'; import { ColumnOptions, Model, ModelCtor, Hookable } from '../model';
export abstract class Association<S extends Model = Model, T extends Model = Model> { export abstract class Association<S extends Model = Model, T extends Model = Model> {
public associationType: string; public associationType: string;
...@@ -42,17 +42,7 @@ export interface ForeignKeyOptions extends ColumnOptions { ...@@ -42,17 +42,7 @@ export interface ForeignKeyOptions extends ColumnOptions {
/** /**
* Options provided when associating models * Options provided when associating models
*/ */
export interface AssociationOptions { export interface AssociationOptions extends Hookable {
/**
* Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade.
* For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks
* for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking
* any hooks.
*
* @default false
*/
hooks?: boolean;
/** /**
* The alias of this model, in singular form. See also the `name` option passed to `sequelize.define`. If * The alias of this model, in singular form. See also the `name` option passed to `sequelize.define`. If
* you create multiple associations between the same tables, you should provide an alias to be able to * you create multiple associations between the same tables, you should provide an alias to be able to
......
export interface ValidationOptions { import { Hookable } from "./model";
export interface ValidationOptions extends Hookable {
/** /**
* An array of strings. All properties that are in this array will not be validated * An array of strings. All properties that are in this array will not be validated
*/ */
...@@ -7,9 +9,4 @@ export interface ValidationOptions { ...@@ -7,9 +9,4 @@ export interface ValidationOptions {
* An array of strings. Only the properties that are in this array will be validated * An array of strings. Only the properties that are in this array will be validated
*/ */
fields?: string[]; fields?: string[];
/**
* Run before and after validate hooks.
* @default true.
*/
hooks?: boolean;
} }
...@@ -154,7 +154,7 @@ export interface WhereOperators { ...@@ -154,7 +154,7 @@ export interface WhereOperators {
[Op.ne]?: string | number | Literal | WhereOperators; [Op.ne]?: string | number | Literal | WhereOperators;
/** Example: `[Op.not]: true,` becomes `IS NOT TRUE` */ /** Example: `[Op.not]: true,` becomes `IS NOT TRUE` */
[Op.not]?: boolean | string | number | Literal | WhereOperators; [Op.not]?: boolean | string | number | Literal | WhereOperators;
/** Example: `[Op.between]: [6, 10],` becomes `BETWEEN 6 AND 10` */ /** Example: `[Op.between]: [6, 10],` becomes `BETWEEN 6 AND 10` */
[Op.between]?: [number, number]; [Op.between]?: [number, number];
...@@ -479,13 +479,13 @@ export type ProjectionAlias = [string | Literal | Fn, string]; ...@@ -479,13 +479,13 @@ export type ProjectionAlias = [string | Literal | Fn, string];
export type FindAttributeOptions = export type FindAttributeOptions =
| (string | ProjectionAlias)[] | (string | ProjectionAlias)[]
| { | {
exclude: string[]; exclude: string[];
include?: (string | ProjectionAlias)[]; include?: (string | ProjectionAlias)[];
} }
| { | {
exclude?: string[]; exclude?: string[];
include: (string | ProjectionAlias)[]; include: (string | ProjectionAlias)[];
}; };
export interface IndexHint { export interface IndexHint {
type: IndexHints; type: IndexHints;
...@@ -545,9 +545,9 @@ export interface FindOptions extends QueryOptions, Filterable, Projectable, Para ...@@ -545,9 +545,9 @@ export interface FindOptions extends QueryOptions, Filterable, Projectable, Para
* locks with joins. See [transaction.LOCK for an example](transaction#lock) * locks with joins. See [transaction.LOCK for an example](transaction#lock)
*/ */
lock?: lock?:
| LOCK | LOCK
| { level: LOCK; of: typeof Model } | { level: LOCK; of: typeof Model }
| boolean; | boolean;
/** /**
* Skip locked rows. Only supported in Postgres. * Skip locked rows. Only supported in Postgres.
*/ */
...@@ -615,7 +615,7 @@ export interface CountWithOptions extends CountOptions { ...@@ -615,7 +615,7 @@ export interface CountWithOptions extends CountOptions {
group: GroupOption; group: GroupOption;
} }
export interface FindAndCountOptions extends CountOptions, FindOptions {} export interface FindAndCountOptions extends CountOptions, FindOptions { }
/** /**
* Options for Model.build method * Options for Model.build method
...@@ -651,7 +651,7 @@ export interface Silent { ...@@ -651,7 +651,7 @@ export interface Silent {
/** /**
* Options for Model.create method * Options for Model.create method
*/ */
export interface CreateOptions extends BuildOptions, Logging, Silent, Transactionable { export interface CreateOptions extends BuildOptions, Logging, Silent, Transactionable, Hookable {
/** /**
* If set, only columns matching those in fields will be saved * If set, only columns matching those in fields will be saved
*/ */
...@@ -668,6 +668,17 @@ export interface CreateOptions extends BuildOptions, Logging, Silent, Transactio ...@@ -668,6 +668,17 @@ export interface CreateOptions extends BuildOptions, Logging, Silent, Transactio
* @default true * @default true
*/ */
validate?: boolean; validate?: boolean;
}
export interface Hookable {
/**
* If `false` the applicable hooks will not be called.
* The default value depends on the context.
*/
hooks?: boolean
} }
/** /**
...@@ -688,18 +699,13 @@ export interface FindOrCreateOptions extends Logging, Transactionable { ...@@ -688,18 +699,13 @@ export interface FindOrCreateOptions extends Logging, Transactionable {
/** /**
* Options for Model.upsert method * Options for Model.upsert method
*/ */
export interface UpsertOptions extends Logging, Transactionable, SearchPathable { export interface UpsertOptions extends Logging, Transactionable, SearchPathable, Hookable {
/** /**
* The fields to insert / update. Defaults to all fields * The fields to insert / update. Defaults to all fields
*/ */
fields?: string[]; fields?: string[];
/** /**
* Run before / after bulk create hooks?
*/
hooks?: boolean;
/**
* Return the affected rows (only for postgres) * Return the affected rows (only for postgres)
*/ */
returning?: boolean; returning?: boolean;
...@@ -713,7 +719,7 @@ export interface UpsertOptions extends Logging, Transactionable, SearchPathable ...@@ -713,7 +719,7 @@ export interface UpsertOptions extends Logging, Transactionable, SearchPathable
/** /**
* Options for Model.bulkCreate method * Options for Model.bulkCreate method
*/ */
export interface BulkCreateOptions extends Logging, Transactionable { export interface BulkCreateOptions extends Logging, Transactionable, Hookable {
/** /**
* Fields to insert (defaults to all fields) * Fields to insert (defaults to all fields)
*/ */
...@@ -726,11 +732,6 @@ export interface BulkCreateOptions extends Logging, Transactionable { ...@@ -726,11 +732,6 @@ export interface BulkCreateOptions extends Logging, Transactionable {
validate?: boolean; validate?: boolean;
/** /**
* Run before / after bulk create hooks?
*/
hooks?: boolean;
/**
* Run before / after create hooks for each individual Instance? BulkCreate hooks will still be run if * Run before / after create hooks for each individual Instance? BulkCreate hooks will still be run if
* options.hooks is true. * options.hooks is true.
*/ */
...@@ -763,7 +764,7 @@ export interface BulkCreateOptions extends Logging, Transactionable { ...@@ -763,7 +764,7 @@ export interface BulkCreateOptions extends Logging, Transactionable {
/** /**
* The options passed to Model.destroy in addition to truncate * The options passed to Model.destroy in addition to truncate
*/ */
export interface TruncateOptions extends Logging, Transactionable, Filterable { export interface TruncateOptions extends Logging, Transactionable, Filterable, Hookable {
/** /**
* Only used in conjuction with TRUNCATE. Truncates all tables that have foreign-key references to the * Only used in conjuction with TRUNCATE. Truncates all tables that have foreign-key references to the
* named table, or to any tables added to the group due to CASCADE. * named table, or to any tables added to the group due to CASCADE.
...@@ -773,11 +774,6 @@ export interface TruncateOptions extends Logging, Transactionable, Filterable { ...@@ -773,11 +774,6 @@ export interface TruncateOptions extends Logging, Transactionable, Filterable {
cascade?: boolean; cascade?: boolean;
/** /**
* Run before / after bulk destroy hooks?
*/
hooks?: boolean;
/**
* If set to true, destroy will SELECT all records matching the where parameter and will execute before / * If set to true, destroy will SELECT all records matching the where parameter and will execute before /
* after destroy hooks on each row * after destroy hooks on each row
*/ */
...@@ -814,11 +810,7 @@ export interface DestroyOptions extends TruncateOptions { ...@@ -814,11 +810,7 @@ export interface DestroyOptions extends TruncateOptions {
/** /**
* Options for Model.restore * Options for Model.restore
*/ */
export interface RestoreOptions extends Logging, Transactionable, Filterable { export interface RestoreOptions extends Logging, Transactionable, Filterable, Hookable {
/**
* Run before / after bulk restore hooks?
*/
hooks?: boolean;
/** /**
* If set to true, restore will find all records within the where parameter and will execute before / after * If set to true, restore will find all records within the where parameter and will execute before / after
...@@ -835,7 +827,7 @@ export interface RestoreOptions extends Logging, Transactionable, Filterable { ...@@ -835,7 +827,7 @@ export interface RestoreOptions extends Logging, Transactionable, Filterable {
/** /**
* Options used for Model.update * Options used for Model.update
*/ */
export interface UpdateOptions extends Logging, Transactionable, Paranoid { export interface UpdateOptions extends Logging, Transactionable, Paranoid, Hookable {
/** /**
* Options to describe the scope of the search. * Options to describe the scope of the search.
*/ */
...@@ -855,13 +847,6 @@ export interface UpdateOptions extends Logging, Transactionable, Paranoid { ...@@ -855,13 +847,6 @@ export interface UpdateOptions extends Logging, Transactionable, Paranoid {
validate?: boolean; validate?: boolean;
/** /**
* Run before / after bulk update hooks?
*
* @default true
*/
hooks?: boolean;
/**
* Whether or not to update the side effects of any virtual setters. * Whether or not to update the side effects of any virtual setters.
* *
* @default true * @default true
...@@ -913,7 +898,7 @@ export interface AggregateOptions<T extends DataType | unknown> extends QueryOpt ...@@ -913,7 +898,7 @@ export interface AggregateOptions<T extends DataType | unknown> extends QueryOpt
/** /**
* Options used for Instance.increment method * Options used for Instance.increment method
*/ */
export interface IncrementDecrementOptions extends Logging, Transactionable, Silent, SearchPathable, Filterable {} export interface IncrementDecrementOptions extends Logging, Transactionable, Silent, SearchPathable, Filterable { }
/** /**
* Options used for Instance.increment method * Options used for Instance.increment method
...@@ -930,7 +915,7 @@ export interface IncrementDecrementOptionsWithBy extends IncrementDecrementOptio ...@@ -930,7 +915,7 @@ export interface IncrementDecrementOptionsWithBy extends IncrementDecrementOptio
/** /**
* Options used for Instance.restore method * Options used for Instance.restore method
*/ */
export interface InstanceRestoreOptions extends Logging, Transactionable {} export interface InstanceRestoreOptions extends Logging, Transactionable { }
/** /**
* Options used for Instance.destroy method * Options used for Instance.destroy method
...@@ -945,7 +930,7 @@ export interface InstanceDestroyOptions extends Logging, Transactionable { ...@@ -945,7 +930,7 @@ export interface InstanceDestroyOptions extends Logging, Transactionable {
/** /**
* Options used for Instance.update method * Options used for Instance.update method
*/ */
export interface InstanceUpdateOptions extends SaveOptions, SetOptions, Filterable {} export interface InstanceUpdateOptions extends SaveOptions, SetOptions, Filterable { }
/** /**
* Options used for Instance.set method * Options used for Instance.set method
...@@ -1512,7 +1497,7 @@ export interface ModelOptions<M extends Model = Model> { ...@@ -1512,7 +1497,7 @@ export interface ModelOptions<M extends Model = Model> {
/** /**
* Options passed to [[Model.init]] * Options passed to [[Model.init]]
*/ */
export interface InitOptions<M extends Model = Model> extends ModelOptions<M> { export interface InitOptions<M extends Model = Model> extends ModelOptions<M> {
/** /**
* The sequelize connection. Required ATM. * The sequelize connection. Required ATM.
*/ */
...@@ -1639,10 +1624,10 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1639,10 +1624,10 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param options * @param options
*/ */
public static schema<M extends Model>( public static schema<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
schema: string, schema: string,
options?: SchemaOptions options?: SchemaOptions
): { new (): M } & typeof Model; ): { new(): M } & typeof Model;
/** /**
* Get the tablename of the model, taking schema into account. The method will return The name as a string * Get the tablename of the model, taking schema into account. The method will return The name as a string
...@@ -1708,7 +1693,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1708,7 +1693,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @return Model A reference to the model, with the scope(s) applied. Calling scope again on the returned * @return Model A reference to the model, with the scope(s) applied. Calling scope again on the returned
* model will clear the previous scope. * model will clear the previous scope.
*/ */
public static scope<M extends { new (): Model }>( public static scope<M extends { new(): Model }>(
this: M, this: M,
options?: string | ScopeOptions | (string | ScopeOptions)[] | WhereAttributeHash options?: string | ScopeOptions | (string | ScopeOptions)[] | WhereAttributeHash
): M; ): M;
...@@ -1786,7 +1771,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1786,7 +1771,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* *
* @see {Sequelize#query} * @see {Sequelize#query}
*/ */
public static findAll<M extends Model>(this: { new (): M } & typeof Model, options?: FindOptions): Promise<M[]>; public static findAll<M extends Model>(this: { new(): M } & typeof Model, options?: FindOptions): Promise<M[]>;
/** /**
* Search for a single instance by its primary key. This applies LIMIT 1, so the listener will * Search for a single instance by its primary key. This applies LIMIT 1, so the listener will
...@@ -1809,7 +1794,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1809,7 +1794,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
*/ */
public static findOne<M extends Model>(this: { new (): M } & typeof Model, options: NonNullFindOptions): Promise<M>; public static findOne<M extends Model>(this: { new (): M } & typeof Model, options: NonNullFindOptions): Promise<M>;
public static findOne<M extends Model>( public static findOne<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
options?: FindOptions options?: FindOptions
): Promise<M | null>; ): Promise<M | null>;
...@@ -1823,7 +1808,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1823,7 +1808,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* which case the complete data result is returned. * which case the complete data result is returned.
*/ */
public static aggregate<M extends Model, T extends DataType | unknown>( public static aggregate<M extends Model, T extends DataType | unknown>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
field: keyof M, field: keyof M,
aggregateFunction: string, aggregateFunction: string,
options?: AggregateOptions<T> options?: AggregateOptions<T>
...@@ -1877,7 +1862,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1877,7 +1862,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* profiles will be counted * profiles will be counted
*/ */
public static findAndCountAll<M extends Model>( public static findAndCountAll<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
options?: FindAndCountOptions options?: FindAndCountOptions
): Promise<{ rows: M[]; count: number }>; ): Promise<{ rows: M[]; count: number }>;
...@@ -1885,7 +1870,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1885,7 +1870,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* Find the maximum value of field * Find the maximum value of field
*/ */
public static max<M extends Model, T extends DataType | unknown>( public static max<M extends Model, T extends DataType | unknown>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
field: keyof M, field: keyof M,
options?: AggregateOptions<T> options?: AggregateOptions<T>
): Promise<T>; ): Promise<T>;
...@@ -1894,7 +1879,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1894,7 +1879,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* Find the minimum value of field * Find the minimum value of field
*/ */
public static min<M extends Model, T extends DataType | unknown>( public static min<M extends Model, T extends DataType | unknown>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
field: keyof M, field: keyof M,
options?: AggregateOptions<T> options?: AggregateOptions<T>
): Promise<T>; ): Promise<T>;
...@@ -1903,7 +1888,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1903,7 +1888,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* Find the sum of field * Find the sum of field
*/ */
public static sum<M extends Model, T extends DataType | unknown>( public static sum<M extends Model, T extends DataType | unknown>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
field: keyof M, field: keyof M,
options?: AggregateOptions<T> options?: AggregateOptions<T>
): Promise<number>; ): Promise<number>;
...@@ -1912,7 +1897,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1912,7 +1897,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* Builds a new model instance. Values is an object of key value pairs, must be defined but can be empty. * Builds a new model instance. Values is an object of key value pairs, must be defined but can be empty.
*/ */
public static build<M extends Model>( public static build<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
record?: object, record?: object,
options?: BuildOptions options?: BuildOptions
): M; ): M;
...@@ -1921,7 +1906,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1921,7 +1906,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* Undocumented bulkBuild * Undocumented bulkBuild
*/ */
public static bulkBuild<M extends Model>( public static bulkBuild<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
records: object[], records: object[],
options?: BuildOptions options?: BuildOptions
): M[]; ): M[];
...@@ -1930,7 +1915,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1930,7 +1915,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* Builds a new model instance and calls save on it. * Builds a new model instance and calls save on it.
*/ */
public static create<M extends Model>( public static create<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
values?: object, values?: object,
options?: CreateOptions options?: CreateOptions
): Promise<M>; ): Promise<M>;
...@@ -1941,7 +1926,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1941,7 +1926,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* The successfull result of the promise will be (instance, initialized) - Make sure to use `.then(([...]))` * The successfull result of the promise will be (instance, initialized) - Make sure to use `.then(([...]))`
*/ */
public static findOrBuild<M extends Model>( public static findOrBuild<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
options: FindOrCreateOptions options: FindOrCreateOptions
): Promise<[M, boolean]>; ): Promise<[M, boolean]>;
...@@ -1957,7 +1942,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1957,7 +1942,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* will be created instead, and any unique constraint violation will be handled internally. * will be created instead, and any unique constraint violation will be handled internally.
*/ */
public static findOrCreate<M extends Model>( public static findOrCreate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
options: FindOrCreateOptions options: FindOrCreateOptions
): Promise<[M, boolean]>; ): Promise<[M, boolean]>;
...@@ -1966,7 +1951,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1966,7 +1951,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* Will execute a find call, if empty then attempt to create, if unique constraint then attempt to find again * Will execute a find call, if empty then attempt to create, if unique constraint then attempt to find again
*/ */
public static findCreateFind<M extends Model>( public static findCreateFind<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
options: FindOrCreateOptions options: FindOrCreateOptions
): Promise<[M, boolean]>; ): Promise<[M, boolean]>;
...@@ -1990,16 +1975,16 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -1990,16 +1975,16 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* whether the row was inserted or not. * whether the row was inserted or not.
*/ */
public static upsert<M extends Model>( public static upsert<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
values: object, values: object,
options?: UpsertOptions & { returning?: false | undefined } options?: UpsertOptions & { returning?: false | undefined }
): Promise<boolean>; ): Promise<boolean>;
public static upsert<M extends Model> ( public static upsert<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
values: object, values: object,
options?: UpsertOptions & { returning: true } options?: UpsertOptions & { returning: true }
): Promise<[ M, boolean ]>; ): Promise<[M, boolean]>;
/** /**
* Create and insert multiple instances in bulk. * Create and insert multiple instances in bulk.
...@@ -2013,7 +1998,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2013,7 +1998,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param records List of objects (key/value pairs) to create instances from * @param records List of objects (key/value pairs) to create instances from
*/ */
public static bulkCreate<M extends Model>( public static bulkCreate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
records: object[], records: object[],
options?: BulkCreateOptions options?: BulkCreateOptions
): Promise<M[]>; ): Promise<M[]>;
...@@ -2041,7 +2026,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2041,7 +2026,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* affected rows (only supported in postgres with `options.returning` true.) * affected rows (only supported in postgres with `options.returning` true.)
*/ */
public static update<M extends Model>( public static update<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
values: object, values: object,
options: UpdateOptions options: UpdateOptions
): Promise<[number, M[]]>; ): Promise<[number, M[]]>;
...@@ -2050,7 +2035,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2050,7 +2035,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* Increments a single field. * Increments a single field.
*/ */
public static increment<M extends Model, K extends keyof M>( public static increment<M extends Model, K extends keyof M>(
this: { new (): M }, this: { new(): M },
field: K, field: K,
options: IncrementDecrementOptionsWithBy options: IncrementDecrementOptionsWithBy
): Promise<M>; ): Promise<M>;
...@@ -2059,7 +2044,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2059,7 +2044,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* Increments multiple fields by the same value. * Increments multiple fields by the same value.
*/ */
public static increment<M extends Model, K extends keyof M>( public static increment<M extends Model, K extends keyof M>(
this: { new (): M }, this: { new(): M },
fields: K[], fields: K[],
options: IncrementDecrementOptionsWithBy options: IncrementDecrementOptionsWithBy
): Promise<M>; ): Promise<M>;
...@@ -2068,7 +2053,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2068,7 +2053,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* Increments multiple fields by different values. * Increments multiple fields by different values.
*/ */
public static increment<M extends Model, K extends keyof M>( public static increment<M extends Model, K extends keyof M>(
this: { new (): M }, this: { new(): M },
fields: { [key in K]?: number }, fields: { [key in K]?: number },
options: IncrementDecrementOptions options: IncrementDecrementOptions
): Promise<M>; ): Promise<M>;
...@@ -2091,12 +2076,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2091,12 +2076,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instance, options * @param fn A callback function that is called with instance, options
*/ */
public static beforeValidate<M extends Model>( public static beforeValidate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instance: M, options: ValidationOptions) => HookReturn fn: (instance: M, options: ValidationOptions) => HookReturn
): void; ): void;
public static beforeValidate<M extends Model>( public static beforeValidate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instance: M, options: ValidationOptions) => HookReturn fn: (instance: M, options: ValidationOptions) => HookReturn
): void; ): void;
...@@ -2107,12 +2092,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2107,12 +2092,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instance, options * @param fn A callback function that is called with instance, options
*/ */
public static afterValidate<M extends Model>( public static afterValidate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instance: M, options: ValidationOptions) => HookReturn fn: (instance: M, options: ValidationOptions) => HookReturn
): void; ): void;
public static afterValidate<M extends Model>( public static afterValidate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instance: M, options: ValidationOptions) => HookReturn fn: (instance: M, options: ValidationOptions) => HookReturn
): void; ): void;
...@@ -2123,12 +2108,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2123,12 +2108,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with attributes, options * @param fn A callback function that is called with attributes, options
*/ */
public static beforeCreate<M extends Model>( public static beforeCreate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (attributes: M, options: CreateOptions) => HookReturn fn: (attributes: M, options: CreateOptions) => HookReturn
): void; ): void;
public static beforeCreate<M extends Model>( public static beforeCreate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (attributes: M, options: CreateOptions) => HookReturn fn: (attributes: M, options: CreateOptions) => HookReturn
): void; ): void;
...@@ -2139,12 +2124,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2139,12 +2124,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with attributes, options * @param fn A callback function that is called with attributes, options
*/ */
public static afterCreate<M extends Model>( public static afterCreate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (attributes: M, options: CreateOptions) => HookReturn fn: (attributes: M, options: CreateOptions) => HookReturn
): void; ): void;
public static afterCreate<M extends Model>( public static afterCreate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (attributes: M, options: CreateOptions) => HookReturn fn: (attributes: M, options: CreateOptions) => HookReturn
): void; ): void;
...@@ -2155,12 +2140,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2155,12 +2140,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instance, options * @param fn A callback function that is called with instance, options
*/ */
public static beforeDestroy<M extends Model>( public static beforeDestroy<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instance: M, options: InstanceDestroyOptions) => HookReturn fn: (instance: M, options: InstanceDestroyOptions) => HookReturn
): void; ): void;
public static beforeDestroy<M extends Model>( public static beforeDestroy<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instance: M, options: InstanceDestroyOptions) => HookReturn fn: (instance: M, options: InstanceDestroyOptions) => HookReturn
): void; ): void;
...@@ -2171,12 +2156,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2171,12 +2156,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instance, options * @param fn A callback function that is called with instance, options
*/ */
public static afterDestroy<M extends Model>( public static afterDestroy<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instance: M, options: InstanceDestroyOptions) => HookReturn fn: (instance: M, options: InstanceDestroyOptions) => HookReturn
): void; ): void;
public static afterDestroy<M extends Model>( public static afterDestroy<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instance: M, options: InstanceDestroyOptions) => HookReturn fn: (instance: M, options: InstanceDestroyOptions) => HookReturn
): void; ): void;
...@@ -2187,12 +2172,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2187,12 +2172,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instance, options * @param fn A callback function that is called with instance, options
*/ */
public static beforeUpdate<M extends Model>( public static beforeUpdate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instance: M, options: UpdateOptions) => HookReturn fn: (instance: M, options: UpdateOptions) => HookReturn
): void; ): void;
public static beforeUpdate<M extends Model>( public static beforeUpdate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instance: M, options: UpdateOptions) => HookReturn fn: (instance: M, options: UpdateOptions) => HookReturn
): void; ): void;
...@@ -2203,12 +2188,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2203,12 +2188,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instance, options * @param fn A callback function that is called with instance, options
*/ */
public static afterUpdate<M extends Model>( public static afterUpdate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instance: M, options: UpdateOptions) => HookReturn fn: (instance: M, options: UpdateOptions) => HookReturn
): void; ): void;
public static afterUpdate<M extends Model>( public static afterUpdate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instance: M, options: UpdateOptions) => HookReturn fn: (instance: M, options: UpdateOptions) => HookReturn
): void; ): void;
...@@ -2219,12 +2204,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2219,12 +2204,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instance, options * @param fn A callback function that is called with instance, options
*/ */
public static beforeSave<M extends Model>( public static beforeSave<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn
): void; ): void;
public static beforeSave<M extends Model>( public static beforeSave<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn
): void; ): void;
...@@ -2235,12 +2220,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2235,12 +2220,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instance, options * @param fn A callback function that is called with instance, options
*/ */
public static afterSave<M extends Model>( public static afterSave<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn
): void; ): void;
public static afterSave<M extends Model>( public static afterSave<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn
): void; ): void;
...@@ -2251,12 +2236,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2251,12 +2236,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instances, options * @param fn A callback function that is called with instances, options
*/ */
public static beforeBulkCreate<M extends Model>( public static beforeBulkCreate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instances: M[], options: BulkCreateOptions) => HookReturn fn: (instances: M[], options: BulkCreateOptions) => HookReturn
): void; ): void;
public static beforeBulkCreate<M extends Model>( public static beforeBulkCreate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instances: M[], options: BulkCreateOptions) => HookReturn fn: (instances: M[], options: BulkCreateOptions) => HookReturn
): void; ): void;
...@@ -2267,12 +2252,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2267,12 +2252,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instances, options * @param fn A callback function that is called with instances, options
*/ */
public static afterBulkCreate<M extends Model>( public static afterBulkCreate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instances: M[], options: BulkCreateOptions) => HookReturn fn: (instances: M[], options: BulkCreateOptions) => HookReturn
): void; ): void;
public static afterBulkCreate<M extends Model>( public static afterBulkCreate<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instances: M[], options: BulkCreateOptions) => HookReturn fn: (instances: M[], options: BulkCreateOptions) => HookReturn
): void; ): void;
...@@ -2355,12 +2340,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2355,12 +2340,12 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* @param fn A callback function that is called with instance(s), options * @param fn A callback function that is called with instance(s), options
*/ */
public static afterFind<M extends Model>( public static afterFind<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
name: string, name: string,
fn: (instancesOrInstance: M[] | M | null, options: FindOptions) => HookReturn fn: (instancesOrInstance: M[] | M | null, options: FindOptions) => HookReturn
): void; ): void;
public static afterFind<M extends Model>( public static afterFind<M extends Model>(
this: { new (): M } & typeof Model, this: { new(): M } & typeof Model,
fn: (instancesOrInstance: M[] | M | null, options: FindOptions) => HookReturn fn: (instancesOrInstance: M[] | M | null, options: FindOptions) => HookReturn
): void; ): void;
...@@ -2743,6 +2728,6 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2743,6 +2728,6 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
export type ModelType = typeof Model; export type ModelType = typeof Model;
export type ModelCtor<M extends Model> = { new (): M } & ModelType; export type ModelCtor<M extends Model> = { new(): M } & ModelType;
export default Model; export default Model;
...@@ -20,6 +20,7 @@ import { ...@@ -20,6 +20,7 @@ import {
WhereAttributeHash, WhereAttributeHash,
WhereOperators, WhereOperators,
ModelCtor, ModelCtor,
Hookable,
} from './model'; } from './model';
import { ModelManager } from './model-manager'; import { ModelManager } from './model-manager';
import * as Op from './operators'; import * as Op from './operators';
...@@ -46,7 +47,7 @@ export interface SyncAlterOptions { ...@@ -46,7 +47,7 @@ export interface SyncAlterOptions {
/** /**
* Sync Options * Sync Options
*/ */
export interface SyncOptions extends Logging { export interface SyncOptions extends Logging, Hookable {
/** /**
* If force is true, each DAO will do DROP TABLE IF EXISTS ..., before it tries to create its own table * If force is true, each DAO will do DROP TABLE IF EXISTS ..., before it tries to create its own table
*/ */
...@@ -74,13 +75,9 @@ export interface SyncOptions extends Logging { ...@@ -74,13 +75,9 @@ export interface SyncOptions extends Logging {
*/ */
searchPath?: string; searchPath?: string;
/**
* If hooks is true then beforeSync, afterSync, beforeBulkSync, afterBulkSync hooks will be called
*/
hooks?: boolean;
} }
export interface DefaultSetOptions {} export interface DefaultSetOptions { }
/** /**
* Connection Pool options * Connection Pool options
...@@ -170,7 +167,7 @@ export interface Config { ...@@ -170,7 +167,7 @@ export interface Config {
}; };
} }
export type Dialect = 'mysql' | 'postgres' | 'sqlite' | 'mariadb' | 'mssql'; export type Dialect = 'mysql' | 'postgres' | 'sqlite' | 'mariadb' | 'mssql';
export interface RetryOptions { export interface RetryOptions {
match?: (RegExp | string | Function)[]; match?: (RegExp | string | Function)[];
...@@ -380,7 +377,7 @@ export interface Options extends Logging { ...@@ -380,7 +377,7 @@ export interface Options extends Logging {
retry?: RetryOptions; retry?: RetryOptions;
} }
export interface QueryOptionsTransactionRequired {} export interface QueryOptionsTransactionRequired { }
/** /**
* This is the main class, the entry point to sequelize. To use it, you just need to * This is the main class, the entry point to sequelize. To use it, you just need to
...@@ -1080,7 +1077,7 @@ export class Sequelize extends Hooks { ...@@ -1080,7 +1077,7 @@ export class Sequelize extends Hooks {
* Returns the database name. * Returns the database name.
*/ */
public getDatabaseName() : string; public getDatabaseName(): string;
/** /**
* Returns an instance of QueryInterface. * Returns an instance of QueryInterface.
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!