UNPKG

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