import { ModelType } from '../index'; import { ValidationOptions } from './instance-validator'; import Model, { BulkCreateOptions, CountOptions, CreateOptions, DestroyOptions, FindOptions, InstanceDestroyOptions, InstanceRestoreOptions, InstanceUpdateOptions, ModelAttributes, ModelOptions, RestoreOptions, UpdateOptions, UpsertOptions } from './model'; import { AbstractQuery } from './query'; import { QueryOptions } from './query-interface'; import { Config, Options, Sequelize, SyncOptions } from './sequelize'; import { DeepWriteable } from './utils'; 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 = any, TCreationAttributes = 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; 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. */ _attributes: TModelAttributes; /** * A similar dummy variable that doesn't exist on the real object. Do not * try to access this in real code. */ _creationAttributes: TCreationAttributes; /** * 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 >( this: HooksStatic, hookType: K, name: string, fn: SequelizeHooks[K] ): HooksCtor; public static addHook< H extends Hooks, K extends keyof SequelizeHooks >( this: HooksStatic, hookType: K, fn: SequelizeHooks[K] ): HooksCtor; /** * Remove hook from the model */ public static removeHook( this: HooksStatic, hookType: keyof SequelizeHooks, name: string, ): HooksCtor; /** * Check whether the mode has any hooks of this type */ public static hasHook( this: HooksStatic, hookType: keyof SequelizeHooks, ): boolean; public static hasHooks( this: HooksStatic, hookType: keyof SequelizeHooks, ): 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 };