1 | import * as DataTypes from './data-types';
|
2 | import * as Deferrable from './deferrable';
|
3 | import { HookReturn, Hooks, SequelizeHooks } from './hooks';
|
4 | import { ValidationOptions } from './instance-validator';
|
5 | import {
|
6 | AndOperator,
|
7 | BulkCreateOptions,
|
8 | CreateOptions,
|
9 | DestroyOptions,
|
10 | DropOptions,
|
11 | FindOptions,
|
12 | InstanceDestroyOptions,
|
13 | Logging,
|
14 | Model,
|
15 | ModelAttributeColumnOptions,
|
16 | ModelAttributes,
|
17 | ModelOptions,
|
18 | OrOperator,
|
19 | UpdateOptions,
|
20 | WhereAttributeHash,
|
21 | WhereOperators,
|
22 | ModelCtor,
|
23 | } from './model';
|
24 | import { ModelManager } from './model-manager';
|
25 | import * as Op from './operators';
|
26 | import { Promise } from './promise';
|
27 | import { QueryInterface, QueryOptions, QueryOptionsWithModel, QueryOptionsWithType } from './query-interface';
|
28 | import QueryTypes = require('./query-types');
|
29 | import { Transaction, TransactionOptions } from './transaction';
|
30 | import { Cast, Col, Fn, Json, Literal, Where } from './utils';
|
31 |
|
32 | import * as Utils from './utils';
|
33 | import { validator } from './utils/validator-extras';
|
34 | import { ConnectionManager } from './connection-manager';
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | export interface SyncOptions extends Logging {
|
40 | |
41 |
|
42 |
|
43 | force?: boolean;
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 | alter?: boolean;
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 | match?: RegExp;
|
56 |
|
57 | |
58 |
|
59 |
|
60 | schema?: string;
|
61 |
|
62 | |
63 |
|
64 |
|
65 | searchPath?: string;
|
66 |
|
67 | |
68 |
|
69 |
|
70 | hooks?: boolean;
|
71 | }
|
72 |
|
73 | export interface DefaultSetOptions {}
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | export interface PoolOptions {
|
79 | |
80 |
|
81 |
|
82 | max?: number;
|
83 |
|
84 | |
85 |
|
86 |
|
87 | min?: number;
|
88 |
|
89 | |
90 |
|
91 |
|
92 | idle?: number;
|
93 |
|
94 | |
95 |
|
96 |
|
97 | acquire?: number;
|
98 |
|
99 | |
100 |
|
101 |
|
102 | evict?: number;
|
103 |
|
104 | |
105 |
|
106 |
|
107 |
|
108 | validate?(client?: unknown): boolean;
|
109 | }
|
110 |
|
111 | export interface ConnectionOptions {
|
112 | host?: string;
|
113 | port?: string | number;
|
114 | username?: string;
|
115 | password?: string;
|
116 | database?: string;
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | export interface ReplicationOptions {
|
123 | read: ConnectionOptions[];
|
124 |
|
125 | write: ConnectionOptions;
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | export interface OperatorsAliases {
|
132 | [K: string]: symbol;
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | export interface Config {
|
139 | readonly database: string;
|
140 | readonly dialectModule?: object;
|
141 | readonly host?: string;
|
142 | readonly port?: string;
|
143 | readonly username: string;
|
144 | readonly password: string | null;
|
145 | readonly pool?: {
|
146 | readonly acquire: number;
|
147 | readonly idle: number;
|
148 | readonly max: number;
|
149 | readonly min: number;
|
150 | };
|
151 | readonly protocol: 'tcp';
|
152 | readonly native: boolean;
|
153 | readonly ssl: boolean;
|
154 | readonly replication: boolean;
|
155 | readonly dialectModulePath: null | string;
|
156 | readonly keepDefaultTimezone?: boolean;
|
157 | readonly dialectOptions?: {
|
158 | readonly charset?: string;
|
159 | readonly timeout?: number;
|
160 | };
|
161 | }
|
162 |
|
163 | export type Dialect = 'mysql' | 'postgres' | 'sqlite' | 'mariadb' | 'mssql' | 'mariadb';
|
164 |
|
165 | export interface RetryOptions {
|
166 | match?: (RegExp | string | Function)[];
|
167 | max?: number;
|
168 | }
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | export interface Options extends Logging {
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 | dialect?: Dialect;
|
180 |
|
181 | |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | dialectModule?: object;
|
188 |
|
189 |
|
190 | |
191 |
|
192 |
|
193 |
|
194 | dialectModulePath?: string;
|
195 |
|
196 | |
197 |
|
198 |
|
199 | dialectOptions?: object;
|
200 |
|
201 | |
202 |
|
203 |
|
204 |
|
205 |
|
206 | storage?: string;
|
207 |
|
208 | |
209 |
|
210 |
|
211 | database?: string;
|
212 |
|
213 | |
214 |
|
215 |
|
216 | username?: string;
|
217 |
|
218 | |
219 |
|
220 |
|
221 | password?: string;
|
222 |
|
223 | |
224 |
|
225 |
|
226 |
|
227 |
|
228 | host?: string;
|
229 |
|
230 | |
231 |
|
232 |
|
233 | port?: number;
|
234 |
|
235 | |
236 |
|
237 |
|
238 | ssl?: boolean;
|
239 |
|
240 | |
241 |
|
242 |
|
243 |
|
244 |
|
245 | protocol?: string;
|
246 |
|
247 | |
248 |
|
249 |
|
250 | define?: ModelOptions;
|
251 |
|
252 | |
253 |
|
254 |
|
255 | query?: QueryOptions;
|
256 |
|
257 | |
258 |
|
259 |
|
260 | set?: DefaultSetOptions;
|
261 |
|
262 | |
263 |
|
264 |
|
265 | sync?: SyncOptions;
|
266 |
|
267 | |
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | timezone?: string;
|
278 |
|
279 | |
280 |
|
281 |
|
282 |
|
283 |
|
284 | omitNull?: boolean;
|
285 |
|
286 | |
287 |
|
288 |
|
289 |
|
290 |
|
291 | native?: boolean;
|
292 |
|
293 | |
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 | replication?: ReplicationOptions;
|
302 |
|
303 | |
304 |
|
305 |
|
306 | pool?: PoolOptions;
|
307 |
|
308 | |
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | quoteIdentifiers?: boolean;
|
315 |
|
316 | |
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | isolationLevel?: string;
|
323 |
|
324 | |
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | typeValidation?: boolean;
|
331 |
|
332 | |
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | operatorsAliases?: OperatorsAliases;
|
340 |
|
341 |
|
342 | |
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | standardConformingStrings?: boolean;
|
349 |
|
350 | |
351 |
|
352 |
|
353 | hooks?: Partial<SequelizeHooks>;
|
354 |
|
355 | |
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | minifyAliases?: boolean;
|
362 |
|
363 | |
364 |
|
365 |
|
366 |
|
367 |
|
368 | logQueryParameters?: boolean;
|
369 |
|
370 | retry?: RetryOptions;
|
371 | }
|
372 |
|
373 | export interface QueryOptionsTransactionRequired {}
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 | export class Sequelize extends Hooks {
|
388 |
|
389 |
|
390 |
|
391 | |
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 | public static fn: typeof fn;
|
407 |
|
408 | |
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 | public static col: typeof col;
|
415 |
|
416 | |
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | public static cast: typeof cast;
|
423 |
|
424 | |
425 |
|
426 |
|
427 |
|
428 |
|
429 | public static literal: typeof literal;
|
430 |
|
431 | |
432 |
|
433 |
|
434 |
|
435 |
|
436 | public static and: typeof and;
|
437 |
|
438 | |
439 |
|
440 |
|
441 |
|
442 |
|
443 | public static or: typeof or;
|
444 |
|
445 | |
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 | public static json: typeof json;
|
454 |
|
455 | |
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 | public static where: typeof where;
|
474 |
|
475 | |
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 | public static beforeValidate(name: string, fn: (instance: Model, options: ValidationOptions) => void): void;
|
482 | public static beforeValidate(fn: (instance: Model, options: ValidationOptions) => void): void;
|
483 |
|
484 | |
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 | public static afterValidate(name: string, fn: (instance: Model, options: ValidationOptions) => void): void;
|
491 | public static afterValidate(fn: (instance: Model, options: ValidationOptions) => void): void;
|
492 |
|
493 | |
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 | public static beforeCreate(name: string, fn: (attributes: Model, options: CreateOptions) => void): void;
|
500 | public static beforeCreate(fn: (attributes: Model, options: CreateOptions) => void): void;
|
501 |
|
502 | |
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 | public static afterCreate(name: string, fn: (attributes: Model, options: CreateOptions) => void): void;
|
509 | public static afterCreate(fn: (attributes: Model, options: CreateOptions) => void): void;
|
510 |
|
511 | |
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 | public static beforeDestroy(name: string, fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
|
518 | public static beforeDestroy(fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
|
519 |
|
520 | |
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 | public static afterDestroy(name: string, fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
|
527 | public static afterDestroy(fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
|
528 |
|
529 | |
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 | public static beforeUpdate(name: string, fn: (instance: Model, options: UpdateOptions) => void): void;
|
536 | public static beforeUpdate(fn: (instance: Model, options: UpdateOptions) => void): void;
|
537 |
|
538 | |
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 | public static afterUpdate(name: string, fn: (instance: Model, options: UpdateOptions) => void): void;
|
545 | public static afterUpdate(fn: (instance: Model, options: UpdateOptions) => void): void;
|
546 |
|
547 | |
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 | public static beforeSave(name: string, fn: (instance: Model, options: UpdateOptions | CreateOptions) => void): void;
|
554 | public static beforeSave(fn: (instance: Model, options: UpdateOptions | CreateOptions) => void): void;
|
555 |
|
556 | |
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 | public static afterSave(name: string, fn: (instance: Model, options: UpdateOptions | CreateOptions) => void): void;
|
563 | public static afterSave(fn: (instance: Model, options: UpdateOptions | CreateOptions) => void): void;
|
564 |
|
565 | |
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 | public static beforeBulkCreate(name: string, fn: (instances: Model[], options: BulkCreateOptions) => void): void;
|
572 | public static beforeBulkCreate(fn: (instances: Model[], options: BulkCreateOptions) => void): void;
|
573 |
|
574 | |
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 | public static afterBulkCreate(name: string, fn: (instances: Model[], options: BulkCreateOptions) => void): void;
|
581 | public static afterBulkCreate(fn: (instances: Model[], options: BulkCreateOptions) => void): void;
|
582 |
|
583 | |
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 | public static beforeBulkDestroy(name: string, fn: (options: BulkCreateOptions) => void): void;
|
590 | public static beforeBulkDestroy(fn: (options: BulkCreateOptions) => void): void;
|
591 |
|
592 | |
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 | public static afterBulkDestroy(name: string, fn: (options: DestroyOptions) => void): void;
|
599 | public static afterBulkDestroy(fn: (options: DestroyOptions) => void): void;
|
600 |
|
601 | |
602 |
|
603 |
|
604 |
|
605 |
|
606 |
|
607 | public static beforeBulkUpdate(name: string, fn: (options: UpdateOptions) => void): void;
|
608 | public static beforeBulkUpdate(fn: (options: UpdateOptions) => void): void;
|
609 |
|
610 | |
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 | public static afterBulkUpdate(name: string, fn: (options: UpdateOptions) => void): void;
|
617 | public static afterBulkUpdate(fn: (options: UpdateOptions) => void): void;
|
618 |
|
619 | |
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 | public static beforeFind(name: string, fn: (options: FindOptions) => void): void;
|
626 | public static beforeFind(fn: (options: FindOptions) => void): void;
|
627 |
|
628 | |
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 | public static beforeConnect(name: string, fn: (options: Config) => void): void;
|
635 | public static beforeConnect(fn: (options: Config) => void): void;
|
636 |
|
637 | |
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 | public static afterConnect(name: string, fn: (connection: unknown, options: Config) => void): void;
|
644 | public static afterConnect(fn: (connection: unknown, options: Config) => void): void;
|
645 |
|
646 | |
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 | public static beforeDisconnect(name: string, fn: (connection: unknown) => void): void;
|
653 | public static beforeDisconnect(fn: (connection: unknown) => void): void;
|
654 |
|
655 | |
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 | public static afterDisconnect(name: string, fn: (connection: unknown) => void): void;
|
662 | public static afterDisconnect(fn: (connection: unknown) => void): void;
|
663 |
|
664 | |
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 | public static beforeFindAfterExpandIncludeAll(name: string, fn: (options: FindOptions) => void): void;
|
671 | public static beforeFindAfterExpandIncludeAll(fn: (options: FindOptions) => void): void;
|
672 |
|
673 | |
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 | public static beforeFindAfterOptions(name: string, fn: (options: FindOptions) => void): void;
|
680 | public static beforeFindAfterOptions(fn: (options: FindOptions) => void): void;
|
681 |
|
682 | |
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 | public static afterFind(
|
689 | name: string,
|
690 | fn: (instancesOrInstance: Model[] | Model | null, options: FindOptions) => void
|
691 | ): void;
|
692 | public static afterFind(
|
693 | fn: (instancesOrInstance: Model[] | Model | null, options: FindOptions) => void
|
694 | ): void;
|
695 |
|
696 | |
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 | public static beforeDefine<M extends Model>(
|
703 | name: string,
|
704 | fn: (attributes: ModelAttributes, options: ModelOptions<M>) => void
|
705 | ): void;
|
706 | public static beforeDefine<M extends Model>(
|
707 | fn: (attributes: ModelAttributes, options: ModelOptions<M>) => void
|
708 | ): void;
|
709 |
|
710 | |
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 | public static afterDefine(name: string, fn: (model: typeof Model) => void): void;
|
717 | public static afterDefine(fn: (model: typeof Model) => void): void;
|
718 |
|
719 | |
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 | public static beforeInit(name: string, fn: (config: Config, options: Options) => void): void;
|
726 | public static beforeInit(fn: (config: Config, options: Options) => void): void;
|
727 |
|
728 | |
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 | public static afterInit(name: string, fn: (sequelize: Sequelize) => void): void;
|
735 | public static afterInit(fn: (sequelize: Sequelize) => void): void;
|
736 |
|
737 | |
738 |
|
739 |
|
740 |
|
741 | public static beforeBulkSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
|
742 | public static beforeBulkSync(fn: (options: SyncOptions) => HookReturn): void;
|
743 |
|
744 | |
745 |
|
746 |
|
747 |
|
748 | public static afterBulkSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
|
749 | public static afterBulkSync(fn: (options: SyncOptions) => HookReturn): void;
|
750 |
|
751 | |
752 |
|
753 |
|
754 |
|
755 | public static beforeSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
|
756 | public static beforeSync(fn: (options: SyncOptions) => HookReturn): void;
|
757 |
|
758 | |
759 |
|
760 |
|
761 |
|
762 | public static afterSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
|
763 | public static afterSync(fn: (options: SyncOptions) => HookReturn): void;
|
764 |
|
765 | |
766 |
|
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 | public static useCLS(namespace: object): typeof Sequelize;
|
773 |
|
774 | |
775 |
|
776 |
|
777 | public Sequelize: typeof Sequelize;
|
778 |
|
779 | |
780 |
|
781 |
|
782 | public readonly config: Config;
|
783 |
|
784 | public readonly modelManager: ModelManager;
|
785 |
|
786 | public readonly connectionManager: ConnectionManager;
|
787 |
|
788 | |
789 |
|
790 |
|
791 | public readonly models: {
|
792 | [key: string]: ModelCtor<Model>;
|
793 | };
|
794 |
|
795 | |
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 | constructor(database: string, username: string, password?: string, options?: Options);
|
825 | constructor(database: string, username: string, options?: Options);
|
826 | constructor(options?: Options);
|
827 |
|
828 | /**
|
829 | * Instantiate sequelize with an URI
|
830 | * @param uri A full database URI
|
831 | * @param options See above for possible options
|
832 | */
|
833 | constructor(uri: string, options?: Options);
|
834 |
|
835 | /**
|
836 | * A hook that is run before validation
|
837 | *
|
838 | * @param name
|
839 | * @param fn A callback function that is called with instance, options
|
840 | */
|
841 | public beforeValidate(name: string, fn: (instance: Model, options: ValidationOptions) => void): void;
|
842 | public beforeValidate(fn: (instance: Model, options: ValidationOptions) => void): void;
|
843 |
|
844 | /**
|
845 | * A hook that is run after validation
|
846 | *
|
847 | * @param name
|
848 | * @param fn A callback function that is called with instance, options
|
849 | */
|
850 | public afterValidate(name: string, fn: (instance: Model, options: ValidationOptions) => void): void;
|
851 | public afterValidate(fn: (instance: Model, options: ValidationOptions) => void): void;
|
852 |
|
853 | /**
|
854 | * A hook that is run before creating a single instance
|
855 | *
|
856 | * @param name
|
857 | * @param fn A callback function that is called with attributes, options
|
858 | */
|
859 | public beforeCreate(name: string, fn: (attributes: Model, options: CreateOptions) => void): void;
|
860 | public beforeCreate(fn: (attributes: Model, options: CreateOptions) => void): void;
|
861 |
|
862 | /**
|
863 | * A hook that is run after creating a single instance
|
864 | *
|
865 | * @param name
|
866 | * @param fn A callback function that is called with attributes, options
|
867 | */
|
868 | public afterCreate(name: string, fn: (attributes: Model, options: CreateOptions) => void): void;
|
869 | public afterCreate(fn: (attributes: Model, options: CreateOptions) => void): void;
|
870 |
|
871 | /**
|
872 | * A hook that is run before destroying a single instance
|
873 | *
|
874 | * @param name
|
875 | * @param fn A callback function that is called with instance, options
|
876 | */
|
877 | public beforeDestroy(name: string, fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
|
878 | public beforeDestroy(fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
|
879 |
|
880 | /**
|
881 | * A hook that is run after destroying a single instance
|
882 | *
|
883 | * @param name
|
884 | * @param fn A callback function that is called with instance, options
|
885 | */
|
886 | public afterDestroy(name: string, fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
|
887 | public afterDestroy(fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
|
888 |
|
889 | /**
|
890 | * A hook that is run before updating a single instance
|
891 | *
|
892 | * @param name
|
893 | * @param fn A callback function that is called with instance, options
|
894 | */
|
895 | public beforeUpdate(name: string, fn: (instance: Model, options: UpdateOptions) => void): void;
|
896 | public beforeUpdate(fn: (instance: Model, options: UpdateOptions) => void): void;
|
897 |
|
898 | /**
|
899 | * A hook that is run after updating a single instance
|
900 | *
|
901 | * @param name
|
902 | * @param fn A callback function that is called with instance, options
|
903 | */
|
904 | public afterUpdate(name: string, fn: (instance: Model, options: UpdateOptions) => void): void;
|
905 | public afterUpdate(fn: (instance: Model, options: UpdateOptions) => void): void;
|
906 |
|
907 | /**
|
908 | * A hook that is run before creating instances in bulk
|
909 | *
|
910 | * @param name
|
911 | * @param fn A callback function that is called with instances, options
|
912 | */
|
913 | public beforeBulkCreate(name: string, fn: (instances: Model[], options: BulkCreateOptions) => void): void;
|
914 | public beforeBulkCreate(fn: (instances: Model[], options: BulkCreateOptions) => void): void;
|
915 |
|
916 | /**
|
917 | * A hook that is run after creating instances in bulk
|
918 | *
|
919 | * @param name
|
920 | * @param fn A callback function that is called with instances, options
|
921 | */
|
922 | public afterBulkCreate(name: string, fn: (instances: Model[], options: BulkCreateOptions) => void): void;
|
923 | public afterBulkCreate(fn: (instances: Model[], options: BulkCreateOptions) => void): void;
|
924 |
|
925 | /**
|
926 | * A hook that is run before destroying instances in bulk
|
927 | *
|
928 | * @param name
|
929 | * @param fn A callback function that is called with options
|
930 | */
|
931 | public beforeBulkDestroy(name: string, fn: (options: BulkCreateOptions) => void): void;
|
932 | public beforeBulkDestroy(fn: (options: BulkCreateOptions) => void): void;
|
933 |
|
934 | /**
|
935 | * A hook that is run after destroying instances in bulk
|
936 | *
|
937 | * @param name
|
938 | * @param fn A callback function that is called with options
|
939 | */
|
940 | public afterBulkDestroy(name: string, fn: (options: DestroyOptions) => void): void;
|
941 | public afterBulkDestroy(fn: (options: DestroyOptions) => void): void;
|
942 |
|
943 | /**
|
944 | * A hook that is run after updating instances in bulk
|
945 | *
|
946 | * @param name
|
947 | * @param fn A callback function that is called with options
|
948 | */
|
949 | public beforeBulkUpdate(name: string, fn: (options: UpdateOptions) => void): void;
|
950 | public beforeBulkUpdate(fn: (options: UpdateOptions) => void): void;
|
951 |
|
952 | /**
|
953 | * A hook that is run after updating instances in bulk
|
954 | *
|
955 | * @param name
|
956 | * @param fn A callback function that is called with options
|
957 | */
|
958 | public afterBulkUpdate(name: string, fn: (options: UpdateOptions) => void): void;
|
959 | public afterBulkUpdate(fn: (options: UpdateOptions) => void): void;
|
960 |
|
961 | /**
|
962 | * A hook that is run before a find (select) query
|
963 | *
|
964 | * @param name
|
965 | * @param fn A callback function that is called with options
|
966 | */
|
967 | public beforeFind(name: string, fn: (options: FindOptions) => void): void;
|
968 | public beforeFind(fn: (options: FindOptions) => void): void;
|
969 |
|
970 | /**
|
971 | * A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
|
972 | *
|
973 | * @param name
|
974 | * @param fn A callback function that is called with options
|
975 | */
|
976 | public beforeFindAfterExpandIncludeAll(name: string, fn: (options: FindOptions) => void): void;
|
977 | public beforeFindAfterExpandIncludeAll(fn: (options: FindOptions) => void): void;
|
978 |
|
979 | /**
|
980 | * A hook that is run before a find (select) query, after all option parsing is complete
|
981 | *
|
982 | * @param name
|
983 | * @param fn A callback function that is called with options
|
984 | */
|
985 | public beforeFindAfterOptions(name: string, fn: (options: FindOptions) => void): void;
|
986 | public beforeFindAfterOptions(fn: (options: FindOptions) => void): void;
|
987 |
|
988 | /**
|
989 | * A hook that is run after a find (select) query
|
990 | *
|
991 | * @param name
|
992 | * @param fn A callback function that is called with instance(s), options
|
993 | */
|
994 | public afterFind(
|
995 | name: string,
|
996 | fn: (instancesOrInstance: Model[] | Model | null, options: FindOptions) => void
|
997 | ): void;
|
998 | public afterFind(fn: (instancesOrInstance: Model[] | Model | null, options: FindOptions) => void): void;
|
999 |
|
1000 | /**
|
1001 | * A hook that is run before a define call
|
1002 | *
|
1003 | * @param name
|
1004 | * @param fn A callback function that is called with attributes, options
|
1005 | */
|
1006 | public beforeDefine(name: string, fn: (attributes: ModelAttributes, options: ModelOptions) => void): void;
|
1007 | public beforeDefine(fn: (attributes: ModelAttributes, options: ModelOptions) => void): void;
|
1008 |
|
1009 | /**
|
1010 | * A hook that is run after a define call
|
1011 | *
|
1012 | * @param name
|
1013 | * @param fn A callback function that is called with factory
|
1014 | */
|
1015 | public afterDefine(name: string, fn: (model: typeof Model) => void): void;
|
1016 | public afterDefine(fn: (model: typeof Model) => void): void;
|
1017 |
|
1018 | /**
|
1019 | * A hook that is run before Sequelize() call
|
1020 | *
|
1021 | * @param name
|
1022 | * @param fn A callback function that is called with config, options
|
1023 | */
|
1024 | public beforeInit(name: string, fn: (config: Config, options: Options) => void): void;
|
1025 | public beforeInit(fn: (config: Config, options: Options) => void): void;
|
1026 |
|
1027 | /**
|
1028 | * A hook that is run after Sequelize() call
|
1029 | *
|
1030 | * @param name
|
1031 | * @param fn A callback function that is called with sequelize
|
1032 | */
|
1033 | public afterInit(name: string, fn: (sequelize: Sequelize) => void): void;
|
1034 | public afterInit(fn: (sequelize: Sequelize) => void): void;
|
1035 |
|
1036 | /**
|
1037 | * A hook that is run before sequelize.sync call
|
1038 | * @param fn A callback function that is called with options passed to sequelize.sync
|
1039 | */
|
1040 | public beforeBulkSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
|
1041 | public beforeBulkSync(fn: (options: SyncOptions) => HookReturn): void;
|
1042 |
|
1043 | /**
|
1044 | * A hook that is run after sequelize.sync call
|
1045 | * @param fn A callback function that is called with options passed to sequelize.sync
|
1046 | */
|
1047 | public afterBulkSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
|
1048 | public afterBulkSync(fn: (options: SyncOptions) => HookReturn): void;
|
1049 |
|
1050 | /**
|
1051 | * A hook that is run before Model.sync call
|
1052 | * @param fn A callback function that is called with options passed to Model.sync
|
1053 | */
|
1054 | public beforeSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
|
1055 | public beforeSync(fn: (options: SyncOptions) => HookReturn): void;
|
1056 |
|
1057 | /**
|
1058 | * A hook that is run after Model.sync call
|
1059 | * @param fn A callback function that is called with options passed to Model.sync
|
1060 | */
|
1061 | public afterSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
|
1062 | public afterSync(fn: (options: SyncOptions) => HookReturn): void;
|
1063 |
|
1064 | /**
|
1065 | * Returns the specified dialect.
|
1066 | */
|
1067 | public getDialect(): string;
|
1068 |
|
1069 | /**
|
1070 | * Returns an instance of QueryInterface.
|
1071 | */
|
1072 | public getQueryInterface(): QueryInterface;
|
1073 |
|
1074 | /**
|
1075 | * Define a new model, representing a table in the DB.
|
1076 | *
|
1077 | * The table columns are defined by the hash that is given as the second argument. Each attribute of the
|
1078 | * hash
|
1079 | * represents a column. A short table definition might look like this:
|
1080 | *
|
1081 | * ```js
|
1082 | * class MyModel extends Model {}
|
1083 | * MyModel.init({
|
1084 | * columnA: {
|
1085 | * type: Sequelize.BOOLEAN,
|
1086 | * validate: {
|
1087 | * is: ["[a-z]",'i'],
|
1088 | * max: 23,
|
1089 | * isIn: {
|
1090 | * args: [['en', 'zh']],
|
1091 | * msg: "Must be English or Chinese"
|
1092 | * }
|
1093 | * },
|
1094 | * field: 'column_a'
|
1095 | *
|
1096 | * },
|
1097 | * columnB: Sequelize.STRING,
|
1098 | * columnC: 'MY VERY OWN COLUMN TYPE'
|
1099 | * }, { sequelize })
|
1100 | *
|
1101 | * sequelize.models.modelName // The model will now be available in models under the name given to define
|
1102 | * ```
|
1103 | *
|
1104 | * As shown above, column definitions can be either strings, a reference to one of the datatypes that are
|
1105 | * predefined on the Sequelize constructor, or an object that allows you to specify both the type of the
|
1106 | * column, and other attributes such as default values, foreign key constraints and custom setters and
|
1107 | * getters.
|
1108 | *
|
1109 | * For a list of possible data types, see
|
1110 | * https://sequelize.org/master/en/latest/docs/models-definition/#data-types
|
1111 | *
|
1112 | * For more about getters and setters, see
|
1113 | * https://sequelize.org/master/en/latest/docs/models-definition/#getters-setters
|
1114 | *
|
1115 | * For more about instance and class methods, see
|
1116 | * https://sequelize.org/master/en/latest/docs/models-definition/#expansion-of-models
|
1117 | *
|
1118 | * For more about validation, see
|
1119 | * https://sequelize.org/master/en/latest/docs/models-definition/#validations
|
1120 | *
|
1121 | * @param modelName The name of the model. The model will be stored in `sequelize.models` under this name
|
1122 | * @param attributes An object, where each attribute is a column of the table. Each column can be either a
|
1123 | * DataType, a string or a type-description object, with the properties described below:
|
1124 | * @param options These options are merged with the default define options provided to the Sequelize
|
1125 | * constructor
|
1126 | */
|
1127 | public define(modelName: string, attributes: ModelAttributes, options?: ModelOptions): typeof Model;
|
1128 |
|
1129 | /**
|
1130 | * Fetch a Model which is already defined
|
1131 | *
|
1132 | * @param modelName The name of a model defined with Sequelize.define
|
1133 | */
|
1134 | public model(modelName: string): ModelCtor<Model>;
|
1135 |
|
1136 | /**
|
1137 | * Checks whether a model with the given name is defined
|
1138 | *
|
1139 | * @param modelName The name of a model defined with Sequelize.define
|
1140 | */
|
1141 | public isDefined(modelName: string): boolean;
|
1142 |
|
1143 | /**
|
1144 | * Imports a model defined in another file
|
1145 | *
|
1146 | * Imported models are cached, so multiple calls to import with the same path will not load the file
|
1147 | * multiple times
|
1148 | *
|
1149 | * See https://github.com/sequelize/sequelize/blob/master/examples/using-multiple-model-files/Task.js for a
|
1150 | * short example of how to define your models in separate files so that they can be imported by
|
1151 | * sequelize.import
|
1152 | *
|
1153 | * @param path The path to the file that holds the model you want to import. If the part is relative, it
|
1154 | * will be resolved relatively to the calling file
|
1155 | *
|
1156 | * @param defineFunction An optional function that provides model definitions. Useful if you do not
|
1157 | * want to use the module root as the define function
|
1158 | */
|
1159 | public import<T extends typeof Model>(
|
1160 | path: string,
|
1161 | defineFunction?: (sequelize: Sequelize, dataTypes: typeof DataTypes) => T
|
1162 | ): T;
|
1163 |
|
1164 | /**
|
1165 | * Execute a query on the DB, optionally bypassing all the Sequelize goodness.
|
1166 | *
|
1167 | * By default, the function will return two arguments: an array of results, and a metadata object,
|
1168 | * containing number of affected rows etc. Use `.then(([...]))` to access the results.
|
1169 | *
|
1170 | * If you are running a type of query where you don't need the metadata, for example a `SELECT` query, you
|
1171 | * can pass in a query type to make sequelize format the results:
|
1172 | *
|
1173 | * ```js
|
1174 | * sequelize.query('SELECT...').then(([results, metadata]) {
|
1175 | *
|
1176 | * });
|
1177 | *
|
1178 | * sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }).then(results => {
|
1179 | *
|
1180 | * })
|
1181 | * ```
|
1182 | *
|
1183 | * @param sql
|
1184 | * @param options Query options
|
1185 | */
|
1186 | public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.UPDATE>): Promise<[undefined, number]>;
|
1187 | public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.BULKUPDATE>): Promise<number>;
|
1188 | public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.INSERT>): Promise<[number, number]>;
|
1189 | public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.UPSERT>): Promise<number>;
|
1190 | public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.DELETE>): Promise<void>;
|
1191 | public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.BULKDELETE>): Promise<number>;
|
1192 | public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.SHOWTABLES>): Promise<string[]>;
|
1193 | public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.DESCRIBE>): Promise<{
|
1194 | [key: string]: {
|
1195 | type: string;
|
1196 | allowNull: boolean;
|
1197 | defaultValue: string;
|
1198 | primaryKey: boolean;
|
1199 | autoIncrement: boolean;
|
1200 | comment: string | null;
|
1201 | }
|
1202 | }>;
|
1203 | public query<M extends Model>(
|
1204 | sql: string | { query: string; values: unknown[] },
|
1205 | options: QueryOptionsWithModel
|
1206 | ): Promise<M[]>;
|
1207 | public query<T extends object>(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.SELECT> & { plain: true }): Promise<T>;
|
1208 | public query<T extends object>(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.SELECT>): Promise<T[]>;
|
1209 | public query(sql: string | { query: string; values: unknown[] }, options: (QueryOptions | QueryOptionsWithType<QueryTypes.RAW>) & { plain: true }): Promise<{ [key: string]: unknown }>;
|
1210 | public query(sql: string | { query: string; values: unknown[] }, options?: QueryOptions | QueryOptionsWithType<QueryTypes.RAW>): Promise<[unknown[], unknown]>;
|
1211 |
|
1212 | |
1213 |
|
1214 |
|
1215 | public random(): Fn;
|
1216 |
|
1217 | |
1218 |
|
1219 |
|
1220 |
|
1221 |
|
1222 |
|
1223 |
|
1224 |
|
1225 |
|
1226 | public set(variables: object, options: QueryOptionsTransactionRequired): Promise<unknown>;
|
1227 |
|
1228 | |
1229 |
|
1230 |
|
1231 |
|
1232 |
|
1233 | public escape(value: string | number | Date): string;
|
1234 |
|
1235 | |
1236 |
|
1237 |
|
1238 |
|
1239 |
|
1240 |
|
1241 |
|
1242 |
|
1243 |
|
1244 |
|
1245 | public createSchema(schema: string, options: Logging): Promise<unknown>;
|
1246 |
|
1247 | |
1248 |
|
1249 |
|
1250 |
|
1251 |
|
1252 |
|
1253 |
|
1254 |
|
1255 |
|
1256 | public showAllSchemas(options: Logging): Promise<object[]>;
|
1257 |
|
1258 | |
1259 |
|
1260 |
|
1261 |
|
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 |
|
1267 |
|
1268 | public dropSchema(schema: string, options: Logging): Promise<unknown[]>;
|
1269 |
|
1270 | |
1271 |
|
1272 |
|
1273 |
|
1274 |
|
1275 |
|
1276 |
|
1277 |
|
1278 |
|
1279 | public dropAllSchemas(options: Logging): Promise<unknown[]>;
|
1280 |
|
1281 | |
1282 |
|
1283 |
|
1284 |
|
1285 |
|
1286 | public sync(options?: SyncOptions): Promise<this>;
|
1287 |
|
1288 | |
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 | public truncate(options?: DestroyOptions): Promise<unknown[]>;
|
1295 |
|
1296 | |
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 | public drop(options?: DropOptions): Promise<unknown[]>;
|
1302 |
|
1303 | |
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 | public authenticate(options?: QueryOptions): Promise<void>;
|
1309 | public validate(options?: QueryOptions): Promise<void>;
|
1310 |
|
1311 | |
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 |
|
1321 |
|
1322 |
|
1323 |
|
1324 |
|
1325 |
|
1326 |
|
1327 |
|
1328 |
|
1329 |
|
1330 |
|
1331 |
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 |
|
1337 |
|
1338 |
|
1339 |
|
1340 |
|
1341 |
|
1342 |
|
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 |
|
1349 |
|
1350 |
|
1351 |
|
1352 |
|
1353 |
|
1354 |
|
1355 |
|
1356 | public transaction<T>(options: TransactionOptions, autoCallback: (t: Transaction) => PromiseLike<T>): Promise<T>;
|
1357 | public transaction<T>(autoCallback: (t: Transaction) => PromiseLike<T>): Promise<T>;
|
1358 | public transaction(options?: TransactionOptions): Promise<Transaction>;
|
1359 |
|
1360 | /**
|
1361 | * Close all connections used by this sequelize instance, and free all references so the instance can be
|
1362 | * garbage collected.
|
1363 | *
|
1364 | * Normally this is done on process exit, so you only need to call this method if you are creating multiple
|
1365 | * instances, and want to garbage collect some of them.
|
1366 | */
|
1367 | public close(): Promise<void>;
|
1368 |
|
1369 | /**
|
1370 | * Returns the database version
|
1371 | */
|
1372 | public databaseVersion(): Promise<string>;
|
1373 | }
|
1374 |
|
1375 | // Utilities
|
1376 |
|
1377 | /**
|
1378 | * Creates a object representing a database function. This can be used in search queries, both in where and
|
1379 | * order parts, and as default values in column definitions. If you want to refer to columns in your
|
1380 | * function, you should use `sequelize.col`, so that the columns are properly interpreted as columns and
|
1381 | * not a strings.
|
1382 | *
|
1383 | * Convert a user's username to upper case
|
1384 | * ```js
|
1385 | * instance.update({
|
1386 | * username: self.sequelize.fn('upper', self.sequelize.col('username'))
|
1387 | * })
|
1388 | * ```
|
1389 | * @param fn The function you want to call
|
1390 | * @param args All further arguments will be passed as arguments to the function
|
1391 | */
|
1392 | export function fn(fn: string, ...args: unknown[]): Fn;
|
1393 |
|
1394 | /**
|
1395 | * Creates a object representing a column in the DB. This is often useful in conjunction with
|
1396 | * `sequelize.fn`, since raw string arguments to fn will be escaped.
|
1397 | *
|
1398 | * @param col The name of the column
|
1399 | */
|
1400 | export function col(col: string): Col;
|
1401 |
|
1402 | /**
|
1403 | * Creates a object representing a call to the cast function.
|
1404 | *
|
1405 | * @param val The value to cast
|
1406 | * @param type The type to cast it to
|
1407 | */
|
1408 | export function cast(val: unknown, type: string): Cast;
|
1409 |
|
1410 | /**
|
1411 | * Creates a object representing a literal, i.e. something that will not be escaped.
|
1412 | *
|
1413 | * @param val
|
1414 | */
|
1415 | export function literal(val: string): Literal;
|
1416 |
|
1417 | /**
|
1418 | * An AND query
|
1419 | *
|
1420 | * @param args Each argument will be joined by AND
|
1421 | */
|
1422 | export function and(...args: (WhereOperators | WhereAttributeHash | Where)[]): AndOperator;
|
1423 |
|
1424 | /**
|
1425 | * An OR query
|
1426 | *
|
1427 | * @param args Each argument will be joined by OR
|
1428 | */
|
1429 | export function or(...args: (WhereOperators | WhereAttributeHash | Where)[]): OrOperator;
|
1430 |
|
1431 | /**
|
1432 | * Creates an object representing nested where conditions for postgres's json data-type.
|
1433 | *
|
1434 | * @param conditionsOrPath A hash containing strings/numbers or other nested hash, a string using dot
|
1435 | * notation or a string using postgres json syntax.
|
1436 | * @param value An optional value to compare against. Produces a string of the form "<json path> =
|
1437 | * '<value>'".
|
1438 | */
|
1439 | export function json(conditionsOrPath: string | object, value?: string | number | boolean): Json;
|
1440 |
|
1441 | export type AttributeType = Fn | Col | Literal | ModelAttributeColumnOptions | string;
|
1442 | export type LogicType = Fn | Col | Literal | OrOperator | AndOperator | WhereOperators | string;
|
1443 |
|
1444 | /**
|
1445 | * A way of specifying attr = condition.
|
1446 | *
|
1447 | * The attr can either be an object taken from `Model.rawAttributes` (for example `Model.rawAttributes.id`
|
1448 | * or
|
1449 | * `Model.rawAttributes.name`). The attribute should be defined in your model definition. The attribute can
|
1450 | * also be an object from one of the sequelize utility functions (`sequelize.fn`, `sequelize.col` etc.)
|
1451 | *
|
1452 | * For string attributes, use the regular `{ where: { attr: something }}` syntax. If you don't want your
|
1453 | * string to be escaped, use `sequelize.literal`.
|
1454 | *
|
1455 | * @param attr The attribute, which can be either an attribute object from `Model.rawAttributes` or a
|
1456 | * sequelize object, for example an instance of `sequelize.fn`. For simple string attributes, use the
|
1457 | * POJO syntax
|
1458 | * @param comparator Comparator
|
1459 | * @param logic The condition. Can be both a simply type, or a further condition (`.or`, `.and`, `.literal`
|
1460 | * etc.)
|
1461 | */
|
1462 | export function where(attr: AttributeType, comparator: string, logic: LogicType): Where;
|
1463 | export function where(attr: AttributeType, logic: LogicType): Where;
|
1464 |
|
1465 | export default Sequelize;
|
1466 |
|
\ | No newline at end of file |