UNPKG

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