UNPKG

17 kBTypeScriptView Raw
1import { DataType } from './data-types';
2import { Logging, Model, ModelAttributeColumnOptions, ModelAttributes, Transactionable, WhereOptions, Filterable, Poolable } from './model';
3import { Promise } from './promise';
4import QueryTypes = require('./query-types');
5import { Sequelize, RetryOptions } from './sequelize';
6import { Transaction } from './transaction';
7
8type BindOrReplacements = { [key: string]: unknown } | unknown[];
9
10/**
11 * Interface for query options
12 */
13export interface QueryOptions extends Logging, Transactionable, Poolable {
14 /**
15 * If true, sequelize will not try to format the results of the query, or build an instance of a model from
16 * the result
17 */
18 raw?: boolean;
19
20 /**
21 * The type of query you are executing. The query type affects how results are formatted before they are
22 * passed back. The type is a string, but `Sequelize.QueryTypes` is provided as convenience shortcuts.
23 */
24 type?: string;
25
26 /**
27 * If true, transforms objects with `.` separated property names into nested objects using
28 * [dottie.js](https://github.com/mickhansen/dottie.js). For example { 'user.username': 'john' } becomes
29 * { user: { username: 'john' }}. When `nest` is true, the query type is assumed to be `'SELECT'`,
30 * unless otherwise specified
31 *
32 * @default false
33 */
34 nest?: boolean;
35
36 /**
37 * Sets the query type to `SELECT` and return a single row
38 */
39 plain?: boolean;
40
41 /**
42 * Either an object of named parameter replacements in the format `:param` or an array of unnamed
43 * replacements to replace `?` in your SQL.
44 */
45 replacements?: BindOrReplacements;
46
47 /**
48 * Either an object of named parameter bindings in the format `$param` or an array of unnamed
49 * values to bind to `$1`, `$2`, etc in your SQL.
50 */
51 bind?: BindOrReplacements;
52
53 /**
54 * A sequelize instance used to build the return instance
55 */
56 instance?: Model;
57
58 /**
59 * Map returned fields to model's fields if `options.model` or `options.instance` is present.
60 * Mapping will occur before building the model instance.
61 */
62 mapToModel?: boolean;
63
64 retry?: RetryOptions;
65}
66
67export interface QueryOptionsWithWhere extends QueryOptions, Filterable {
68
69}
70
71export interface QueryOptionsWithModel extends QueryOptions {
72 /**
73 * A sequelize model used to build the returned model instances (used to be called callee)
74 */
75 model: typeof Model;
76}
77
78export interface QueryOptionsWithType<T extends QueryTypes> extends QueryOptions {
79 /**
80 * The type of query you are executing. The query type affects how results are formatted before they are
81 * passed back. The type is a string, but `Sequelize.QueryTypes` is provided as convenience shortcuts.
82 */
83 type: T;
84}
85
86export interface QueryOptionsWithForce extends QueryOptions {
87 force?: boolean;
88}
89
90/**
91* Most of the methods accept options and use only the logger property of the options. That's why the most used
92* interface type for options in a method is separated here as another interface.
93*/
94export interface QueryInterfaceOptions extends Logging, Transactionable {}
95
96export interface CollateCharsetOptions {
97 collate?: string;
98 charset?: string;
99}
100
101export interface QueryInterfaceCreateTableOptions extends QueryInterfaceOptions, CollateCharsetOptions {
102 engine?: string;
103 /**
104 * Used for compound unique keys.
105 */
106 uniqueKeys?: {
107 [keyName: string]: {
108 fields: string[];
109 customIndex?: boolean;
110 };
111 };
112}
113
114export interface QueryInterfaceDropTableOptions extends QueryInterfaceOptions {
115 cascade?: boolean;
116 force?: boolean;
117}
118
119export interface QueryInterfaceDropAllTablesOptions extends QueryInterfaceOptions {
120 skip?: string[];
121}
122
123export type IndexType = 'UNIQUE' | 'FULLTEXT' | 'SPATIAL';
124export type IndexMethod = 'BTREE' | 'HASH' | 'GIST' | 'SPGIST' | 'GIN' | 'BRIN' | string;
125
126export interface IndexesOptions {
127 /**
128 * The name of the index. Defaults to model name + _ + fields concatenated
129 */
130 name?: string;
131
132 /** For FULLTEXT columns set your parser */
133 parser?: string | null;
134
135 /**
136 * Index type. Only used by mysql. One of `UNIQUE`, `FULLTEXT` and `SPATIAL`
137 */
138 type?: IndexType;
139
140 /**
141 * Should the index by unique? Can also be triggered by setting type to `UNIQUE`
142 *
143 * @default false
144 */
145 unique?: boolean;
146
147 /**
148 * PostgreSQL will build the index without taking any write locks. Postgres only
149 *
150 * @default false
151 */
152 concurrently?: boolean;
153
154 /**
155 * An array of the fields to index. Each field can either be a string containing the name of the field,
156 * a sequelize object (e.g `sequelize.fn`), or an object with the following attributes: `name`
157 * (field name), `length` (create a prefix index of length chars), `order` (the direction the column
158 * should be sorted in), `collate` (the collation (sort order) for the column)
159 */
160 fields?: (string | { name: string; length?: number; order?: 'ASC' | 'DESC'; collate?: string })[];
161
162 /**
163 * The method to create the index by (`USING` statement in SQL). BTREE and HASH are supported by mysql and
164 * postgres, and postgres additionally supports GIST, SPGIST, BRIN and GIN.
165 */
166 using?: IndexMethod;
167
168 /**
169 * Index operator type. Postgres only
170 */
171 operator?: string;
172
173 /**
174 * Optional where parameter for index. Can be used to limit the index to certain rows.
175 */
176 where?: WhereOptions;
177
178 /**
179 * Prefix to append to the index name.
180 */
181 prefix?: string;
182}
183
184export interface QueryInterfaceIndexOptions extends IndexesOptions, QueryInterfaceOptions {}
185
186export interface AddUniqueConstraintOptions {
187 type: 'unique';
188 name?: string;
189}
190
191export interface AddDefaultConstraintOptions {
192 type: 'default';
193 name?: string;
194 defaultValue?: unknown;
195}
196
197export interface AddCheckConstraintOptions {
198 type: 'check';
199 name?: string;
200 where?: WhereOptions;
201}
202
203export interface AddPrimaryKeyConstraintOptions {
204 type: 'primary key';
205 name?: string;
206}
207
208export interface AddForeignKeyConstraintOptions {
209 type: 'foreign key';
210 name?: string;
211 references?: {
212 table: string;
213 field: string;
214 };
215 onDelete: string;
216 onUpdate: string;
217}
218
219export type AddConstraintOptions =
220| AddUniqueConstraintOptions
221| AddDefaultConstraintOptions
222| AddCheckConstraintOptions
223| AddPrimaryKeyConstraintOptions
224| AddForeignKeyConstraintOptions;
225
226export interface CreateDatabaseOptions extends CollateCharsetOptions, QueryOptions {
227 encoding?: string;
228}
229
230export interface FunctionParam {
231 type: string;
232 name?: string;
233 direction?: string;
234}
235
236/**
237* The interface that Sequelize uses to talk to all databases.
238*
239* This interface is available through sequelize.QueryInterface. It should not be commonly used, but it's
240* referenced anyway, so it can be used.
241*/
242export class QueryInterface {
243 /**
244 * Returns the dialect-specific sql generator.
245 *
246 * We don't have a definition for the QueryGenerator, because I doubt it is commonly in use separately.
247 */
248 public QueryGenerator: unknown;
249
250 /**
251 * Returns the current sequelize instance.
252 */
253 public sequelize: Sequelize;
254
255 constructor(sequelize: Sequelize);
256
257 /**
258 * Queries the schema (table list).
259 *
260 * @param schema The schema to query. Applies only to Postgres.
261 */
262 public createSchema(schema?: string, options?: QueryInterfaceOptions): Promise<void>;
263
264 /**
265 * Drops the specified schema (table).
266 *
267 * @param schema The schema to query. Applies only to Postgres.
268 */
269 public dropSchema(schema?: string, options?: QueryInterfaceOptions): Promise<void>;
270
271 /**
272 * Drops all tables.
273 */
274 public dropAllSchemas(options?: QueryInterfaceDropAllTablesOptions): Promise<void>;
275
276 /**
277 * Queries all table names in the database.
278 *
279 * @param options
280 */
281 public showAllSchemas(options?: QueryOptions): Promise<object>;
282
283 /**
284 * Return database version
285 */
286 public databaseVersion(options?: QueryInterfaceOptions): Promise<string>;
287
288 /**
289 * Creates a table with specified attributes.
290 *
291 * @param tableName Name of table to create
292 * @param attributes Hash of attributes, key is attribute name, value is data type
293 * @param options Table options.
294 */
295 public createTable(
296 tableName: string | { schema?: string; tableName?: string },
297 attributes: ModelAttributes,
298 options?: QueryInterfaceCreateTableOptions
299 ): Promise<void>;
300
301 /**
302 * Drops the specified table.
303 *
304 * @param tableName Table name.
305 * @param options Query options, particularly "force".
306 */
307 public dropTable(tableName: string, options?: QueryInterfaceDropTableOptions): Promise<void>;
308
309 /**
310 * Drops all tables.
311 *
312 * @param options
313 */
314 public dropAllTables(options?: QueryInterfaceDropTableOptions): Promise<void>;
315
316 /**
317 * Drops all defined enums
318 *
319 * @param options
320 */
321 public dropAllEnums(options?: QueryOptions): Promise<void>;
322
323 /**
324 * Renames a table
325 */
326 public renameTable(before: string, after: string, options?: QueryInterfaceOptions): Promise<void>;
327
328 /**
329 * Returns all tables
330 */
331 public showAllTables(options?: QueryOptions): Promise<string[]>;
332
333 /**
334 * Describe a table
335 */
336 public describeTable(
337 tableName: string | { schema?: string; tableName?: string },
338 options?: string | { schema?: string; schemaDelimiter?: string } & Logging
339 ): Promise<object>;
340
341 /**
342 * Adds a new column to a table
343 */
344 public addColumn(
345 table: string | { schema?: string; tableName?: string },
346 key: string,
347 attribute: ModelAttributeColumnOptions | DataType,
348 options?: QueryInterfaceOptions
349 ): Promise<void>;
350
351 /**
352 * Removes a column from a table
353 */
354 public removeColumn(
355 table: string | { schema?: string; tableName?: string },
356 attribute: string,
357 options?: QueryInterfaceOptions
358 ): Promise<void>;
359
360 /**
361 * Changes a column
362 */
363 public changeColumn(
364 tableName: string | { schema?: string; tableName?: string },
365 attributeName: string,
366 dataTypeOrOptions?: DataType | ModelAttributeColumnOptions,
367 options?: QueryInterfaceOptions
368 ): Promise<void>;
369
370 /**
371 * Renames a column
372 */
373 public renameColumn(
374 tableName: string | { schema?: string; tableName?: string },
375 attrNameBefore: string,
376 attrNameAfter: string,
377 options?: QueryInterfaceOptions
378 ): Promise<void>;
379
380 /**
381 * Adds a new index to a table
382 */
383 public addIndex(
384 tableName: string,
385 attributes: string[],
386 options?: QueryInterfaceIndexOptions,
387 rawTablename?: string
388 ): Promise<void>;
389 public addIndex(
390 tableName: string,
391 options: QueryInterfaceIndexOptions & { fields: string[] },
392 rawTablename?: string
393 ): Promise<void>;
394
395 /**
396 * Removes an index of a table
397 */
398 public removeIndex(tableName: string, indexName: string, options?: QueryInterfaceIndexOptions): Promise<void>;
399 public removeIndex(tableName: string, attributes: string[], options?: QueryInterfaceIndexOptions): Promise<void>;
400
401 /**
402 * Adds constraints to a table
403 */
404 public addConstraint(
405 tableName: string,
406 attributes: string[],
407 options?: AddConstraintOptions & QueryInterfaceOptions
408 ): Promise<void>;
409
410 /**
411 * Removes constraints from a table
412 */
413 public removeConstraint(tableName: string, constraintName: string, options?: QueryInterfaceOptions): Promise<void>;
414
415 /**
416 * Shows the index of a table
417 */
418 public showIndex(tableName: string | object, options?: QueryOptions): Promise<object>;
419
420 /**
421 * Put a name to an index
422 */
423 public nameIndexes(indexes: string[], rawTablename: string): Promise<void>;
424
425 /**
426 * Returns all foreign key constraints of a table
427 */
428 public getForeignKeysForTables(tableNames: string, options?: QueryInterfaceOptions): Promise<object>;
429
430 /**
431 * Inserts a new record
432 */
433 public insert(instance: Model, tableName: string, values: object, options?: QueryOptions): Promise<object>;
434
435 /**
436 * Inserts or Updates a record in the database
437 */
438 public upsert(
439 tableName: string,
440 values: object,
441 updateValues: object,
442 model: typeof Model,
443 options?: QueryOptions
444 ): Promise<object>;
445
446 /**
447 * Inserts multiple records at once
448 */
449 public bulkInsert(
450 tableName: string,
451 records: object[],
452 options?: QueryOptions,
453 attributes?: string[] | string
454 ): Promise<object>;
455
456 /**
457 * Updates a row
458 */
459 public update(
460 instance: Model,
461 tableName: string,
462 values: object,
463 identifier: WhereOptions,
464 options?: QueryOptions
465 ): Promise<object>;
466
467 /**
468 * Updates multiple rows at once
469 */
470 public bulkUpdate(
471 tableName: string,
472 values: object,
473 identifier: WhereOptions,
474 options?: QueryOptions,
475 attributes?: string[] | string
476 ): Promise<object>;
477
478 /**
479 * Deletes a row
480 */
481 public delete(instance: Model | null, tableName: string, identifier: WhereOptions, options?: QueryOptions): Promise<object>;
482
483 /**
484 * Deletes multiple rows at once
485 */
486 public bulkDelete(
487 tableName: string,
488 identifier: WhereOptions,
489 options?: QueryOptions,
490 model?: typeof Model
491 ): Promise<object>;
492
493 /**
494 * Returns selected rows
495 */
496 public select(model: typeof Model | null, tableName: string, options?: QueryOptionsWithWhere): Promise<object[]>;
497
498 /**
499 * Increments a row value
500 */
501 public increment(
502 instance: Model,
503 tableName: string,
504 values: object,
505 identifier: WhereOptions,
506 options?: QueryOptions
507 ): Promise<object>;
508
509 /**
510 * Selects raw without parsing the string into an object
511 */
512 public rawSelect(
513 tableName: string,
514 options: QueryOptionsWithWhere,
515 attributeSelector: string | string[],
516 model?: typeof Model
517 ): Promise<string[]>;
518
519 /**
520 * Postgres only. Creates a trigger on specified table to call the specified function with supplied
521 * parameters.
522 */
523 public createTrigger(
524 tableName: string,
525 triggerName: string,
526 timingType: string,
527 fireOnArray: {
528 [key: string]: unknown;
529 }[],
530 functionName: string,
531 functionParams: FunctionParam[],
532 optionsArray: string[],
533 options?: QueryInterfaceOptions
534 ): Promise<void>;
535
536 /**
537 * Postgres only. Drops the specified trigger.
538 */
539 public dropTrigger(tableName: string, triggerName: string, options?: QueryInterfaceOptions): Promise<void>;
540
541 /**
542 * Postgres only. Renames a trigger
543 */
544 public renameTrigger(
545 tableName: string,
546 oldTriggerName: string,
547 newTriggerName: string,
548 options?: QueryInterfaceOptions
549 ): Promise<void>;
550
551 /**
552 * Postgres only. Create a function
553 */
554 public createFunction(
555 functionName: string,
556 params: FunctionParam[],
557 returnType: string,
558 language: string,
559 body: string,
560 optionsArray?: string[],
561 options?: QueryOptionsWithForce
562 ): Promise<void>;
563
564 /**
565 * Postgres only. Drops a function
566 */
567 public dropFunction(functionName: string, params: FunctionParam[], options?: QueryInterfaceOptions): Promise<void>;
568
569 /**
570 * Postgres only. Rename a function
571 */
572 public renameFunction(
573 oldFunctionName: string,
574 params: FunctionParam[],
575 newFunctionName: string,
576 options?: QueryInterfaceOptions
577 ): Promise<void>;
578
579 /**
580 * Escape an identifier (e.g. a table or attribute name). If force is true, the identifier will be quoted
581 * even if the `quoteIdentifiers` option is false.
582 */
583 public quoteIdentifier(identifier: string, force: boolean): string;
584
585 /**
586 * Escape a table name
587 */
588 public quoteTable(identifier: string): string;
589
590 /**
591 * Split an identifier into .-separated tokens and quote each part. If force is true, the identifier will be
592 * quoted even if the `quoteIdentifiers` option is false.
593 */
594 public quoteIdentifiers(identifiers: string, force: boolean): string;
595
596 /**
597 * Escape a value (e.g. a string, number or date)
598 */
599 public escape(value?: string | number | Date): string;
600
601 /**
602 * Set option for autocommit of a transaction
603 */
604 public setAutocommit(transaction: Transaction, value: boolean, options?: QueryOptions): Promise<void>;
605
606 /**
607 * Set the isolation level of a transaction
608 */
609 public setIsolationLevel(transaction: Transaction, value: string, options?: QueryOptions): Promise<void>;
610
611 /**
612 * Begin a new transaction
613 */
614 public startTransaction(transaction: Transaction, options?: QueryOptions): Promise<void>;
615
616 /**
617 * Defer constraints
618 */
619 public deferConstraints(transaction: Transaction, options?: QueryOptions): Promise<void>;
620
621 /**
622 * Commit an already started transaction
623 */
624 public commitTransaction(transaction: Transaction, options?: QueryOptions): Promise<void>;
625
626 /**
627 * Rollback ( revert ) a transaction that has'nt been commited
628 */
629 public rollbackTransaction(transaction: Transaction, options?: QueryOptions): Promise<void>;
630
631 /**
632 * Creates a database
633 */
634 public createDatabase(name: string, options?: CreateDatabaseOptions): Promise<void>;
635
636 /**
637 * Creates a database
638 */
639 public dropDatabase(name: string, options?: QueryOptions): Promise<void>;
640}