Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 387x 387x 387x 387x 387x 387x 387x 34x 31x 31x 31x 411x 76x 563x 151x 298x 2x 1x 151x 34x 1x 2x | import type { DataTypeInput } from '../data-types/data-types.ts';
import type { ConstraintOptions, IndexField, IndexOptions } from '../ddl/ddl-generator.ts';
import type { TableName } from '../ddl/sql-literal.ts';
import type { AttributeOptions } from '../model/model-definition.ts';
import type { QueryOptionsBase, RuntimeOwner } from '../model/runtime.ts';
import { BulkData } from './bulk-data.ts';
import { ColumnCommands } from './column-commands.ts';
import { EnumCommands } from './enum-commands.ts';
import { IndexCommands } from './index-commands.ts';
import { QueryGenerator } from './query-generator.ts';
import { QueryInterfaceCatalog } from './query-interface-catalog.ts';
import { StatementRunner } from './statement-runner.ts';
export type {
ColumnDescription,
CreateTableOptions,
DropTableOptions,
ForeignKeyReference,
} from './query-interface-types.ts';
/**
* Schema manipulation API (Sequelize v6 `QueryInterface`) for PostgreSQL. Used by `sync()` and by
* migrations (`sequelize-cli`, umzug). Obtain the instance with {@link Sequelize.getQueryInterface}.
* Table names are `string` or `{ tableName, schema }`; identifiers are always quoted and values bound or
* escaped, never concatenated raw.
*
* @example
* ```ts
* const queryInterface = sequelize.getQueryInterface();
* await queryInterface.createTable('users', {
* id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
* name: { type: DataTypes.STRING, allowNull: false },
* });
* await queryInterface.addIndex('users', ['name'], { unique: true });
* ```
* @see [Schema guide](/guide/schema)
*/
export class QueryInterface extends QueryInterfaceCatalog {
/** Sequelize v6 `queryGenerator` property; only `selectQuery` is provided. */
readonly queryGenerator: QueryGenerator = new QueryGenerator();
readonly #columns: ColumnCommands;
readonly #indexes: IndexCommands;
readonly #enums: EnumCommands;
readonly #bulk: BulkData;
/**
* Creates the schema API bound to a `Sequelize` instance; prefer {@link Sequelize.getQueryInterface}.
*
* @param sequelize - Owning `Sequelize` instance.
* @example
* ```ts
* const api = new QueryInterface(sequelize);
* await api.showAllTables();
* ```
*/
constructor(sequelize: RuntimeOwner) {
super(sequelize);
const runner = new StatementRunner(sequelize);
this.#columns = new ColumnCommands(runner);
this.#indexes = new IndexCommands(runner);
this.#enums = new EnumCommands(runner);
this.#bulk = new BulkData(runner);
}
/**
* Adds a column with `ALTER TABLE ... ADD COLUMN`; a plain data type or a full attribute definition (nullability, default, references, ...) is accepted.
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column name.
* @param attribute - Column definition.
* @param options - Query options.
*
* @example
* ```ts
* await queryInterface.addColumn('users', 'nickname', { type: DataTypes.STRING, allowNull: true });
* ```
*/
async addColumn(
table: TableName,
column: string,
attribute: DataTypeInput | AttributeOptions,
options?: QueryOptionsBase,
): Promise<void> {
await this.#columns.addColumn(table, column, attribute, options);
}
/**
* Drops a column with `ALTER TABLE ... DROP COLUMN`.
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column name.
* @param options - Query options.
*
* @example
* ```ts
* await queryInterface.removeColumn('users', 'nickname');
* ```
*/
async removeColumn(table: TableName, column: string, options?: QueryOptionsBase): Promise<void> {
await this.#columns.removeColumn(table, column, options);
}
/**
* Alters the type, nullability, default and comment of an existing column in one go.
*
* @param table - Table name or `{ tableName, schema }`.
* @param column - Column name.
* @param attribute - New column definition.
* @param options - Query options.
*
* @example
* ```ts
* await queryInterface.changeColumn('users', 'name', { type: DataTypes.STRING(100), allowNull: false });
* ```
*/
async changeColumn(
table: TableName,
column: string,
attribute: DataTypeInput | AttributeOptions,
options?: QueryOptionsBase,
): Promise<void> {
await this.#columns.changeColumn(table, column, attribute, options);
}
/**
* Renames a column with `ALTER TABLE ... RENAME COLUMN`.
*
* @param table - Table name or `{ tableName, schema }`.
* @param before - Current column name.
* @param after - New column name.
* @param options - Query options.
*
* @example
* ```ts
* await queryInterface.renameColumn('users', 'name', 'full_name');
* ```
*/
async renameColumn(
table: TableName,
before: string,
after: string,
options?: QueryOptionsBase,
): Promise<void> {
await this.#columns.renameColumn(table, before, after, options);
}
/**
* Creates an index on a table, optionally unique, partial, covering or `CONCURRENTLY`.
*
* @param table - Table name or `{ tableName, schema }`.
* @param fieldsOrOptions - Indexed fields or index options.
* @param options - Index options when fields are given separately.
*
* @example
* ```ts
* await queryInterface.addIndex('users', ['email'], { unique: true, name: 'users_email_unique' });
* await queryInterface.addIndex('users', {
* name: 'users_lower_email',
* fields: [{ expression: 'lower(email)', nulls: 'LAST' }],
* });
* ```
*/
async addIndex(
table: TableName,
fieldsOrOptions: readonly IndexField[] | IndexOptions,
options: IndexOptions & QueryOptionsBase = {},
): Promise<void> {
await this.#indexes.addIndex(table, fieldsOrOptions, options);
}
/**
* Drops an index identified by name, or by the fields it was created on (the default generated name is derived from them).
*
* @param table - Table name or `{ tableName, schema }`.
* @param nameOrFields - Index name or indexed fields.
* @param options - `concurrently` and query options.
*
* @example
* ```ts
* await queryInterface.removeIndex('users', 'users_email_unique');
* ```
*/
async removeIndex(
table: TableName,
nameOrFields: string | readonly IndexField[],
options: QueryOptionsBase & Readonly<{ concurrently?: boolean }> = {},
): Promise<void> {
await this.#indexes.removeIndex(table, nameOrFields, options);
}
/**
* Adds a `CHECK`, `UNIQUE`, `PRIMARY KEY`, `FOREIGN KEY` or `EXCLUDE` constraint to a table.
* `notValid: true` skips checking existing rows (check and foreign key); validate later with
* {@link QueryInterface.validateConstraint}.
*
* @param table - Table name or `{ tableName, schema }`.
* @param options - Constraint definition.
*
* @example
* ```ts
* await queryInterface.addConstraint('products', {
* type: 'check',
* fields: ['price'],
* name: 'products_price_positive',
* where: { price: { [Op.gt]: 0 } },
* });
* await queryInterface.addConstraint('bookings', {
* type: 'exclude',
* name: 'bookings_no_overlap',
* using: 'gist',
* exclude: [
* { name: 'room', with: '=' },
* { expression: 'tstzrange(starts, ends)', with: '&&' },
* ],
* });
* ```
*/
async addConstraint(
table: TableName,
options: ConstraintOptions & QueryOptionsBase,
): Promise<void> {
await this.#indexes.addConstraint(table, options);
}
/**
* Validates a constraint added with `notValid: true`, scanning existing rows without blocking writes.
*
* @postgrelize
* @param table - Table name or `{ tableName, schema }`.
* @param name - Constraint name.
* @param options - Query options.
*
* @example
* ```ts
* await queryInterface.addConstraint('products', {
* type: 'check',
* name: 'products_price_positive',
* where: { price: { [Op.gt]: 0 } },
* notValid: true,
* });
* await queryInterface.validateConstraint('products', 'products_price_positive');
* ```
*/
async validateConstraint(
table: TableName,
name: string,
options?: QueryOptionsBase,
): Promise<void> {
await this.#indexes.validateConstraint(table, name, options);
}
/**
* Drops a named constraint from a table.
*
* @param table - Table name or `{ tableName, schema }`.
* @param name - Constraint name.
* @param options - Query options.
*
* @example
* ```ts
* await queryInterface.removeConstraint('products', 'products_price_positive');
* ```
*/
async removeConstraint(
table: TableName,
name: string,
options?: QueryOptionsBase,
): Promise<void> {
await this.#indexes.removeConstraint(table, name, options);
}
/**
* Runs `DROP TYPE IF EXISTS` for an enum type.
*
* @param name - Enum type name.
* @param options - Query options.
*
* @example
* ```ts
* await queryInterface.dropEnum('enum_users_role');
* ```
*/
async dropEnum(name: string, options?: QueryOptionsBase): Promise<void> {
await this.#enums.dropEnum(name, options);
}
/**
* Drops every enum type of the current schema with `CASCADE`, which also drops dependent columns' defaults. Destructive.
*
* @param options - Query options.
*
* @example
* ```ts
* await queryInterface.dropAllEnums();
* ```
*/
async dropAllEnums(options?: QueryOptionsBase): Promise<void> {
await this.#enums.dropAllEnums(options);
}
/** @internal */
async dropEnumsOf(
table: TableName,
fields: readonly string[],
options?: QueryOptionsBase,
): Promise<void> {
await this.#enums.dropEnumsOf(table, fields, options);
}
/**
* Inserts several rows with one `INSERT` (used by seeders). Values are bound as parameters.
*
* @param table - Table name or `{ tableName, schema }`.
* @param records - Rows keyed by column name.
* @param options - Query options.
* @returns Number of inserted rows.
*
* @example
* ```ts
* const inserted = await queryInterface.bulkInsert('users', [
* { name: 'Ada', email: 'ada@example.com' },
* { name: 'Alan', email: 'alan@example.com' },
* ]);
* ```
*/
async bulkInsert(
table: TableName,
records: readonly Readonly<Record<string, unknown>>[],
options: QueryOptionsBase = {},
): Promise<number> {
return await this.#bulk.bulkInsert(table, records, options);
}
/**
* Updates every row matching a where object with one `UPDATE`, without model hooks or validation.
*
* @param table - Table name or `{ tableName, schema }`.
* @param values - New column values.
* @param where - Filter (where object keyed by column).
* @param options - Query options.
* @returns Number of updated rows.
*
* @example
* ```ts
* const updated = await queryInterface.bulkUpdate('users', { active: false }, { name: 'Ada' });
* ```
*/
async bulkUpdate(
table: TableName,
values: Readonly<Record<string, unknown>>,
where: unknown,
options: QueryOptionsBase = {},
): Promise<number> {
return await this.#bulk.bulkUpdate(table, values, where, options);
}
/**
* Deletes every row matching a where object with one `DELETE`, without model hooks.
*
* @param table - Table name or `{ tableName, schema }`.
* @param where - Filter (where object keyed by column); empty deletes all rows.
* @param options - Query options.
* @returns Number of deleted rows.
*
* @example
* ```ts
* const removed = await queryInterface.bulkDelete('users', { active: false });
* ```
*/
async bulkDelete(
table: TableName,
where: unknown = {},
options: QueryOptionsBase = {},
): Promise<number> {
return await this.#bulk.bulkDelete(table, where, options);
}
}
|