import { ValidationOptions } from './instance-validator'; import Model, { BulkCreateOptions, CountOptions, CreateOptions, DestroyOptions, FindOptions, InstanceDestroyOptions, InstanceRestoreOptions, InstanceUpdateOptions, ModelAttributes, ModelOptions, RestoreOptions, UpdateOptions, UpsertOptions, Attributes, CreationAttributes, ModelType } from './model'; import { AbstractQuery } from './dialects/abstract/query'; import { QueryOptions } from './dialects/abstract/query-interface'; import { Config, Options, Sequelize, SyncOptions } from './sequelize'; import { DeepWriteable } from './utils'; import { Connection, GetConnectionOptions } from './dialects/abstract/connection-manager'; export type HookReturn = Promise | void; /** * Options for Model.init. We mostly duplicate the Hooks here, since there is no way to combine the two * interfaces. */ export interface ModelHooks { beforeValidate(instance: M, options: ValidationOptions): HookReturn; afterValidate(instance: M, options: ValidationOptions): HookReturn; beforeCreate(attributes: M, options: CreateOptions): HookReturn; afterCreate(attributes: M, options: CreateOptions): HookReturn; beforeDestroy(instance: M, options: InstanceDestroyOptions): HookReturn; afterDestroy(instance: M, options: InstanceDestroyOptions): HookReturn; beforeRestore(instance: M, options: InstanceRestoreOptions): HookReturn; afterRestore(instance: M, options: InstanceRestoreOptions): HookReturn; beforeUpdate(instance: M, options: InstanceUpdateOptions): HookReturn; afterUpdate(instance: M, options: InstanceUpdateOptions): HookReturn; beforeUpsert(attributes: M, options: UpsertOptions): HookReturn; afterUpsert(attributes: [ M, boolean | null ], options: UpsertOptions): HookReturn; beforeSave( instance: M, options: InstanceUpdateOptions | CreateOptions ): HookReturn; afterSave( instance: M, options: InstanceUpdateOptions | CreateOptions ): HookReturn; beforeBulkCreate(instances: M[], options: BulkCreateOptions): HookReturn; afterBulkCreate(instances: readonly M[], options: BulkCreateOptions): HookReturn; beforeBulkDestroy(options: DestroyOptions): HookReturn; afterBulkDestroy(options: DestroyOptions): HookReturn; beforeBulkRestore(options: RestoreOptions): HookReturn; afterBulkRestore(options: RestoreOptions): HookReturn; beforeBulkUpdate(options: UpdateOptions): HookReturn; afterBulkUpdate(options: UpdateOptions): HookReturn; beforeFind(options: FindOptions): HookReturn; beforeCount(options: CountOptions): HookReturn; beforeFindAfterExpandIncludeAll(options: FindOptions): HookReturn; beforeFindAfterOptions(options: FindOptions): HookReturn; afterFind(instancesOrInstance: readonly M[] | M | null, options: FindOptions): HookReturn; beforeSync(options: SyncOptions): HookReturn; afterSync(options: SyncOptions): HookReturn; beforeBulkSync(options: SyncOptions): HookReturn; afterBulkSync(options: SyncOptions): HookReturn; beforeQuery(options: QueryOptions, query: AbstractQuery): HookReturn; afterQuery(options: QueryOptions, query: AbstractQuery): HookReturn; } export interface SequelizeHooks< M extends Model = Model, TAttributes extends {} = any, TCreationAttributes extends {} = TAttributes > extends ModelHooks { beforeDefine(attributes: ModelAttributes, options: ModelOptions): void; afterDefine(model: ModelType): void; beforeInit(config: Config, options: Options): void; afterInit(sequelize: Sequelize): void; beforeConnect(config: DeepWriteable): HookReturn; afterConnect(connection: unknown, config: Config): HookReturn; beforePoolAcquire(config: GetConnectionOptions): HookReturn; afterPoolAcquire(connection: Connection, config: GetConnectionOptions): HookReturn; beforeDisconnect(connection: unknown): HookReturn; afterDisconnect(connection: unknown): HookReturn; } /** * Virtual class for deduplication */ export class Hooks< M extends Model = Model, TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes > { /** * A dummy variable that doesn't exist on the real object. This exists so * Typescript can infer the type of the attributes in static functions. Don't * try to access this! */ _model: M; /** * A similar dummy variable that doesn't exist on the real object. Do not * try to access this in real code. * * @deprecated This property will become a Symbol in v7 to prevent collisions. * Use Attributes instead of this property to be forward-compatible. */ _attributes: TModelAttributes; // TODO [>6]: make this a non-exported symbol (same as the one in model.d.ts) /** * A similar dummy variable that doesn't exist on the real object. Do not * try to access this in real code. * * @deprecated This property will become a Symbol in v7 to prevent collisions. * Use CreationAttributes instead of this property to be forward-compatible. */ _creationAttributes: TCreationAttributes; // TODO [>6]: make this a non-exported symbol (same as the one in model.d.ts) /** * Add a hook to the model * * @param name Provide a name for the hook function. It can be used to remove the hook later or to order * hooks based on some sort of priority system in the future. */ public static addHook< H extends Hooks, K extends keyof SequelizeHooks, CreationAttributes> >( this: HooksStatic, hookType: K, name: string, fn: SequelizeHooks, CreationAttributes>[K] ): HooksCtor; public static addHook< H extends Hooks, K extends keyof SequelizeHooks, CreationAttributes> >( this: HooksStatic, hookType: K, fn: SequelizeHooks, CreationAttributes>[K] ): HooksCtor; /** * Remove hook from the model */ public static removeHook( this: HooksStatic, hookType: keyof SequelizeHooks, CreationAttributes>, name: string, ): HooksCtor; /** * Check whether the mode has any hooks of this type */ public static hasHook( this: HooksStatic, hookType: keyof SequelizeHooks, CreationAttributes>, ): boolean; public static hasHooks( this: HooksStatic, hookType: keyof SequelizeHooks, CreationAttributes>, ): boolean; /** * Add a hook to the model * * @param name Provide a name for the hook function. It can be used to remove the hook later or to order * hooks based on some sort of priority system in the future. */ public addHook>( hookType: K, name: string, fn: SequelizeHooks[K] ): this; public addHook>( hookType: K, fn: SequelizeHooks[K]): this; /** * Remove hook from the model */ public removeHook>( hookType: K, name: string ): this; /** * Check whether the mode has any hooks of this type */ public hasHook>(hookType: K): boolean; public hasHooks>(hookType: K): boolean; } export type HooksCtor = typeof Hooks & { new(): H }; export type HooksStatic = { new(): H };