1 | import { DataType } from './data-types';
|
2 | import { Logging, Model, ModelAttributeColumnOptions, ModelAttributes, Transactionable, WhereOptions, Filterable, Poolable } from './model';
|
3 | import { Promise } from './promise';
|
4 | import QueryTypes = require('./query-types');
|
5 | import { Sequelize, RetryOptions } from './sequelize';
|
6 | import { Transaction } from './transaction';
|
7 |
|
8 | type BindOrReplacements = { [key: string]: unknown } | unknown[];
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export interface QueryOptions extends Logging, Transactionable, Poolable {
|
14 | |
15 |
|
16 |
|
17 |
|
18 | raw?: boolean;
|
19 |
|
20 | |
21 |
|
22 |
|
23 |
|
24 | type?: string;
|
25 |
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | nest?: boolean;
|
35 |
|
36 | |
37 |
|
38 |
|
39 | plain?: boolean;
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 | replacements?: BindOrReplacements;
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
51 | bind?: BindOrReplacements;
|
52 |
|
53 | |
54 |
|
55 |
|
56 | instance?: Model;
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 | mapToModel?: boolean;
|
63 |
|
64 | retry?: RetryOptions;
|
65 | }
|
66 |
|
67 | export interface QueryOptionsWithWhere extends QueryOptions, Filterable {
|
68 |
|
69 | }
|
70 |
|
71 | export interface QueryOptionsWithModel extends QueryOptions {
|
72 | |
73 |
|
74 |
|
75 | model: typeof Model;
|
76 | }
|
77 |
|
78 | export interface QueryOptionsWithType<T extends QueryTypes> extends QueryOptions {
|
79 | |
80 |
|
81 |
|
82 |
|
83 | type: T;
|
84 | }
|
85 |
|
86 | export interface QueryOptionsWithForce extends QueryOptions {
|
87 | force?: boolean;
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | export interface QueryInterfaceOptions extends Logging, Transactionable {}
|
95 |
|
96 | export interface CollateCharsetOptions {
|
97 | collate?: string;
|
98 | charset?: string;
|
99 | }
|
100 |
|
101 | export interface QueryInterfaceCreateTableOptions extends QueryInterfaceOptions, CollateCharsetOptions {
|
102 | engine?: string;
|
103 | |
104 |
|
105 |
|
106 | uniqueKeys?: {
|
107 | [keyName: string]: {
|
108 | fields: string[];
|
109 | customIndex?: boolean;
|
110 | };
|
111 | };
|
112 | }
|
113 |
|
114 | export interface QueryInterfaceDropTableOptions extends QueryInterfaceOptions {
|
115 | cascade?: boolean;
|
116 | force?: boolean;
|
117 | }
|
118 |
|
119 | export interface QueryInterfaceDropAllTablesOptions extends QueryInterfaceOptions {
|
120 | skip?: string[];
|
121 | }
|
122 |
|
123 | export type IndexType = 'UNIQUE' | 'FULLTEXT' | 'SPATIAL';
|
124 | export type IndexMethod = 'BTREE' | 'HASH' | 'GIST' | 'SPGIST' | 'GIN' | 'BRIN' | string;
|
125 |
|
126 | export interface IndexesOptions {
|
127 | |
128 |
|
129 |
|
130 | name?: string;
|
131 |
|
132 |
|
133 | parser?: string | null;
|
134 |
|
135 | |
136 |
|
137 |
|
138 | type?: IndexType;
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 | unique?: boolean;
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 | concurrently?: boolean;
|
153 |
|
154 | |
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | fields?: (string | { name: string; length?: number; order?: 'ASC' | 'DESC'; collate?: string })[];
|
161 |
|
162 | |
163 |
|
164 |
|
165 |
|
166 | using?: IndexMethod;
|
167 |
|
168 | |
169 |
|
170 |
|
171 | operator?: string;
|
172 |
|
173 | |
174 |
|
175 |
|
176 | where?: WhereOptions;
|
177 |
|
178 | |
179 |
|
180 |
|
181 | prefix?: string;
|
182 | }
|
183 |
|
184 | export interface QueryInterfaceIndexOptions extends IndexesOptions, QueryInterfaceOptions {}
|
185 |
|
186 | export interface AddUniqueConstraintOptions {
|
187 | type: 'unique';
|
188 | name?: string;
|
189 | }
|
190 |
|
191 | export interface AddDefaultConstraintOptions {
|
192 | type: 'default';
|
193 | name?: string;
|
194 | defaultValue?: unknown;
|
195 | }
|
196 |
|
197 | export interface AddCheckConstraintOptions {
|
198 | type: 'check';
|
199 | name?: string;
|
200 | where?: WhereOptions;
|
201 | }
|
202 |
|
203 | export interface AddPrimaryKeyConstraintOptions {
|
204 | type: 'primary key';
|
205 | name?: string;
|
206 | }
|
207 |
|
208 | export 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 |
|
219 | export type AddConstraintOptions =
|
220 | | AddUniqueConstraintOptions
|
221 | | AddDefaultConstraintOptions
|
222 | | AddCheckConstraintOptions
|
223 | | AddPrimaryKeyConstraintOptions
|
224 | | AddForeignKeyConstraintOptions;
|
225 |
|
226 | export interface CreateDatabaseOptions extends CollateCharsetOptions, QueryOptions {
|
227 | encoding?: string;
|
228 | }
|
229 |
|
230 | export interface FunctionParam {
|
231 | type: string;
|
232 | name?: string;
|
233 | direction?: string;
|
234 | }
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | export class QueryInterface {
|
243 | |
244 |
|
245 |
|
246 |
|
247 |
|
248 | public QueryGenerator: unknown;
|
249 |
|
250 | |
251 |
|
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 | }
|