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