UNPKG

53.9 kBTypeScriptView Raw
1import type { Options as RetryAsPromisedOptions } from 'retry-as-promised';
2import { HookReturn, Hooks, SequelizeHooks } from './hooks';
3import { ValidationOptions } from './instance-validator';
4import {
5 AndOperator,
6 BulkCreateOptions,
7 CreateOptions,
8 DestroyOptions,
9 DropOptions,
10 FindOptions,
11 InstanceDestroyOptions,
12 Logging,
13 Model,
14 ModelAttributeColumnOptions,
15 ModelAttributes,
16 ModelOptions,
17 OrOperator,
18 UpdateOptions,
19 WhereOperators,
20 ModelCtor,
21 Hookable,
22 ModelType,
23 CreationAttributes,
24 Attributes,
25 ColumnReference, WhereAttributeHashValue,
26} from './model';
27import { ModelManager } from './model-manager';
28import { QueryInterface, QueryOptions, QueryOptionsWithModel, QueryOptionsWithType, ColumnsDescription } from './dialects/abstract/query-interface';
29import QueryTypes = require('./query-types');
30import { Transaction, TransactionOptions } from './transaction';
31import { Op } from './index';
32import { Cast, Col, DeepWriteable, Fn, Json, Literal, Where } from './utils';
33import { Connection, ConnectionManager, GetConnectionOptions } from './dialects/abstract/connection-manager';
34
35export type RetryOptions = RetryAsPromisedOptions;
36
37/**
38 * Additional options for table altering during sync
39 */
40export interface SyncAlterOptions {
41 /**
42 * Prevents any drop statements while altering a table when set to `false`
43 */
44 drop?: boolean;
45}
46
47/**
48 * Sync Options
49 */
50export interface SyncOptions extends Logging, Hookable {
51 /**
52 * If force is true, each DAO will do DROP TABLE IF EXISTS ..., before it tries to create its own table
53 */
54 force?: boolean;
55
56 /**
57 * If alter is true, each DAO will do ALTER TABLE ... CHANGE ...
58 * Alters tables to fit models. Provide an object for additional configuration. Not recommended for production use. If not further configured deletes data in columns that were removed or had their type changed in the model.
59 */
60 alter?: boolean | SyncAlterOptions;
61
62 /**
63 * Match a regex against the database name before syncing, a safety check for cases where force: true is
64 * used in tests but not live code
65 */
66 match?: RegExp;
67
68 /**
69 * The schema that the tables should be created in. This can be overridden for each table in sequelize.define
70 */
71 schema?: string;
72
73 /**
74 * An optional parameter to specify the schema search_path (Postgres only)
75 */
76 searchPath?: string;
77
78}
79
80export interface DefaultSetOptions { }
81
82/**
83 * Connection Pool options
84 */
85export interface PoolOptions {
86 /**
87 * Maximum number of connections in pool. Default is 5
88 */
89 max?: number;
90
91 /**
92 * Minimum number of connections in pool. Default is 0
93 */
94 min?: number;
95
96 /**
97 * The maximum time, in milliseconds, that a connection can be idle before being released
98 */
99 idle?: number;
100
101 /**
102 * The maximum time, in milliseconds, that pool will try to get connection before throwing error
103 */
104 acquire?: number;
105
106 /**
107 * The time interval, in milliseconds, after which sequelize-pool will remove idle connections.
108 */
109 evict?: number;
110
111 /**
112 * The number of times to use a connection before closing and replacing it. Default is Infinity
113 */
114 maxUses?: number;
115
116 /**
117 * A function that validates a connection. Called with client. The default function checks that client is an
118 * object, and that its state is not disconnected
119 */
120 validate?(client?: unknown): boolean;
121}
122
123export interface ConnectionOptions {
124 host?: string;
125 port?: string | number;
126 username?: string;
127 password?: string;
128 database?: string;
129}
130
131/**
132 * Interface for replication Options in the sequelize constructor
133 */
134export interface ReplicationOptions {
135 read: ConnectionOptions[];
136
137 write: ConnectionOptions;
138}
139
140/**
141 * Used to map operators to their Symbol representations
142 */
143export interface OperatorsAliases {
144 [K: string]: symbol;
145}
146
147/**
148 * Final config options generated by sequelize.
149 */
150export interface Config {
151 readonly database: string;
152 readonly dialectModule?: object;
153 readonly host?: string;
154 readonly port?: string;
155 readonly username: string;
156 readonly password: string | null;
157 readonly pool?: {
158 readonly acquire: number;
159 readonly idle: number;
160 readonly max: number;
161 readonly min: number;
162 };
163 readonly protocol: 'tcp';
164 readonly native: boolean;
165 readonly ssl: boolean;
166 readonly replication: ReplicationOptions | false;
167 readonly dialectModulePath: null | string;
168 readonly keepDefaultTimezone?: boolean;
169 readonly dialectOptions?: {
170 readonly charset?: string;
171 readonly timeout?: number;
172 };
173}
174
175export type Dialect = 'mysql' | 'postgres' | 'sqlite' | 'mariadb' | 'mssql' | 'db2' | 'snowflake' | 'oracle';
176
177/**
178 * Options for the constructor of Sequelize main class
179 */
180export interface Options extends Logging {
181 /**
182 * The dialect of the database you are connecting to. One of mysql, postgres, sqlite, mariadb and mssql.
183 *
184 * @default 'mysql'
185 */
186 dialect?: Dialect;
187
188 /**
189 * If specified, will use the provided module as the dialect.
190 *
191 * @example
192 * `dialectModule: require('@myorg/tedious'),`
193 */
194 dialectModule?: object;
195
196
197 /**
198 * If specified, load the dialect library from this path. For example, if you want to use pg.js instead of
199 * pg when connecting to a pg database, you should specify 'pg.js' here
200 */
201 dialectModulePath?: string;
202
203 /**
204 * An object of additional options, which are passed directly to the connection library
205 */
206 dialectOptions?: object;
207
208 /**
209 * Only used by sqlite.
210 *
211 * @default ':memory:'
212 */
213 storage?: string;
214
215 /**
216 * The name of the database
217 */
218 database?: string;
219
220 /**
221 * The username which is used to authenticate against the database.
222 */
223 username?: string;
224
225 /**
226 * The password which is used to authenticate against the database.
227 */
228 password?: string;
229
230 /**
231 * The host of the relational database.
232 *
233 * @default 'localhost'
234 */
235 host?: string;
236
237 /**
238 * The port of the relational database.
239 */
240 port?: number;
241
242 /**
243 * A flag that defines if is used SSL.
244 */
245 ssl?: boolean;
246
247 /**
248 * The protocol of the relational database.
249 *
250 * @default 'tcp'
251 */
252 protocol?: string;
253
254 /**
255 * Default options for model definitions. See Model.init.
256 */
257 define?: ModelOptions;
258
259 /**
260 * Default options for sequelize.query
261 */
262 query?: QueryOptions;
263
264 /**
265 * Default options for sequelize.set
266 */
267 set?: DefaultSetOptions;
268
269 /**
270 * Default options for sequelize.sync
271 */
272 sync?: SyncOptions;
273
274 /**
275 * The timezone used when converting a date from the database into a JavaScript date. The timezone is also
276 * used to SET TIMEZONE when connecting to the server, to ensure that the result of NOW, CURRENT_TIMESTAMP
277 * and other time related functions have in the right timezone. For best cross platform performance use the
278 * format
279 * +/-HH:MM. Will also accept string versions of timezones used by moment.js (e.g. 'America/Los_Angeles');
280 * this is useful to capture daylight savings time changes.
281 *
282 * @default '+00:00'
283 */
284 timezone?: string;
285
286 /**
287 * A flag that defines if null values should be passed to SQL queries or not.
288 *
289 * @default false
290 */
291 omitNull?: boolean;
292
293 /**
294 * A flag that defines if native library shall be used or not. Currently only has an effect for postgres
295 *
296 * @default false
297 */
298 native?: boolean;
299
300 /**
301 * Use read / write replication. To enable replication, pass an object, with two properties, read and write.
302 * Write should be an object (a single server for handling writes), and read an array of object (several
303 * servers to handle reads). Each read/write server can have the following properties: `host`, `port`,
304 * `username`, `password`, `database`
305 *
306 * @default false
307 */
308 replication?: ReplicationOptions | false;
309
310 /**
311 * Connection pool options
312 */
313 pool?: PoolOptions;
314
315 /**
316 * Set to `false` to make table names and attributes case-insensitive on Postgres and skip double quoting of
317 * them.
318 *
319 * @default true
320 */
321 quoteIdentifiers?: boolean;
322
323 /**
324 * Set the default transaction isolation level. See `Sequelize.Transaction.ISOLATION_LEVELS` for possible
325 * options.
326 *
327 * @default 'REPEATABLE_READ'
328 */
329 isolationLevel?: string;
330
331 /**
332 * Set the default transaction type. See Sequelize.Transaction.TYPES for possible options. Sqlite only.
333 *
334 * @default 'DEFERRED'
335 */
336 transactionType?: Transaction.TYPES;
337
338 /**
339 * Run built in type validators on insert and update, e.g. validate that arguments passed to integer
340 * fields are integer-like.
341 *
342 * @default false
343 */
344 typeValidation?: boolean;
345
346 /**
347 * Sets available operator aliases.
348 * See (https://sequelize.org/master/manual/querying.html#operators) for more information.
349 * WARNING: Setting this to boolean value was deprecated and is no-op.
350 *
351 * @default all aliases
352 */
353 operatorsAliases?: OperatorsAliases;
354
355
356 /**
357 * The PostgreSQL `standard_conforming_strings` session parameter. Set to `false` to not set the option.
358 * WARNING: Setting this to false may expose vulnerabilities and is not recommended!
359 *
360 * @default true
361 */
362 standardConformingStrings?: boolean;
363
364 /**
365 * The PostgreSQL `client_min_messages` session parameter.
366 * Set to `false` to not override the database's default.
367 *
368 * Deprecated in v7, please use the sequelize option "dialectOptions.clientMinMessages" instead
369 *
370 * @deprecated
371 * @default 'warning'
372 */
373 clientMinMessages?: string | boolean;
374
375 /**
376 * Sets global permanent hooks.
377 */
378 hooks?: Partial<SequelizeHooks<Model, any, any>>;
379
380 /**
381 * Set to `true` to automatically minify aliases generated by sequelize.
382 * Mostly useful to circumvent the POSTGRES alias limit of 64 characters.
383 *
384 * @default false
385 */
386 minifyAliases?: boolean;
387
388 /**
389 * Set to `true` to show bind parameters in log.
390 *
391 * @default false
392 */
393 logQueryParameters?: boolean;
394
395 retry?: RetryOptions;
396
397 /**
398 * If defined the connection will use the provided schema instead of the default ("public").
399 */
400 schema?: string;
401
402 /**
403 * Sequelize had to introduce a breaking change to fix vulnerability CVE-2023-22578.
404 * This option allows you to revert to the old behavior (unsafe-legacy), or to opt in to the new behavior (escape).
405 * The default behavior throws an error to warn you about the change (throw).
406 */
407 attributeBehavior?: 'escape' | 'throw' | 'unsafe-legacy';
408}
409
410export interface QueryOptionsTransactionRequired { }
411
412/**
413 * This is the main class, the entry point to sequelize. To use it, you just need to
414 * import sequelize:
415 *
416 * ```js
417 * const Sequelize = require('sequelize');
418 * ```
419 *
420 * In addition to sequelize, the connection library for the dialect you want to use
421 * should also be installed in your project. You don't need to import it however, as
422 * sequelize will take care of that.
423 */
424export class Sequelize extends Hooks {
425
426 // -------------------- Utilities ------------------------------------------------------------------------
427
428 /**
429 * Creates a object representing a database function. This can be used in search queries, both in where and
430 * order parts, and as default values in column definitions. If you want to refer to columns in your
431 * function, you should use `sequelize.col`, so that the columns are properly interpreted as columns and
432 * not a strings.
433 *
434 * Convert a user's username to upper case
435 * ```js
436 * instance.update({
437 * username: self.sequelize.fn('upper', self.sequelize.col('username'))
438 * })
439 * ```
440 *
441 * @param fn The function you want to call
442 * @param args All further arguments will be passed as arguments to the function
443 */
444 public static fn: typeof fn;
445 public fn: typeof fn;
446
447 /**
448 * Creates a object representing a column in the DB. This is often useful in conjunction with
449 * `sequelize.fn`, since raw string arguments to fn will be escaped.
450 *
451 * @param col The name of the column
452 */
453 public static col: typeof col;
454 public col: typeof col;
455
456 /**
457 * Creates a object representing a call to the cast function.
458 *
459 * @param val The value to cast
460 * @param type The type to cast it to
461 */
462 public static cast: typeof cast;
463 public cast: typeof cast;
464
465 /**
466 * Creates a object representing a literal, i.e. something that will not be escaped.
467 *
468 * @param val
469 */
470 public static literal: typeof literal;
471 public literal: typeof literal;
472
473 /**
474 * An AND query
475 *
476 * @param args Each argument will be joined by AND
477 */
478 public static and: typeof and;
479 public and: typeof and;
480
481 /**
482 * An OR query
483 *
484 * @param args Each argument will be joined by OR
485 */
486 public static or: typeof or;
487 public or: typeof or;
488
489 /**
490 * Creates an object representing nested where conditions for postgres's json data-type.
491 *
492 * @param conditionsOrPath A hash containing strings/numbers or other nested hash, a string using dot
493 * notation or a string using postgres json syntax.
494 * @param value An optional value to compare against. Produces a string of the form "<json path> =
495 * '<value>'".
496 */
497 public static json: typeof json;
498 public json: typeof json;
499
500 /**
501 * A way of specifying attr = condition.
502 *
503 * The attr can either be an object taken from `Model.rawAttributes` (for example `Model.rawAttributes.id`
504 * or
505 * `Model.rawAttributes.name`). The attribute should be defined in your model definition. The attribute can
506 * also be an object from one of the sequelize utility functions (`sequelize.fn`, `sequelize.col` etc.)
507 *
508 * For string attributes, use the regular `{ where: { attr: something }}` syntax. If you don't want your
509 * string to be escaped, use `sequelize.literal`.
510 *
511 * @param attr The attribute, which can be either an attribute object from `Model.rawAttributes` or a
512 * sequelize object, for example an instance of `sequelize.fn`. For simple string attributes, use the
513 * POJO syntax
514 * @param comparator Comparator
515 * @param logic The condition. Can be both a simply type, or a further condition (`.or`, `.and`, `.literal`
516 * etc.)
517 */
518 public static where: typeof where;
519 public where: typeof where;
520
521 /**
522 * A hook that is run before validation
523 *
524 * @param name
525 * @param fn A callback function that is called with instance, options
526 */
527 public static beforeValidate(name: string, fn: (instance: Model, options: ValidationOptions) => void): void;
528 public static beforeValidate(fn: (instance: Model, options: ValidationOptions) => void): void;
529
530 /**
531 * A hook that is run after validation
532 *
533 * @param name
534 * @param fn A callback function that is called with instance, options
535 */
536 public static afterValidate(name: string, fn: (instance: Model, options: ValidationOptions) => void): void;
537 public static afterValidate(fn: (instance: Model, options: ValidationOptions) => void): void;
538
539 /**
540 * A hook that is run before creating a single instance
541 *
542 * @param name
543 * @param fn A callback function that is called with attributes, options
544 */
545 public static beforeCreate(name: string, fn: (attributes: Model, options: CreateOptions<any>) => void): void;
546 public static beforeCreate(fn: (attributes: Model, options: CreateOptions<any>) => void): void;
547
548 /**
549 * A hook that is run after creating a single instance
550 *
551 * @param name
552 * @param fn A callback function that is called with attributes, options
553 */
554 public static afterCreate(name: string, fn: (attributes: Model, options: CreateOptions<any>) => void): void;
555 public static afterCreate(fn: (attributes: Model, options: CreateOptions<any>) => void): void;
556
557 /**
558 * A hook that is run before destroying a single instance
559 *
560 * @param name
561 * @param fn A callback function that is called with instance, options
562 */
563 public static beforeDestroy(name: string, fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
564 public static beforeDestroy(fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
565
566 /**
567 * A hook that is run after destroying a single instance
568 *
569 * @param name
570 * @param fn A callback function that is called with instance, options
571 */
572 public static afterDestroy(name: string, fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
573 public static afterDestroy(fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
574
575 /**
576 * A hook that is run before updating a single instance
577 *
578 * @param name
579 * @param fn A callback function that is called with instance, options
580 */
581 public static beforeUpdate(name: string, fn: (instance: Model, options: UpdateOptions<any>) => void): void;
582 public static beforeUpdate(fn: (instance: Model, options: UpdateOptions<any>) => void): void;
583
584 /**
585 * A hook that is run after updating a single instance
586 *
587 * @param name
588 * @param fn A callback function that is called with instance, options
589 */
590 public static afterUpdate(name: string, fn: (instance: Model, options: UpdateOptions<any>) => void): void;
591 public static afterUpdate(fn: (instance: Model, options: UpdateOptions<any>) => void): void;
592
593 /**
594 * A hook that is run before creating or updating a single instance, It proxies `beforeCreate` and `beforeUpdate`
595 *
596 * @param name
597 * @param fn A callback function that is called with instance, options
598 */
599 public static beforeSave(
600 name: string,
601 fn: (instance: Model, options: UpdateOptions<any> | CreateOptions<any>) => void
602 ): void;
603 public static beforeSave(fn: (instance: Model, options: UpdateOptions<any> | CreateOptions<any>) => void): void;
604
605 /**
606 * A hook that is run after creating or updating a single instance, It proxies `afterCreate` and `afterUpdate`
607 *
608 * @param name
609 * @param fn A callback function that is called with instance, options
610 */
611 public static afterSave(
612 name: string,
613 fn: (instance: Model, options: UpdateOptions<any> | CreateOptions<any>) => void
614 ): void;
615 public static afterSave(
616 fn: (instance: Model, options: UpdateOptions<any> | CreateOptions<any>) => void
617 ): void;
618
619 /**
620 * A hook that is run before creating instances in bulk
621 *
622 * @param name
623 * @param fn A callback function that is called with instances, options
624 */
625 public static beforeBulkCreate(
626 name: string,
627 fn: (instances: Model[], options: BulkCreateOptions<any>) => void
628 ): void;
629 public static beforeBulkCreate(fn: (instances: Model[], options: BulkCreateOptions<any>) => void): void;
630
631 /**
632 * A hook that is run after creating instances in bulk
633 *
634 * @param name
635 * @param fn A callback function that is called with instances, options
636 */
637 public static afterBulkCreate(
638 name: string, fn: (instances: Model[], options: BulkCreateOptions<any>) => void
639 ): void;
640 public static afterBulkCreate(fn: (instances: Model[], options: BulkCreateOptions<any>) => void): void;
641
642 /**
643 * A hook that is run before destroying instances in bulk
644 *
645 * @param name
646 * @param fn A callback function that is called with options
647 */
648 public static beforeBulkDestroy(name: string, fn: (options: BulkCreateOptions<any>) => void): void;
649 public static beforeBulkDestroy(fn: (options: BulkCreateOptions<any>) => void): void;
650
651 /**
652 * A hook that is run after destroying instances in bulk
653 *
654 * @param name
655 * @param fn A callback function that is called with options
656 */
657 public static afterBulkDestroy(name: string, fn: (options: DestroyOptions<any>) => void): void;
658 public static afterBulkDestroy(fn: (options: DestroyOptions<any>) => void): void;
659
660 /**
661 * A hook that is run after updating instances in bulk
662 *
663 * @param name
664 * @param fn A callback function that is called with options
665 */
666 public static beforeBulkUpdate(name: string, fn: (options: UpdateOptions<any>) => void): void;
667 public static beforeBulkUpdate(fn: (options: UpdateOptions<any>) => void): void;
668
669 /**
670 * A hook that is run after updating instances in bulk
671 *
672 * @param name
673 * @param fn A callback function that is called with options
674 */
675 public static afterBulkUpdate(name: string, fn: (options: UpdateOptions<any>) => void): void;
676 public static afterBulkUpdate(fn: (options: UpdateOptions<any>) => void): void;
677
678 /**
679 * A hook that is run before a find (select) query
680 *
681 * @param name
682 * @param fn A callback function that is called with options
683 */
684 public static beforeFind(name: string, fn: (options: FindOptions<any>) => void): void;
685 public static beforeFind(fn: (options: FindOptions<any>) => void): void;
686
687 /**
688 * A hook that is run before a connection is established
689 *
690 * @param name
691 * @param fn A callback function that is called with options
692 */
693 public static beforeConnect(name: string, fn: (options: DeepWriteable<Config>) => void): void;
694 public static beforeConnect(fn: (options: DeepWriteable<Config>) => void): void;
695
696 /**
697 * A hook that is run after a connection is established
698 *
699 * @param name
700 * @param fn A callback function that is called with options
701 */
702 public static afterConnect(name: string, fn: (connection: unknown, options: Config) => void): void;
703 public static afterConnect(fn: (connection: unknown, options: Config) => void): void;
704
705 /**
706 * A hook that is run before a connection is released
707 *
708 * @param name
709 * @param fn A callback function that is called with options
710 */
711 public static beforeDisconnect(name: string, fn: (connection: unknown) => void): void;
712 public static beforeDisconnect(fn: (connection: unknown) => void): void;
713
714 /**
715 * A hook that is run after a connection is released
716 *
717 * @param name
718 * @param fn A callback function that is called with options
719 */
720 public static afterDisconnect(name: string, fn: (connection: unknown) => void): void;
721 public static afterDisconnect(fn: (connection: unknown) => void): void;
722
723
724 /**
725 * A hook that is run before attempting to acquire a connection from the pool
726 *
727 * @param name
728 * @param fn A callback function that is called with options
729 */
730 public static beforePoolAcquire(name: string, fn: (options: GetConnectionOptions) => void): void;
731 public static beforePoolAcquire(fn: (options: GetConnectionOptions) => void): void;
732
733 /**
734 * A hook that is run after successfully acquiring a connection from the pool
735 *
736 * @param name
737 * @param fn A callback function that is called with options
738 */
739 public static afterPoolAcquire(name: string, fn: (connection: Connection, options: GetConnectionOptions) => void): void;
740 public static afterPoolAcquire(fn: (connection: Connection, options: GetConnectionOptions) => void): void;
741
742
743 /**
744 * A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
745 *
746 * @param name
747 * @param fn A callback function that is called with options
748 */
749 public static beforeFindAfterExpandIncludeAll(name: string, fn: (options: FindOptions<any>) => void): void;
750 public static beforeFindAfterExpandIncludeAll(fn: (options: FindOptions<any>) => void): void;
751
752 /**
753 * A hook that is run before a find (select) query, after all option parsing is complete
754 *
755 * @param name
756 * @param fn A callback function that is called with options
757 */
758 public static beforeFindAfterOptions(name: string, fn: (options: FindOptions<any>) => void): void;
759 public static beforeFindAfterOptions(fn: (options: FindOptions<any>) => void): void;
760
761 /**
762 * A hook that is run after a find (select) query
763 *
764 * @param name
765 * @param fn A callback function that is called with instance(s), options
766 */
767 public static afterFind(
768 name: string,
769 fn: (instancesOrInstance: Model[] | Model | null, options: FindOptions<any>) => void
770 ): void;
771 public static afterFind(
772 fn: (instancesOrInstance: Model[] | Model | null, options: FindOptions<any>) => void
773 ): void;
774
775 /**
776 * A hook that is run before a define call
777 *
778 * @param name
779 * @param fn A callback function that is called with attributes, options
780 */
781 public static beforeDefine<M extends Model>(
782 name: string,
783 fn: (attributes: ModelAttributes<M, CreationAttributes<M>>, options: ModelOptions<M>) => void
784 ): void;
785 public static beforeDefine<M extends Model>(
786 fn: (attributes: ModelAttributes<M, CreationAttributes<M>>, options: ModelOptions<M>) => void
787 ): void;
788
789 /**
790 * A hook that is run after a define call
791 *
792 * @param name
793 * @param fn A callback function that is called with factory
794 */
795 public static afterDefine(name: string, fn: (model: ModelType) => void): void;
796 public static afterDefine(fn: (model: ModelType) => void): void;
797
798 /**
799 * A hook that is run before Sequelize() call
800 *
801 * @param name
802 * @param fn A callback function that is called with config, options
803 */
804 public static beforeInit(name: string, fn: (config: Config, options: Options) => void): void;
805 public static beforeInit(fn: (config: Config, options: Options) => void): void;
806
807 /**
808 * A hook that is run after Sequelize() call
809 *
810 * @param name
811 * @param fn A callback function that is called with sequelize
812 */
813 public static afterInit(name: string, fn: (sequelize: Sequelize) => void): void;
814 public static afterInit(fn: (sequelize: Sequelize) => void): void;
815
816 /**
817 * A hook that is run before sequelize.sync call
818 *
819 * @param fn A callback function that is called with options passed to sequelize.sync
820 */
821 public static beforeBulkSync(dname: string, fn: (options: SyncOptions) => HookReturn): void;
822 public static beforeBulkSync(fn: (options: SyncOptions) => HookReturn): void;
823
824 /**
825 * A hook that is run after sequelize.sync call
826 *
827 * @param fn A callback function that is called with options passed to sequelize.sync
828 */
829 public static afterBulkSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
830 public static afterBulkSync(fn: (options: SyncOptions) => HookReturn): void;
831
832 /**
833 * A hook that is run before Model.sync call
834 *
835 * @param fn A callback function that is called with options passed to Model.sync
836 */
837 public static beforeSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
838 public static beforeSync(fn: (options: SyncOptions) => HookReturn): void;
839
840 /**
841 * A hook that is run after Model.sync call
842 *
843 * @param fn A callback function that is called with options passed to Model.sync
844 */
845 public static afterSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
846 public static afterSync(fn: (options: SyncOptions) => HookReturn): void;
847
848 /**
849 * Use CLS with Sequelize.
850 * CLS namespace provided is stored as `Sequelize._cls`
851 * and Promise is patched to use the namespace, using `cls-hooked` module.
852 *
853 * @param namespace
854 */
855 public static useCLS(namespace: object): typeof Sequelize;
856
857 /**
858 * A reference to Sequelize constructor from sequelize. Useful for accessing DataTypes, Errors etc.
859 */
860 public Sequelize: typeof Sequelize;
861
862 /**
863 * Final config that is used by sequelize.
864 */
865 public readonly config: Config;
866
867 public readonly modelManager: ModelManager;
868
869 public readonly connectionManager: ConnectionManager;
870
871 /**
872 * Dictionary of all models linked with this instance.
873 */
874 public readonly models: {
875 [key: string]: ModelCtor<Model>;
876 };
877
878 /**
879 * Instantiate sequelize with name of database, username and password
880 *
881 * #### Example usage
882 *
883 * ```javascript
884 * // without password and options
885 * const sequelize = new Sequelize('database', 'username')
886 *
887 * // without options
888 * const sequelize = new Sequelize('database', 'username', 'password')
889 *
890 * // without password / with blank password
891 * const sequelize = new Sequelize('database', 'username', null, {})
892 *
893 * // with password and options
894 * const sequelize = new Sequelize('my_database', 'john', 'doe', {})
895 *
896 * // with uri (see below)
897 * const sequelize = new Sequelize('mysql://localhost:3306/database', {})
898 * ```
899 *
900 * @param database The name of the database
901 * @param username The username which is used to authenticate against the
902 * database.
903 * @param password The password which is used to authenticate against the
904 * database.
905 * @param options An object with options.
906 */
907 constructor(database: string, username: string, password?: string, options?: Options);
908 constructor(database: string, username: string, options?: Options);
909 constructor(options?: Options);
910
911 /**
912 * Instantiate sequelize with an URI
913 *
914 * @param uri A full database URI
915 * @param options See above for possible options
916 */
917 constructor(uri: string, options?: Options);
918
919 /**
920 * A hook that is run before validation
921 *
922 * @param name
923 * @param fn A callback function that is called with instance, options
924 */
925 public beforeValidate(name: string, fn: (instance: Model, options: ValidationOptions) => void): void;
926 public beforeValidate(fn: (instance: Model, options: ValidationOptions) => void): void;
927
928 /**
929 * A hook that is run after validation
930 *
931 * @param name
932 * @param fn A callback function that is called with instance, options
933 */
934 public afterValidate(name: string, fn: (instance: Model, options: ValidationOptions) => void): void;
935 public afterValidate(fn: (instance: Model, options: ValidationOptions) => void): void;
936
937 /**
938 * A hook that is run before creating a single instance
939 *
940 * @param name
941 * @param fn A callback function that is called with attributes, options
942 */
943 public beforeCreate(name: string, fn: (attributes: Model, options: CreateOptions<any>) => void): void;
944 public beforeCreate(fn: (attributes: Model, options: CreateOptions<any>) => void): void;
945
946 /**
947 * A hook that is run after creating a single instance
948 *
949 * @param name
950 * @param fn A callback function that is called with attributes, options
951 */
952 public afterCreate(name: string, fn: (attributes: Model, options: CreateOptions<any>) => void): void;
953 public afterCreate(fn: (attributes: Model, options: CreateOptions<any>) => void): void;
954
955 /**
956 * A hook that is run before destroying a single instance
957 *
958 * @param name
959 * @param fn A callback function that is called with instance, options
960 */
961 public beforeDestroy(name: string, fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
962 public beforeDestroy(fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
963
964 /**
965 * A hook that is run after destroying a single instance
966 *
967 * @param name
968 * @param fn A callback function that is called with instance, options
969 */
970 public afterDestroy(name: string, fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
971 public afterDestroy(fn: (instance: Model, options: InstanceDestroyOptions) => void): void;
972
973 /**
974 * A hook that is run before updating a single instance
975 *
976 * @param name
977 * @param fn A callback function that is called with instance, options
978 */
979 public beforeUpdate(name: string, fn: (instance: Model, options: UpdateOptions<any>) => void): void;
980 public beforeUpdate(fn: (instance: Model, options: UpdateOptions<any>) => void): void;
981
982 /**
983 * A hook that is run after updating a single instance
984 *
985 * @param name
986 * @param fn A callback function that is called with instance, options
987 */
988 public afterUpdate(name: string, fn: (instance: Model, options: UpdateOptions<any>) => void): void;
989 public afterUpdate(fn: (instance: Model, options: UpdateOptions<any>) => void): void;
990
991 /**
992 * A hook that is run before creating instances in bulk
993 *
994 * @param name
995 * @param fn A callback function that is called with instances, options
996 */
997 public beforeBulkCreate(name: string, fn: (instances: Model[], options: BulkCreateOptions<any>) => void): void;
998 public beforeBulkCreate(fn: (instances: Model[], options: BulkCreateOptions<any>) => void): void;
999
1000 /**
1001 * A hook that is run after creating instances in bulk
1002 *
1003 * @param name
1004 * @param fn A callback function that is called with instances, options
1005 */
1006 public afterBulkCreate(name: string, fn: (instances: Model[], options: BulkCreateOptions<any>) => void): void;
1007 public afterBulkCreate(fn: (instances: Model[], options: BulkCreateOptions<any>) => void): void;
1008
1009 /**
1010 * A hook that is run before destroying instances in bulk
1011 *
1012 * @param name
1013 * @param fn A callback function that is called with options
1014 */
1015 public beforeBulkDestroy(name: string, fn: (options: BulkCreateOptions<any>) => void): void;
1016 public beforeBulkDestroy(fn: (options: BulkCreateOptions<any>) => void): void;
1017
1018 /**
1019 * A hook that is run after destroying instances in bulk
1020 *
1021 * @param name
1022 * @param fn A callback function that is called with options
1023 */
1024 public afterBulkDestroy(name: string, fn: (options: DestroyOptions<any>) => void): void;
1025 public afterBulkDestroy(fn: (options: DestroyOptions<any>) => void): void;
1026
1027 /**
1028 * A hook that is run after updating instances in bulk
1029 *
1030 * @param name
1031 * @param fn A callback function that is called with options
1032 */
1033 public beforeBulkUpdate(name: string, fn: (options: UpdateOptions<any>) => void): void;
1034 public beforeBulkUpdate(fn: (options: UpdateOptions<any>) => void): void;
1035
1036 /**
1037 * A hook that is run after updating instances in bulk
1038 *
1039 * @param name
1040 * @param fn A callback function that is called with options
1041 */
1042 public afterBulkUpdate(name: string, fn: (options: UpdateOptions<any>) => void): void;
1043 public afterBulkUpdate(fn: (options: UpdateOptions<any>) => void): void;
1044
1045 /**
1046 * A hook that is run before a find (select) query
1047 *
1048 * @param name
1049 * @param fn A callback function that is called with options
1050 */
1051 public beforeFind(name: string, fn: (options: FindOptions<any>) => void): void;
1052 public beforeFind(fn: (options: FindOptions<any>) => void): void;
1053
1054 /**
1055 * A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
1056 *
1057 * @param name
1058 * @param fn A callback function that is called with options
1059 */
1060 public beforeFindAfterExpandIncludeAll(name: string, fn: (options: FindOptions<any>) => void): void;
1061 public beforeFindAfterExpandIncludeAll(fn: (options: FindOptions<any>) => void): void;
1062
1063 /**
1064 * A hook that is run before a find (select) query, after all option parsing is complete
1065 *
1066 * @param name
1067 * @param fn A callback function that is called with options
1068 */
1069 public beforeFindAfterOptions(name: string, fn: (options: FindOptions<any>) => void): void;
1070 public beforeFindAfterOptions(fn: (options: FindOptions<any>) => void): void;
1071
1072 /**
1073 * A hook that is run after a find (select) query
1074 *
1075 * @param name
1076 * @param fn A callback function that is called with instance(s), options
1077 */
1078 public afterFind(
1079 name: string,
1080 fn: (instancesOrInstance: Model[] | Model | null, options: FindOptions<any>) => void
1081 ): void;
1082 public afterFind(fn: (instancesOrInstance: Model[] | Model | null, options: FindOptions<any>) => void): void;
1083
1084 /**
1085 * A hook that is run before a define call
1086 *
1087 * @param name
1088 * @param fn A callback function that is called with attributes, options
1089 */
1090 public beforeDefine(name: string, fn: (attributes: ModelAttributes<Model, any>, options: ModelOptions) => void): void;
1091 public beforeDefine(fn: (attributes: ModelAttributes<Model, any>, options: ModelOptions) => void): void;
1092
1093 /**
1094 * A hook that is run after a define call
1095 *
1096 * @param name
1097 * @param fn A callback function that is called with factory
1098 */
1099 public afterDefine(name: string, fn: (model: ModelType) => void): void;
1100 public afterDefine(fn: (model: ModelType) => void): void;
1101
1102 /**
1103 * A hook that is run before Sequelize() call
1104 *
1105 * @param name
1106 * @param fn A callback function that is called with config, options
1107 */
1108 public beforeInit(name: string, fn: (config: Config, options: Options) => void): void;
1109 public beforeInit(fn: (config: Config, options: Options) => void): void;
1110
1111 /**
1112 * A hook that is run after Sequelize() call
1113 *
1114 * @param name
1115 * @param fn A callback function that is called with sequelize
1116 */
1117 public afterInit(name: string, fn: (sequelize: Sequelize) => void): void;
1118 public afterInit(fn: (sequelize: Sequelize) => void): void;
1119
1120 /**
1121 * A hook that is run before sequelize.sync call
1122 *
1123 * @param fn A callback function that is called with options passed to sequelize.sync
1124 */
1125 public beforeBulkSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
1126 public beforeBulkSync(fn: (options: SyncOptions) => HookReturn): void;
1127
1128 /**
1129 * A hook that is run after sequelize.sync call
1130 *
1131 * @param fn A callback function that is called with options passed to sequelize.sync
1132 */
1133 public afterBulkSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
1134 public afterBulkSync(fn: (options: SyncOptions) => HookReturn): void;
1135
1136 /**
1137 * A hook that is run before Model.sync call
1138 *
1139 * @param fn A callback function that is called with options passed to Model.sync
1140 */
1141 public beforeSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
1142 public beforeSync(fn: (options: SyncOptions) => HookReturn): void;
1143
1144 /**
1145 * A hook that is run after Model.sync call
1146 *
1147 * @param fn A callback function that is called with options passed to Model.sync
1148 */
1149 public afterSync(name: string, fn: (options: SyncOptions) => HookReturn): void;
1150 public afterSync(fn: (options: SyncOptions) => HookReturn): void;
1151
1152 /**
1153 * Returns the specified dialect.
1154 */
1155 public getDialect(): string;
1156
1157 /**
1158 * Returns the database name.
1159 */
1160
1161 public getDatabaseName(): string;
1162
1163 /**
1164 * Returns an instance of QueryInterface.
1165 */
1166 public getQueryInterface(): QueryInterface;
1167
1168 /**
1169 * Define a new model, representing a table in the DB.
1170 *
1171 * The table columns are defined by the hash that is given as the second argument. Each attribute of the
1172 * hash
1173 * represents a column. A short table definition might look like this:
1174 *
1175 * ```js
1176 * class MyModel extends Model {}
1177 * MyModel.init({
1178 * columnA: {
1179 * type: Sequelize.BOOLEAN,
1180 * validate: {
1181 * is: ["[a-z]",'i'], // will only allow letters
1182 * max: 23, // only allow values <= 23
1183 * isIn: {
1184 * args: [['en', 'zh']],
1185 * msg: "Must be English or Chinese"
1186 * }
1187 * },
1188 * field: 'column_a'
1189 * // Other attributes here
1190 * },
1191 * columnB: Sequelize.STRING,
1192 * columnC: 'MY VERY OWN COLUMN TYPE'
1193 * }, { sequelize })
1194 *
1195 * sequelize.models.modelName // The model will now be available in models under the name given to define
1196 * ```
1197 *
1198 * As shown above, column definitions can be either strings, a reference to one of the datatypes that are
1199 * predefined on the Sequelize constructor, or an object that allows you to specify both the type of the
1200 * column, and other attributes such as default values, foreign key constraints and custom setters and
1201 * getters.
1202 *
1203 * For a list of possible data types, see
1204 * https://sequelize.org/master/en/latest/docs/models-definition/#data-types
1205 *
1206 * For more about getters and setters, see
1207 * https://sequelize.org/master/en/latest/docs/models-definition/#getters-setters
1208 *
1209 * For more about instance and class methods, see
1210 * https://sequelize.org/master/en/latest/docs/models-definition/#expansion-of-models
1211 *
1212 * For more about validation, see
1213 * https://sequelize.org/master/en/latest/docs/models-definition/#validations
1214 *
1215 * @param modelName The name of the model. The model will be stored in `sequelize.models` under this name
1216 * @param attributes An object, where each attribute is a column of the table. Each column can be either a
1217 * DataType, a string or a type-description object, with the properties described below:
1218 * @param options These options are merged with the default define options provided to the Sequelize
1219 * constructor
1220 */
1221 public define<M extends Model, TAttributes = Attributes<M>>(
1222 modelName: string,
1223 attributes: ModelAttributes<M, TAttributes>,
1224 options?: ModelOptions<M>
1225 ): ModelCtor<M>;
1226
1227 /**
1228 * Fetch a Model which is already defined
1229 *
1230 * @param modelName The name of a model defined with Sequelize.define
1231 */
1232 public model(modelName: string): ModelCtor<Model>;
1233
1234 /**
1235 * Checks whether a model with the given name is defined
1236 *
1237 * @param modelName The name of a model defined with Sequelize.define
1238 */
1239 public isDefined(modelName: string): boolean;
1240
1241 /**
1242 * Execute a query on the DB, optionally bypassing all the Sequelize goodness.
1243 *
1244 * By default, the function will return two arguments: an array of results, and a metadata object,
1245 * containing number of affected rows etc. Use `const [results, meta] = await ...` to access the results.
1246 *
1247 * If you are running a type of query where you don't need the metadata, for example a `SELECT` query, you
1248 * can pass in a query type to make sequelize format the results:
1249 *
1250 * ```js
1251 * const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
1252 *
1253 * const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
1254 * ```
1255 *
1256 * @param sql
1257 * @param options Query options
1258 */
1259 public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.UPDATE>): Promise<[undefined, number]>;
1260 public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.BULKUPDATE>): Promise<number>;
1261 public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.INSERT>): Promise<[number, number]>;
1262 public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.UPSERT>): Promise<number>;
1263 public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.DELETE>): Promise<void>;
1264 public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.BULKDELETE>): Promise<number>;
1265 public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.SHOWTABLES>): Promise<string[]>;
1266 public query(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.DESCRIBE>): Promise<ColumnsDescription>;
1267 public query<M extends Model>(
1268 sql: string | { query: string; values: unknown[] },
1269 options: QueryOptionsWithModel<M> & { plain: true }
1270 ): Promise<M | null>;
1271 public query<M extends Model>(
1272 sql: string | { query: string; values: unknown[] },
1273 options: QueryOptionsWithModel<M>
1274 ): Promise<M[]>;
1275 public query<T extends object>(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.SELECT> & { plain: true }): Promise<T | null>;
1276 public query<T extends object>(sql: string | { query: string; values: unknown[] }, options: QueryOptionsWithType<QueryTypes.SELECT>): Promise<T[]>;
1277 public query(sql: string | { query: string; values: unknown[] }, options: (QueryOptions | QueryOptionsWithType<QueryTypes.RAW>) & { plain: true }): Promise<{ [key: string]: unknown } | null>;
1278 public query(sql: string | { query: string; values: unknown[] }, options?: QueryOptions | QueryOptionsWithType<QueryTypes.RAW>): Promise<[unknown[], unknown]>;
1279
1280 /**
1281 * Get the fn for random based on the dialect
1282 */
1283 public random(): Fn;
1284
1285 /**
1286 * Execute a query which would set an environment or user variable. The variables are set per connection,
1287 * so this function needs a transaction.
1288 *
1289 * Only works for MySQL.
1290 *
1291 * @param variables object with multiple variables.
1292 * @param options Query options.
1293 */
1294 public set(variables: object, options: QueryOptionsTransactionRequired): Promise<unknown>;
1295
1296 /**
1297 * Escape value.
1298 *
1299 * @param value Value that needs to be escaped
1300 */
1301 public escape(value: string | number | Date): string;
1302
1303 /**
1304 * Create a new database schema.
1305 *
1306 * Note,that this is a schema in the
1307 * [postgres sense of the word](http://www.postgresql.org/docs/9.1/static/ddl-schemas.html),
1308 * not a database table. In mysql and sqlite, this command will do nothing.
1309 *
1310 * @param schema Name of the schema
1311 * @param options Options supplied
1312 */
1313 public createSchema(schema: string, options: Logging): Promise<unknown>;
1314
1315 /**
1316 * Show all defined schemas
1317 *
1318 * Note,that this is a schema in the
1319 * [postgres sense of the word](http://www.postgresql.org/docs/9.1/static/ddl-schemas.html),
1320 * not a database table. In mysql and sqlite, this will show all tables.
1321 *
1322 * @param options Options supplied
1323 */
1324 public showAllSchemas(options: Logging): Promise<object[]>;
1325
1326 /**
1327 * Drop a single schema
1328 *
1329 * Note,that this is a schema in the
1330 * [postgres sense of the word](http://www.postgresql.org/docs/9.1/static/ddl-schemas.html),
1331 * not a database table. In mysql and sqlite, this drop a table matching the schema name
1332 *
1333 * @param schema Name of the schema
1334 * @param options Options supplied
1335 */
1336 public dropSchema(schema: string, options: Logging): Promise<unknown[]>;
1337
1338 /**
1339 * Drop all schemas
1340 *
1341 * Note,that this is a schema in the
1342 * [postgres sense of the word](http://www.postgresql.org/docs/9.1/static/ddl-schemas.html),
1343 * not a database table. In mysql and sqlite, this is the equivalent of drop all tables.
1344 *
1345 * @param options Options supplied
1346 */
1347 public dropAllSchemas(options: Logging): Promise<unknown[]>;
1348
1349 /**
1350 * Sync all defined models to the DB.
1351 *
1352 * @param options Sync Options
1353 */
1354 public sync(options?: SyncOptions): Promise<this>;
1355
1356 /**
1357 * Truncate all tables defined through the sequelize models. This is done
1358 * by calling Model.truncate() on each model.
1359 *
1360 * @param [options] The options passed to Model.destroy in addition to truncate
1361 */
1362 public truncate(options?: DestroyOptions<any>): Promise<unknown[]>;
1363
1364 /**
1365 * Drop all tables defined through this sequelize instance. This is done by calling Model.drop on each model
1366 *
1367 * @param options The options passed to each call to Model.drop
1368 */
1369 public drop(options?: DropOptions): Promise<unknown[]>;
1370
1371 /**
1372 * Test the connection by trying to authenticate
1373 *
1374 * @param options Query Options for authentication
1375 */
1376 public authenticate(options?: QueryOptions): Promise<void>;
1377 public validate(options?: QueryOptions): Promise<void>;
1378
1379 /**
1380 * Start a transaction. When using transactions, you should pass the transaction in the options argument
1381 * in order for the query to happen under that transaction
1382 *
1383 * ```js
1384 * try {
1385 * const transaction = await sequelize.transaction();
1386 * const user = await User.findOne(..., { transaction });
1387 * await user.update(..., { transaction });
1388 * await transaction.commit();
1389 * } catch(err) {
1390 * await transaction.rollback();
1391 * }
1392 * })
1393 * ```
1394 *
1395 * A syntax for automatically committing or rolling back based on the promise chain resolution is also
1396 * supported:
1397 *
1398 * ```js
1399 * try {
1400 * await sequelize.transaction(transaction => { // Note that we pass a callback rather than awaiting the call with no arguments
1401 * const user = await User.findOne(..., {transaction});
1402 * await user.update(..., {transaction});
1403 * });
1404 * // Committed
1405 * } catch(err) {
1406 * // Rolled back
1407 * console.error(err);
1408 * }
1409 * ```
1410 *
1411 * If you have [CLS](https://github.com/Jeff-Lewis/cls-hooked) enabled, the transaction
1412 * will automatically be passed to any query that runs witin the callback. To enable CLS, add it do your
1413 * project, create a namespace and set it on the sequelize constructor:
1414 *
1415 * ```js
1416 * const cls = require('cls-hooked');
1417 * const namespace = cls.createNamespace('....');
1418 * const Sequelize = require('sequelize');
1419 * Sequelize.useCLS(namespace);
1420 * ```
1421 * Note, that CLS is enabled for all sequelize instances, and all instances will share the same namespace
1422 *
1423 * @param options Transaction Options
1424 * @param autoCallback Callback for the transaction
1425 */
1426 public transaction<T>(options: TransactionOptions, autoCallback: (t: Transaction) => PromiseLike<T>): Promise<T>;
1427 public transaction<T>(autoCallback: (t: Transaction) => PromiseLike<T>): Promise<T>;
1428 public transaction(options?: TransactionOptions): Promise<Transaction>;
1429
1430 /**
1431 * Close all connections used by this sequelize instance, and free all references so the instance can be
1432 * garbage collected.
1433 *
1434 * Normally this is done on process exit, so you only need to call this method if you are creating multiple
1435 * instances, and want to garbage collect some of them.
1436 */
1437 public close(): Promise<void>;
1438
1439 /**
1440 * Returns the database version
1441 */
1442 public databaseVersion(): Promise<string>;
1443}
1444
1445// Utilities
1446
1447/**
1448 * Creates a object representing a database function. This can be used in search queries, both in where and
1449 * order parts, and as default values in column definitions. If you want to refer to columns in your
1450 * function, you should use `sequelize.col`, so that the columns are properly interpreted as columns and
1451 * not a strings.
1452 *
1453 * Convert a user's username to upper case
1454 * ```js
1455 * instance.update({
1456 * username: self.sequelize.fn('upper', self.sequelize.col('username'))
1457 * })
1458 * ```
1459 *
1460 * @param fn The function you want to call
1461 * @param args All further arguments will be passed as arguments to the function
1462 */
1463export function fn(fn: string, ...args: unknown[]): Fn;
1464
1465/**
1466 * Creates a object representing a column in the DB. This is often useful in conjunction with
1467 * `sequelize.fn`, since raw string arguments to fn will be escaped.
1468 *
1469 * @param col The name of the column
1470 */
1471export function col(col: string): Col;
1472
1473/**
1474 * Creates a object representing a call to the cast function.
1475 *
1476 * @param val The value to cast
1477 * @param type The type to cast it to
1478 */
1479export function cast(val: unknown, type: string): Cast;
1480
1481/**
1482 * Creates a object representing a literal, i.e. something that will not be escaped.
1483 *
1484 * @param val
1485 */
1486export function literal(val: string): Literal;
1487
1488/**
1489 * An AND query
1490 *
1491 * @param args Each argument will be joined by AND
1492 */
1493export function and<T extends Array<any>>(...args: T): { [Op.and]: T };
1494
1495/**
1496 * An OR query
1497 *
1498 * @param args Each argument will be joined by OR
1499 */
1500export function or<T extends Array<any>>(...args: T): { [Op.or]: T };
1501
1502/**
1503 * Creates an object representing nested where conditions for postgres's json data-type.
1504 *
1505 * @param conditionsOrPath A hash containing strings/numbers or other nested hash, a string using dot
1506 * notation or a string using postgres json syntax.
1507 * @param value An optional value to compare against. Produces a string of the form "<json path> =
1508 * '<value>'".
1509 */
1510export function json(conditionsOrPath: string | object, value?: string | number | boolean): Json;
1511
1512export type WhereLeftOperand = Fn | ColumnReference | Literal | Cast | ModelAttributeColumnOptions;
1513
1514// TODO [>6]: Remove
1515/**
1516 * @deprecated use {@link WhereLeftOperand} instead.
1517 */
1518export type AttributeType = WhereLeftOperand;
1519
1520// TODO [>6]: Remove
1521/**
1522 * @deprecated this is not used anymore, typing definitions for {@link where} have changed to more accurately reflect reality.
1523 */
1524export type LogicType = Fn | Col | Literal | OrOperator<any> | AndOperator<any> | WhereOperators | string | symbol | null;
1525
1526/**
1527 * A way of specifying "attr = condition".
1528 * Can be used as a replacement for the POJO syntax (e.g. `where: { name: 'Lily' }`) when you need to compare a column that the POJO syntax cannot represent.
1529 *
1530 * @param leftOperand The left side of the comparison.
1531 * - A value taken from YourModel.rawAttributes, to reference an attribute.
1532 * The attribute must be defined in your model definition.
1533 * - A Literal (using {@link Sequelize#literal})
1534 * - A SQL Function (using {@link Sequelize#fn})
1535 * - A Column name (using {@link Sequelize#col})
1536 * Note that simple strings to reference an attribute are not supported. You can use the POJO syntax instead.
1537 * @param operator The comparison operator to use. If unspecified, defaults to {@link Op.eq}.
1538 * @param rightOperand The right side of the comparison. Its value depends on the used operator.
1539 * See {@link WhereOperators} for information about what value is valid for each operator.
1540 *
1541 * @example
1542 * // Using an attribute as the left operand.
1543 * // Equal to: WHERE first_name = 'Lily'
1544 * where(User.rawAttributes.firstName, Op.eq, 'Lily');
1545 *
1546 * @example
1547 * // Using a column name as the left operand.
1548 * // Equal to: WHERE first_name = 'Lily'
1549 * where(col('first_name'), Op.eq, 'Lily');
1550 *
1551 * @example
1552 * // Using a SQL function on the left operand.
1553 * // Equal to: WHERE LOWER(first_name) = 'lily'
1554 * where(fn('LOWER', col('first_name')), Op.eq, 'lily');
1555 *
1556 * @example
1557 * // Using raw SQL as the left operand.
1558 * // Equal to: WHERE 'Lily' = 'Lily'
1559 * where(literal(`'Lily'`), Op.eq, 'Lily');
1560 */
1561export function where<Op extends keyof WhereOperators>(leftOperand: WhereLeftOperand | Where, operator: Op, rightOperand: WhereOperators[Op]): Where;
1562export function where<Op extends keyof WhereOperators>(leftOperand: any, operator: string, rightOperand: any): Where;
1563export function where(leftOperand: WhereLeftOperand, rightOperand: WhereAttributeHashValue<any>): Where;
1564
1565export default Sequelize;
1566
\No newline at end of file