/
hooks.d.ts
189 lines (177 loc) · 7.77 KB
/
hooks.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { Attributes, CreationAttributes, 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> | void;
/**
* Options for Model.init. We mostly duplicate the Hooks here, since there is no way to combine the two
* interfaces.
*/
export interface ModelHooks<M extends Model = Model, TAttributes = any> {
beforeValidate(instance: M, options: ValidationOptions): HookReturn;
afterValidate(instance: M, options: ValidationOptions): HookReturn;
beforeCreate(attributes: M, options: CreateOptions<TAttributes>): HookReturn;
afterCreate(attributes: M, options: CreateOptions<TAttributes>): 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<TAttributes>): HookReturn;
afterUpdate(instance: M, options: InstanceUpdateOptions<TAttributes>): HookReturn;
beforeUpsert(attributes: M, options: UpsertOptions<TAttributes>): HookReturn;
afterUpsert(attributes: [ M, boolean | null ], options: UpsertOptions<TAttributes>): HookReturn;
beforeSave(
instance: M,
options: InstanceUpdateOptions<TAttributes> | CreateOptions<TAttributes>
): HookReturn;
afterSave(
instance: M,
options: InstanceUpdateOptions<TAttributes> | CreateOptions<TAttributes>
): HookReturn;
beforeBulkCreate(instances: M[], options: BulkCreateOptions<TAttributes>): HookReturn;
afterBulkCreate(instances: readonly M[], options: BulkCreateOptions<TAttributes>): HookReturn;
beforeBulkDestroy(options: DestroyOptions<TAttributes>): HookReturn;
afterBulkDestroy(options: DestroyOptions<TAttributes>): HookReturn;
beforeBulkRestore(options: RestoreOptions<TAttributes>): HookReturn;
afterBulkRestore(options: RestoreOptions<TAttributes>): HookReturn;
beforeBulkUpdate(options: UpdateOptions<TAttributes>): HookReturn;
afterBulkUpdate(options: UpdateOptions<TAttributes>): HookReturn;
beforeFind(options: FindOptions<TAttributes>): HookReturn;
beforeCount(options: CountOptions<TAttributes>): HookReturn;
beforeFindAfterExpandIncludeAll(options: FindOptions<TAttributes>): HookReturn;
beforeFindAfterOptions(options: FindOptions<TAttributes>): HookReturn;
afterFind(instancesOrInstance: readonly M[] | M | null, options: FindOptions<TAttributes>): 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<TAttributes, TCreationAttributes> = Model,
TAttributes = any,
TCreationAttributes = TAttributes
> extends ModelHooks<M, TAttributes> {
beforeDefine(attributes: ModelAttributes<M, TCreationAttributes>, options: ModelOptions<M>): void;
afterDefine(model: ModelType): void;
beforeInit(config: Config, options: Options): void;
afterInit(sequelize: Sequelize): void;
beforeConnect(config: DeepWriteable<Config>): 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<TModelAttributes, TCreationAttributes> = 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<Model> 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<Model> 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<H['_model'], Attributes<H>, CreationAttributes<H>>
>(
this: HooksStatic<H>,
hookType: K,
name: string,
fn: SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>[K]
): HooksCtor<H>;
public static addHook<
H extends Hooks,
K extends keyof SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>
>(
this: HooksStatic<H>,
hookType: K,
fn: SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>[K]
): HooksCtor<H>;
/**
* Remove hook from the model
*/
public static removeHook<H extends Hooks>(
this: HooksStatic<H>,
hookType: keyof SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>,
name: string,
): HooksCtor<H>;
/**
* Check whether the mode has any hooks of this type
*/
public static hasHook<H extends Hooks>(
this: HooksStatic<H>,
hookType: keyof SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>,
): boolean;
public static hasHooks<H extends Hooks>(
this: HooksStatic<H>,
hookType: keyof SequelizeHooks<H['_model'], Attributes<H>, CreationAttributes<H>>,
): 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<K extends keyof SequelizeHooks<M, TModelAttributes, TCreationAttributes>>(
hookType: K,
name: string,
fn: SequelizeHooks<Model, TModelAttributes, TCreationAttributes>[K]
): this;
public addHook<K extends keyof SequelizeHooks<M, TModelAttributes, TCreationAttributes>>(
hookType: K, fn: SequelizeHooks<M, TModelAttributes, TCreationAttributes>[K]): this;
/**
* Remove hook from the model
*/
public removeHook<K extends keyof SequelizeHooks<M, TModelAttributes, TCreationAttributes>>(
hookType: K,
name: string
): this;
/**
* Check whether the mode has any hooks of this type
*/
public hasHook<K extends keyof SequelizeHooks<M, TModelAttributes, TCreationAttributes>>(hookType: K): boolean;
public hasHooks<K extends keyof SequelizeHooks<M, TModelAttributes, TCreationAttributes>>(hookType: K): boolean;
}
export type HooksCtor<H extends Hooks> = typeof Hooks & { new(): H };
export type HooksStatic<H extends Hooks> = { new(): H };