UNPKG

238 kBTypeScriptView Raw
1// ***************************** IMPORTANT NOTE *****************************
2// These types are for the 4.x branch of Sequelize. As of Sequelize 5.0,
3// types are packaged directly within Sequelize itself. Please target the
4// Sequelize-provided types for any changes related to versions >= 5.0.
5
6// Based on original work by: samuelneff <https://github.com/samuelneff/sequelize-auto-ts/blob/master/lib/sequelize.d.ts>
7
8import * as _ from "lodash";
9import Promise = require("bluebird");
10import * as cls from "continuation-local-storage";
11
12import ValidatorJS = require("validator");
13
14type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
15
16declare namespace sequelize {
17 //
18 // Associations
19 // ~~~~~~~~~~~~~~
20 //
21 // https://github.com/sequelize/sequelize/tree/v3.4.1/lib/associations
22 //
23
24 /**
25 * The options for the getAssociation mixin of the belongsTo association.
26 * @see BelongsToGetAssociationMixin
27 */
28 interface BelongsToGetAssociationMixinOptions {
29 /**
30 * Apply a scope on the related model, or remove its default scope by passing false.
31 */
32 scope?: string | boolean | undefined;
33 }
34
35 /**
36 * The getAssociation mixin applied to models with belongsTo.
37 * An example of usage is as follows:
38 *
39 * ```js
40 *
41 * User.belongsTo(Role);
42 *
43 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttrib>, UserAttrib {
44 * getRole: Sequelize.BelongsToGetAssociationMixin<RoleInstance>;
45 * // setRole...
46 * // createRole...
47 * }
48 * ```
49 *
50 * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/
51 * @see Instance
52 */
53 interface BelongsToGetAssociationMixin<TInstance> {
54 /**
55 * Get the associated instance.
56 * @param options The options to use when getting the association.
57 */
58 (options?: BelongsToGetAssociationMixinOptions): Promise<TInstance | null>;
59 }
60
61 /**
62 * The options for the setAssociation mixin of the belongsTo association.
63 * @see BelongsToSetAssociationMixin
64 */
65 interface BelongsToSetAssociationMixinOptions {
66 /**
67 * Skip saving this after setting the foreign key if false.
68 */
69 save?: boolean | undefined;
70 }
71
72 /**
73 * The setAssociation mixin applied to models with belongsTo.
74 * An example of usage is as follows:
75 *
76 * ```js
77 *
78 * User.belongsTo(Role);
79 *
80 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
81 * // getRole...
82 * setRole: Sequelize.BelongsToSetAssociationMixin<RoleInstance, RoleId>;
83 * // createRole...
84 * }
85 * ```
86 *
87 * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/
88 * @see Instance
89 */
90 interface BelongsToSetAssociationMixin<TInstance, TInstancePrimaryKey> {
91 /**
92 * Set the associated instance.
93 * @param newAssociation An instance or the primary key of an instance to associate with this. Pass null or undefined to remove the association.
94 * @param options the options passed to `this.save`.
95 */
96 (
97 newAssociation?: TInstance | TInstancePrimaryKey,
98 options?: BelongsToSetAssociationMixinOptions | InstanceSaveOptions,
99 ): Promise<void>;
100 }
101
102 /**
103 * The options for the createAssociation mixin of the belongsTo association.
104 * @see BelongsToCreateAssociationMixin
105 */
106 interface BelongsToCreateAssociationMixinOptions {}
107
108 /**
109 * The createAssociation mixin applied to models with belongsTo.
110 * An example of usage is as follows:
111 *
112 * ```js
113 *
114 * User.belongsTo(Role);
115 *
116 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
117 * // getRole...
118 * // setRole...
119 * createRole: Sequelize.BelongsToCreateAssociationMixin<RoleAttributes>;
120 * }
121 * ```
122 *
123 * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/
124 * @see Instance
125 */
126 interface BelongsToCreateAssociationMixin<TAttributes, TInstance> {
127 /**
128 * Create a new instance of the associated model and associate it with this.
129 * @param values The values used to create the association.
130 * @param options The options passed to `target.create` and `setAssociation`.
131 */
132 (
133 values?: TAttributes,
134 options?: BelongsToCreateAssociationMixinOptions | CreateOptions | BelongsToSetAssociationMixinOptions,
135 ): Promise<TInstance>;
136 }
137
138 /**
139 * The options for the getAssociation mixin of the hasOne association.
140 * @see HasOneGetAssociationMixin
141 */
142 interface HasOneGetAssociationMixinOptions {
143 /**
144 * Apply a scope on the related model, or remove its default scope by passing false.
145 */
146 scope?: string | boolean | undefined | null;
147 }
148
149 /**
150 * The getAssociation mixin applied to models with hasOne.
151 * An example of usage is as follows:
152 *
153 * ```js
154 *
155 * User.hasOne(Role);
156 *
157 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttrib>, UserAttrib {
158 * getRole: Sequelize.HasOneGetAssociationMixin<RoleInstance>;
159 * // setRole...
160 * // createRole...
161 * }
162 * ```
163 *
164 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-one/
165 * @see Instance
166 */
167 interface HasOneGetAssociationMixin<TInstance> {
168 /**
169 * Get the associated instance.
170 * @param options The options to use when getting the association.
171 */
172 (options?: HasOneGetAssociationMixinOptions): Promise<TInstance | null>;
173 }
174
175 /**
176 * The options for the setAssociation mixin of the hasOne association.
177 * @see HasOneSetAssociationMixin
178 */
179 interface HasOneSetAssociationMixinOptions {
180 /**
181 * Skip saving this after setting the foreign key if false.
182 */
183 save?: boolean | undefined;
184 }
185
186 /**
187 * The setAssociation mixin applied to models with hasOne.
188 * An example of usage is as follows:
189 *
190 * ```js
191 *
192 * User.hasOne(Role);
193 *
194 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
195 * // getRole...
196 * setRole: Sequelize.HasOneSetAssociationMixin<RoleInstance, RoleId>;
197 * // createRole...
198 * }
199 * ```
200 *
201 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-one/
202 * @see Instance
203 */
204 interface HasOneSetAssociationMixin<TInstance, TInstancePrimaryKey> {
205 /**
206 * Set the associated instance.
207 * @param newAssociation An instance or the primary key of an instance to associate with this. Pass null or undefined to remove the association.
208 * @param options The options passed to `getAssocation` and `target.save`.
209 */
210 (
211 newAssociation?: TInstance | TInstancePrimaryKey,
212 options?: HasOneSetAssociationMixinOptions | HasOneGetAssociationMixinOptions | InstanceSaveOptions,
213 ): Promise<void>;
214 }
215
216 /**
217 * The options for the createAssociation mixin of the hasOne association.
218 * @see HasOneCreateAssociationMixin
219 */
220 interface HasOneCreateAssociationMixinOptions {}
221
222 /**
223 * The createAssociation mixin applied to models with hasOne.
224 * An example of usage is as follows:
225 *
226 * ```js
227 *
228 * User.hasOne(Role);
229 *
230 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
231 * // getRole...
232 * // setRole...
233 * createRole: Sequelize.HasOneCreateAssociationMixin<RoleAttributes>;
234 * }
235 * ```
236 *
237 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-one/
238 * @see Instance
239 */
240 interface HasOneCreateAssociationMixin<TAttributes> {
241 /**
242 * Create a new instance of the associated model and associate it with this.
243 * @param values The values used to create the association.
244 * @param options The options passed to `target.create` and `setAssociation`.
245 */
246 (
247 values?: TAttributes,
248 options?: HasOneCreateAssociationMixinOptions | HasOneSetAssociationMixinOptions | CreateOptions,
249 ): Promise<void>;
250 }
251
252 /**
253 * The options for the getAssociations mixin of the hasMany association.
254 * @see HasManyGetAssociationsMixin
255 */
256 interface HasManyGetAssociationsMixinOptions {
257 /**
258 * An optional where clause to limit the associated models.
259 */
260 where?: AnyWhereOptions | undefined;
261
262 /**
263 * Apply a scope on the related model, or remove its default scope by passing false.
264 */
265 scope?: string | boolean | undefined;
266
267 /**
268 * Load further nested related models
269 */
270 include?: IncludeOptions | undefined;
271 }
272
273 /**
274 * The getAssociations mixin applied to models with hasMany.
275 * An example of usage is as follows:
276 *
277 * ```js
278 *
279 * User.hasMany(Role);
280 *
281 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
282 * getRoles: Sequelize.HasManyGetAssociationsMixin<RoleInstance>;
283 * // setRoles...
284 * // addRoles...
285 * // addRole...
286 * // createRole...
287 * // removeRole...
288 * // removeRoles...
289 * // hasRole...
290 * // hasRoles...
291 * // countRoles...
292 * }
293 * ```
294 *
295 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
296 * @see Instance
297 */
298 interface HasManyGetAssociationsMixin<TInstance> {
299 /**
300 * Get everything currently associated with this, using an optional where clause.
301 * @param options The options to use when getting the associations.
302 */
303 (options?: HasManyGetAssociationsMixinOptions): Promise<TInstance[]>;
304 }
305
306 /**
307 * The options for the setAssociations mixin of the hasMany association.
308 * @see HasManySetAssociationsMixin
309 */
310 interface HasManySetAssociationsMixinOptions {
311 /**
312 * Run validation for the join model.
313 */
314 validate?: boolean | undefined;
315 }
316
317 /**
318 * The setAssociations mixin applied to models with hasMany.
319 * An example of usage is as follows:
320 *
321 * ```js
322 *
323 * User.hasMany(Role);
324 *
325 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
326 * // getRoles...
327 * setRoles: Sequelize.HasManySetAssociationsMixin<RoleInstance, RoleId>;
328 * // addRoles...
329 * // addRole...
330 * // createRole...
331 * // removeRole...
332 * // removeRoles...
333 * // hasRole...
334 * // hasRoles...
335 * // countRoles...
336 * }
337 * ```
338 *
339 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
340 * @see Instance
341 */
342 interface HasManySetAssociationsMixin<TInstance, TInstancePrimaryKey> {
343 /**
344 * Set the associated models by passing an array of instances or their primary keys.
345 * Everything that it not in the passed array will be un-associated.
346 * @param newAssociations An array of instances or primary key of instances to associate with this. Pass null or undefined to remove all associations.
347 * @param options The options passed to `target.findAll` and `update`.
348 */
349 (
350 newAssociations?: Array<TInstance | TInstancePrimaryKey>,
351 options?: HasManySetAssociationsMixinOptions | AnyFindOptions | InstanceUpdateOptions,
352 ): Promise<void>;
353 }
354
355 /**
356 * The options for the addAssociations mixin of the hasMany association.
357 * @see HasManyAddAssociationsMixin
358 */
359 interface HasManyAddAssociationsMixinOptions {
360 /**
361 * Run validation for the join model.
362 */
363 validate?: boolean | undefined;
364 }
365
366 /**
367 * The addAssociations mixin applied to models with hasMany.
368 * An example of usage is as follows:
369 *
370 * ```js
371 *
372 * User.hasMany(Role);
373 *
374 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
375 * // getRoles...
376 * // setRoles...
377 * addRoles: Sequelize.HasManyAddAssociationsMixin<RoleInstance, RoleId>;
378 * // addRole...
379 * // createRole...
380 * // removeRole...
381 * // removeRoles...
382 * // hasRole...
383 * // hasRoles...
384 * // countRoles...
385 * }
386 * ```
387 *
388 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
389 * @see Instance
390 */
391 interface HasManyAddAssociationsMixin<TInstance, TInstancePrimaryKey> {
392 /**
393 * Associate several instances with this.
394 * @param newAssociations An array of instances or primary key of instances to associate with this.
395 * @param options The options passed to `target.update`.
396 */
397 (
398 newAssociations?: Array<TInstance | TInstancePrimaryKey>,
399 options?: HasManyAddAssociationsMixinOptions | InstanceUpdateOptions,
400 ): Promise<void>;
401 }
402
403 /**
404 * The options for the addAssociation mixin of the hasMany association.
405 * @see HasManyAddAssociationMixin
406 */
407 interface HasManyAddAssociationMixinOptions {
408 /**
409 * Run validation for the join model.
410 */
411 validate?: boolean | undefined;
412 }
413
414 /**
415 * The addAssociation mixin applied to models with hasMany.
416 * An example of usage is as follows:
417 *
418 * ```js
419 *
420 * User.hasMany(Role);
421 *
422 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
423 * // getRoles...
424 * // setRoles...
425 * // addRoles...
426 * addRole: Sequelize.HasManyAddAssociationMixin<RoleInstance, RoleId>;
427 * // createRole...
428 * // removeRole...
429 * // removeRoles...
430 * // hasRole...
431 * // hasRoles...
432 * // countRoles...
433 * }
434 * ```
435 *
436 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
437 * @see Instance
438 */
439 interface HasManyAddAssociationMixin<TInstance, TInstancePrimaryKey> {
440 /**
441 * Associate an instance with this.
442 * @param newAssociation An instance or the primary key of an instance to associate with this.
443 * @param options The options passed to `target.update`.
444 */
445 (
446 newAssociation?: TInstance | TInstancePrimaryKey,
447 options?: HasManyAddAssociationMixinOptions | InstanceUpdateOptions,
448 ): Promise<void>;
449 }
450
451 /**
452 * The options for the createAssociation mixin of the hasMany association.
453 * @see HasManyCreateAssociationMixin
454 */
455 interface HasManyCreateAssociationMixinOptions {}
456
457 /**
458 * The createAssociation mixin applied to models with hasMany.
459 * An example of usage is as follows:
460 *
461 * ```js
462 *
463 * User.hasMany(Role);
464 *
465 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
466 * // getRoles...
467 * // setRoles...
468 * // addRoles...
469 * // addRole...
470 * createRole: Sequelize.HasManyCreateAssociationMixin<RoleAttributes>;
471 * // removeRole...
472 * // removeRoles...
473 * // hasRole...
474 * // hasRoles...
475 * // countRoles...
476 * }
477 * ```
478 *
479 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
480 * @see Instance
481 */
482 interface HasManyCreateAssociationMixin<TAttributes, TInstance> {
483 /**
484 * Create a new instance of the associated model and associate it with this.
485 * @param values The values used to create the association.
486 * @param options The options to use when creating the association.
487 */
488 (
489 values?: TAttributes,
490 options?: HasManyCreateAssociationMixinOptions | CreateOptions,
491 ): Promise<TInstance>;
492 }
493
494 /**
495 * The options for the removeAssociation mixin of the hasMany association.
496 * @see HasManyRemoveAssociationMixin
497 */
498 interface HasManyRemoveAssociationMixinOptions {}
499
500 /**
501 * The removeAssociation mixin applied to models with hasMany.
502 * An example of usage is as follows:
503 *
504 * ```js
505 *
506 * User.hasMany(Role);
507 *
508 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
509 * // getRoles...
510 * // setRoles...
511 * // addRoles...
512 * // addRole...
513 * // createRole...
514 * removeRole: Sequelize.HasManyRemoveAssociationMixin<RoleInstance, RoleId>;
515 * // removeRoles...
516 * // hasRole...
517 * // hasRoles...
518 * // countRoles...
519 * }
520 * ```
521 *
522 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
523 * @see Instance
524 */
525 interface HasManyRemoveAssociationMixin<TInstance, TInstancePrimaryKey> {
526 /**
527 * Un-associate the instance.
528 * @param oldAssociated The instance or the primary key of the instance to un-associate.
529 * @param options The options passed to `target.update`.
530 */
531 (
532 oldAssociated?: TInstance | TInstancePrimaryKey,
533 options?: HasManyRemoveAssociationMixinOptions | InstanceUpdateOptions,
534 ): Promise<void>;
535 }
536
537 /**
538 * The options for the removeAssociations mixin of the hasMany association.
539 * @see HasManyRemoveAssociationsMixin
540 */
541 interface HasManyRemoveAssociationsMixinOptions {}
542
543 /**
544 * The removeAssociations mixin applied to models with hasMany.
545 * An example of usage is as follows:
546 *
547 * ```js
548 *
549 * User.hasMany(Role);
550 *
551 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
552 * // getRoles...
553 * // setRoles...
554 * // addRoles...
555 * // addRole...
556 * // createRole...
557 * // removeRole...
558 * removeRoles: Sequelize.HasManyRemoveAssociationsMixin<RoleInstance, RoleId>;
559 * // hasRole...
560 * // hasRoles...
561 * // countRoles...
562 * }
563 * ```
564 *
565 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
566 * @see Instance
567 */
568 interface HasManyRemoveAssociationsMixin<TInstance, TInstancePrimaryKey> {
569 /**
570 * Un-associate several instances.
571 * @param oldAssociated An array of instances or primary key of instances to un-associate.
572 * @param options The options passed to `target.update`.
573 */
574 (
575 oldAssociateds?: Array<TInstance | TInstancePrimaryKey>,
576 options?: HasManyRemoveAssociationsMixinOptions | InstanceUpdateOptions,
577 ): Promise<void>;
578 }
579
580 /**
581 * The options for the hasAssociation mixin of the hasMany association.
582 * @see HasManyHasAssociationMixin
583 */
584 interface HasManyHasAssociationMixinOptions {}
585
586 /**
587 * The hasAssociation mixin applied to models with hasMany.
588 * An example of usage is as follows:
589 *
590 * ```js
591 *
592 * User.hasMany(Role);
593 *
594 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
595 * // getRoles...
596 * // setRoles...
597 * // addRoles...
598 * // addRole...
599 * // createRole...
600 * // removeRole...
601 * // removeRoles...
602 * hasRole: Sequelize.HasManyHasAssociationMixin<RoleInstance, RoleId>;
603 * // hasRoles...
604 * // countRoles...
605 * }
606 * ```
607 *
608 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
609 * @see Instance
610 */
611 interface HasManyHasAssociationMixin<TInstance, TInstancePrimaryKey> {
612 /**
613 * Check if an instance is associated with this.
614 * @param target The instance or the primary key of the instance to check.
615 * @param options The options passed to `getAssociations`.
616 */
617 (
618 target: TInstance | TInstancePrimaryKey,
619 options?: HasManyHasAssociationMixinOptions | HasManyGetAssociationsMixinOptions,
620 ): Promise<boolean>;
621 }
622
623 /**
624 * The options for the hasAssociations mixin of the hasMany association.
625 * @see HasManyHasAssociationsMixin
626 */
627 interface HasManyHasAssociationsMixinOptions {}
628
629 /**
630 * The removeAssociations mixin applied to models with hasMany.
631 * An example of usage is as follows:
632 *
633 * ```js
634 *
635 * User.hasMany(Role);
636 *
637 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
638 * // getRoles...
639 * // setRoles...
640 * // addRoles...
641 * // addRole...
642 * // createRole...
643 * // removeRole...
644 * // removeRoles
645 * // hasRole...
646 * hasRoles: Sequelize.HasManyHasAssociationsMixin<RoleInstance, RoleId>;
647 * // countRoles...
648 * }
649 * ```
650 *
651 * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
652 * @see Instance
653 */
654 interface HasManyHasAssociationsMixin<TInstance, TInstancePrimaryKey> {
655 /**
656 * Check if all instances are associated with this.
657 * @param targets An array of instances or primary key of instances to check.
658 * @param options The options passed to `getAssociations`.
659 */
660 (
661 targets: