-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
query-interface.d.ts
711 lines (603 loc) · 17.4 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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
import type { SetRequired } from 'type-fest';
import type { Deferrable } from '../../deferrable';
import type {
Logging,
Model,
AttributeOptions,
ModelAttributes,
WhereOptions,
Filterable,
ModelStatic,
CreationAttributes,
Attributes,
NormalizedAttributeOptions,
} from '../../model';
import type { Sequelize, QueryRawOptions, QueryRawOptionsWithModel } from '../../sequelize';
import type { Transaction } from '../../transaction';
import type { Fn, Literal, Col } from '../../utils/sequelize-method.js';
import type { AllowLowercase } from '../../utils/types.js';
import type { DataType } from './data-types.js';
import type { RemoveIndexQueryOptions, TableNameOrModel } from './query-generator-typescript';
import type { AbstractQueryGenerator, AddColumnQueryOptions, RemoveColumnQueryOptions } from './query-generator.js';
import { AbstractQueryInterfaceTypeScript } from './query-interface-typescript';
interface Replaceable {
/**
* Only named replacements are allowed in query interface methods.
*/
replacements?: { [key: string]: unknown };
}
interface QiOptionsWithReplacements extends QueryRawOptions, Replaceable {}
export interface QiInsertOptions extends QueryRawOptions, Replaceable {
returning?: boolean | Array<string | Literal | Col>;
}
export interface QiSelectOptions extends QueryRawOptions, Replaceable, Filterable<any> {
}
export interface QiUpdateOptions extends QueryRawOptions, Replaceable {
returning?: boolean | Array<string | Literal | Col>;
}
export interface QiDeleteOptions extends QueryRawOptions, Replaceable {
limit?: number | Literal | null | undefined;
}
export interface QiArithmeticOptions extends QueryRawOptions, Replaceable {
returning?: boolean | Array<string | Literal | Col>;
}
export interface QiUpsertOptions<M extends Model> extends QueryRawOptionsWithModel<M>, Replaceable {
}
export interface CreateFunctionOptions extends QueryRawOptions {
force?: boolean;
}
export interface CollateCharsetOptions {
collate?: string;
charset?: string;
}
export interface QueryInterfaceCreateTableOptions extends QueryRawOptions, CollateCharsetOptions {
engine?: string;
/**
* Used for compound unique keys.
*/
uniqueKeys?: { [indexName: string]: { fields: string[] } };
}
export interface QueryInterfaceDropTableOptions extends QueryRawOptions {
cascade?: boolean;
force?: boolean;
}
export interface QueryInterfaceDropAllTablesOptions extends QueryRawOptions {
skip?: string[];
}
export interface TableNameWithSchema {
tableName: string;
schema?: string;
delimiter?: string;
}
export type TableName = string | TableNameWithSchema;
export type IndexType = AllowLowercase<'UNIQUE' | 'FULLTEXT' | 'SPATIAL'>;
export type IndexMethod = 'BTREE' | 'HASH' | 'GIST' | 'SPGIST' | 'GIN' | 'BRIN' | string;
export interface IndexField {
/**
* The name of the column
*/
name: string;
/**
* Create a prefix index of length chars
*/
length?: number;
/**
* The direction the column should be sorted in
*/
order?: 'ASC' | 'DESC';
/**
* The collation (sort order) for the column
*/
collate?: string;
/**
* Index operator type. Postgres only
*/
operator?: string;
}
export interface IndexOptions {
/**
* 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;
/**
* The message to display if the unique constraint is violated.
*/
msg?: string;
/**
* PostgreSQL will build the index without taking any write locks. Postgres only.
*
* @default false
*/
concurrently?: boolean;
/**
* The fields to index.
*/
// TODO: rename to "columns"
fields?: Array<string | IndexField | Fn | Literal>;
/**
* The method to create the index by (`USING` statement in SQL).
* BTREE and HASH are supported by mysql and postgres.
* 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;
/**
* Non-key columns to be added to the lead level of the nonclustered index.
*/
include?: Literal | Array<string | Literal>;
}
export interface QueryInterfaceIndexOptions extends IndexOptions, Omit<QiOptionsWithReplacements, 'type'> {}
export interface QueryInterfaceRemoveIndexOptions extends QueryInterfaceIndexOptions, RemoveIndexQueryOptions {}
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, QueryRawOptions {
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;
}
export interface DatabaseDescription {
name: string;
}
export interface IndexFieldDescription {
attribute: string;
length: number | undefined;
order: 'DESC' | 'ASC';
collate: string | undefined;
}
export interface IndexDescription {
primary: boolean;
fields: IndexFieldDescription[];
name: string;
tableName: string | undefined;
unique: boolean;
type: string | undefined;
}
export interface AddColumnOptions extends AddColumnQueryOptions, QueryRawOptions, Replaceable {}
export interface RemoveColumnOptions extends RemoveColumnQueryOptions, QueryRawOptions, Replaceable {}
/**
* 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 AbstractQueryInterface extends AbstractQueryInterfaceTypeScript {
/**
* Returns the dialect-specific sql generator.
*
* We don't have a definition for the QueryGenerator, because I doubt it is commonly in use separately.
*/
queryGenerator: AbstractQueryGenerator;
/**
* Returns the current sequelize instance.
*/
sequelize: Sequelize;
constructor(sequelize: Sequelize, queryGenerator: AbstractQueryGenerator);
/**
* Drops the specified schema (table).
*
* @param schema The schema to query. Applies only to Postgres.
*/
dropSchema(schema?: string, options?: QueryRawOptions): Promise<void>;
/**
* Drops all tables.
*/
dropAllSchemas(options?: QueryInterfaceDropAllTablesOptions): Promise<void>;
/**
* Queries all table names in the database.
*
* @param options
*/
showAllSchemas(options?: QueryRawOptions): Promise<string[]>;
/**
* Return database version
*/
databaseVersion(options?: QueryRawOptions): 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.
*/
createTable<M extends Model>(
tableName: TableName,
attributes: ModelAttributes<M, CreationAttributes<M>>,
options?: QueryInterfaceCreateTableOptions
): Promise<void>;
/**
* Drops the specified table.
*
* @param tableName Table name.
* @param options Query options, particularly "force".
*/
dropTable(tableName: TableName, options?: QueryInterfaceDropTableOptions): Promise<void>;
/**
* Drops all tables.
*
* @param options
*/
dropAllTables(options?: QueryInterfaceDropAllTablesOptions): Promise<void>;
/**
* Drops all defined enums
*
* @param options
*/
dropAllEnums(options?: QueryRawOptions): Promise<void>;
/**
* Renames a table
*/
renameTable(before: TableName, after: TableName, options?: QueryRawOptions): Promise<void>;
/**
* Returns all tables
*/
showAllTables(options?: QueryRawOptions): Promise<string[]>;
/**
* Returns a promise that resolves to true if the table exists in the database, false otherwise.
*
* @param tableName The name of the table
* @param options Options passed to {@link Sequelize#query}
*/
tableExists(tableName: TableName, options?: QueryRawOptions): Promise<boolean>;
/**
* Describe a table
*/
describeTable(
tableName: TableName,
options?: string | { schema?: string, schemaDelimiter?: string } & Logging
): Promise<ColumnsDescription>;
/**
* Adds a new column to a table
*/
addColumn(
table: TableName,
key: string,
attribute: AttributeOptions | DataType,
options?: AddColumnOptions
): Promise<void>;
/**
* Removes a column from a table
*/
removeColumn(
table: TableName,
attribute: string,
options?: RemoveColumnOptions,
): Promise<void>;
/**
* Changes a column
*/
changeColumn(
tableName: TableName,
attributeName: string,
dataTypeOrOptions?: DataType | AttributeOptions,
options?: QiOptionsWithReplacements
): Promise<void>;
/**
* Renames a column
*/
renameColumn(
tableName: TableName,
attrNameBefore: string,
attrNameAfter: string,
options?: QiOptionsWithReplacements
): Promise<void>;
/**
* Adds a new index to a table
*/
addIndex(
tableName: TableName,
attributes: string[],
options?: QueryInterfaceIndexOptions,
rawTablename?: string
): Promise<void>;
addIndex(
tableName: TableName,
options: SetRequired<QueryInterfaceIndexOptions, 'fields'>,
rawTablename?: string
): Promise<void>;
/**
* Removes an index of a table
*/
removeIndex(
tableName: TableName,
indexName: string,
options?: QueryInterfaceRemoveIndexOptions
): Promise<void>;
removeIndex(
tableName: TableName,
attributes: string[],
options?: QueryInterfaceRemoveIndexOptions
): Promise<void>;
/**
* Adds constraints to a table
*/
addConstraint(
tableName: TableName,
options?: AddConstraintOptions & QueryRawOptions
): Promise<void>;
/**
* Removes constraints from a table
*/
removeConstraint(tableName: TableName, constraintName: string, options?: QueryRawOptions): Promise<void>;
/**
* Shows the index of a table
*/
showIndex(tableName: TableNameOrModel, options?: QueryRawOptions): Promise<IndexDescription[]>;
/**
* Put a name to an index
*/
nameIndexes(indexes: string[], rawTablename: string): Promise<void>;
/**
* Returns all foreign key constraints of requested tables
*/
getForeignKeysForTables(tableNames: string[], options?: QueryRawOptions): Promise<object>;
/**
* Get foreign key references details for the table
*/
getForeignKeyReferencesForTable(tableName: TableName, options?: QueryRawOptions): Promise<object>;
/**
* Inserts a new record
*/
insert(instance: Model | null, tableName: string, values: object, options?: QiInsertOptions): Promise<object>;
/**
* Inserts or Updates a record in the database
*/
upsert<M extends Model>(
tableName: TableName,
insertValues: object,
updateValues: object,
where: object,
options?: QiUpsertOptions<M>,
): Promise<object>;
/**
* Inserts multiple records at once
*/
bulkInsert(
tableName: TableName,
records: object[],
options?: QiOptionsWithReplacements,
attributes?: Record<string, AttributeOptions>
): Promise<object | number>;
/**
* Updates a row
*/
update<M extends Model>(
instance: M,
tableName: TableName,
values: object,
where: WhereOptions<Attributes<M>>,
options?: QiUpdateOptions
): Promise<object>;
/**
* Updates multiple rows at once
*/
bulkUpdate(
tableName: TableName,
values: object,
where: WhereOptions<any>,
options?: QiOptionsWithReplacements,
columnDefinitions?: { [columnName: string]: NormalizedAttributeOptions },
): Promise<object>;
/**
* Deletes a row
*/
delete(
instance: Model | null,
tableName: TableName,
identifier: WhereOptions<any>,
options?: QiDeleteOptions,
): Promise<object>;
/**
* Deletes multiple rows at once
*/
bulkDelete(
tableName: TableName,
identifier: WhereOptions<any>,
options?: QiOptionsWithReplacements,
model?: ModelStatic
): Promise<object>;
/**
* Returns selected rows
*/
select(model: ModelStatic | null, tableName: TableName, options?: QiSelectOptions): Promise<object[]>;
/**
* Increments a row value
*/
increment<M extends Model>(
model: ModelStatic<M>,
tableName: TableName,
incrementAmountsByField: object,
extraAttributesToBeUpdated?: object,
where?: WhereOptions<Attributes<M>>,
options?: QiArithmeticOptions,
): Promise<object>;
/**
* Decrements a row value
*/
decrement<M extends Model>(
model: ModelStatic<M>,
tableName: TableName,
incrementAmountsByField: object,
extraAttributesToBeUpdated?: object,
where?: WhereOptions<Attributes<M>>,
options?: QiArithmeticOptions,
): Promise<object>;
/**
* Selects raw without parsing the string into an object
*/
rawSelect(
tableName: TableName,
options: QiSelectOptions,
attributeSelector: string,
model?: ModelStatic
): Promise<string[]>;
/**
* Postgres only. Creates a trigger on specified table to call the specified function with supplied
* parameters.
*/
createTrigger(
tableName: TableName,
triggerName: string,
timingType: string,
fireOnArray: Array<{
[key: string]: unknown,
}>,
functionName: string,
functionParams: FunctionParam[],
optionsArray: string[],
options?: QiOptionsWithReplacements
): Promise<void>;
/**
* Postgres only. Drops the specified trigger.
*/
dropTrigger(tableName: TableName, triggerName: string, options?: QiOptionsWithReplacements): Promise<void>;
/**
* Postgres only. Renames a trigger
*/
renameTrigger(
tableName: TableName,
oldTriggerName: string,
newTriggerName: string,
options?: QiOptionsWithReplacements
): Promise<void>;
/**
* Postgres only. Create a function
*/
createFunction(
functionName: string,
params: FunctionParam[],
returnType: string,
language: string,
body: string,
optionsArray?: string[],
options?: CreateFunctionOptions
): Promise<void>;
/**
* Postgres only. Drops a function
*/
dropFunction(functionName: string, params: FunctionParam[], options?: QiOptionsWithReplacements): Promise<void>;
/**
* Postgres only. Rename a function
*/
renameFunction(
oldFunctionName: string,
params: FunctionParam[],
newFunctionName: string,
options?: QiOptionsWithReplacements
): Promise<void>;
/**
* 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.
*/
quoteIdentifier(identifier: string, force?: boolean): string;
/**
* Split an identifier into .-separated tokens and quote each part.
*/
quoteIdentifiers(identifiers: string): string;
/**
* Set option for autocommit of a transaction
*/
setAutocommit(transaction: Transaction, value: boolean, options?: QueryRawOptions): Promise<void>;
/**
* Set the isolation level of a transaction
*/
setIsolationLevel(transaction: Transaction, value: string, options?: QueryRawOptions): Promise<void>;
/**
* Begin a new transaction
*/
startTransaction(transaction: Transaction, options?: QueryRawOptions): Promise<void>;
/**
* Defer constraints
*/
deferConstraints(transaction: Transaction, options?: QueryRawOptions): Promise<void>;
/**
* Commit an already started transaction
*/
commitTransaction(transaction: Transaction, options?: QueryRawOptions): Promise<void>;
/**
* Rollback ( revert ) a transaction that has'nt been commited
*/
rollbackTransaction(transaction: Transaction, options?: QueryRawOptions): Promise<void>;
/**
* Creates a database
*/
createDatabase(name: string, options?: CreateDatabaseOptions): Promise<void>;
/**
* Creates a database
*/
dropDatabase(name: string, options?: QueryRawOptions): Promise<void>;
/**
* Lists all available databases
*/
listDatabases(options?: QueryRawOptions): Promise<DatabaseDescription[]>;
}