Query Runner API
要使用 API 来修改数据库结构,你可以使用 QueryRunner。
import {
MigrationInterface,
QueryRunner,
Table,
TableIndex,
TableColumn,
TableForeignKey,
} from "typeorm"
export class QuestionRefactoringTIMESTAMP implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "question",
columns: [
{
name: "id",
type: "int",
isPrimary: true,
},
{
name: "name",
type: "varchar",
},
],
}),
true,
)
await queryRunner.createIndex(
"question",
new TableIndex({
name: "IDX_QUESTION_NAME",
columnNames: ["name"],
}),
)
await queryRunner.createTable(
new Table({
name: "answer",
columns: [
{
name: "id",
type: "int",
isPrimary: true,
},
{
name: "name",
type: "varchar",
},
{
name: "created_at",
type: "timestamp",
default: "now()",
},
],
}),
true,
)
await queryRunner.addColumn(
"answer",
new TableColumn({
name: "questionId",
type: "int",
}),
)
await queryRunner.createForeignKey(
"answer",
new TableForeignKey({
columnNames: ["questionId"],
referencedColumnNames: ["id"],
referencedTableName: "question",
onDelete: "CASCADE",
}),
)
}
async down(queryRunner: QueryRunner): Promise<void> {
const table = await queryRunner.getTable("answer")
const foreignKey = table.foreignKeys.find(
(fk) => fk.columnNames.indexOf("questionId") !== -1,
)
await queryRunner.dropForeignKey("answer", foreignKey)
await queryRunner.dropColumn("answer", "questionId")
await queryRunner.dropTable("answer")
await queryRunner.dropIndex("question", "IDX_QUESTION_NAME")
await queryRunner.dropTable("question")
}
}getDatabases(): Promise<string[]>返回所有可用的数据库名,包括系统数据库。
getSchemas(database?: string): Promise<string[]>database- 如果指定了 database 参数,返回该数据库下的模式
返回所有可用的模式名,包括系统模式。仅对 SQLServer 和 Postgres 有用。
getTable(tableName: string): Promise<Table|undefined>tableName- 要加载的表名
按给定名称从数据库加载一张表。
getTables(tableNames: string[]): Promise<Table[]>tableNames- 要加载的表名
按给定名称从数据库加载多张表。
hasDatabase(database: string): Promise<boolean>database- 要检查的数据库名
检查给定名称的数据库是否存在。
hasSchema(schema: string): Promise<boolean>schema- 要检查的模式名
检查给定名称的模式是否存在。仅用于 SqlServer 和 Postgres。
hasTable(table: Table|string): Promise<boolean>table- Table 对象或表名
检查表是否存在。
hasColumn(table: Table|string, columnName: string): Promise<boolean>table- Table 对象或表名columnName- 要检查的列名
检查表中是否存在该列。
createDatabase(database: string, ifNotExists?: boolean): Promise<void>database- 数据库名ifNotExists- 设为true时,如果数据库已存在就静默忽略;否则抛出错误(默认)
创建一个新数据库。
dropDatabase(database: string, ifExists?: boolean): Promise<void>database- 数据库名ifExists- 设为true时,如果数据库不存在就静默忽略;否则抛出错误(默认)
删除数据库。
createSchema(schemaPath: string, ifNotExists?: boolean): Promise<void>schemaPath- 模式名。对 SqlServer 可以接受模式路径(例如 'dbName.schemaName')作为参数。如果传入了模式路径,它会在指定数据库中创建模式ifNotExists- 设为true时,如果模式已存在就静默忽略;否则抛出错误(默认)
创建一个新的表模式。
dropSchema(schemaPath: string, ifExists?: boolean, isCascade?: boolean): Promise<void>schemaPath- 模式名。对 SqlServer 可以接受模式路径(例如 'dbName.schemaName')作为参数。如果传入了模式路径,它会在指定数据库中删除模式ifExists- 设为true时,如果模式不存在就静默忽略;否则抛出错误(默认)isCascade- 为true时,自动删除该模式中包含的对象(表、函数等)。仅用于 Postgres。
删除一个表模式。
createTable(table: Table, ifNotExists?: boolean, createForeignKeys?: boolean, createIndices?: boolean): Promise<void>table- Table 对象。ifNotExists- 设为true时,如果表已存在就静默忽略;否则抛出错误(默认)createForeignKeys- 表示建表时是否创建外键。默认truecreateIndices- 表示建表时是否创建索引。默认true
创建一张新表。
dropTable(table: Table|string, ifExists?: boolean, dropForeignKeys?: boolean, dropIndices?: boolean): Promise<void>table- 要删除的 Table 对象或表名ifExists- 设为true时,如果表不存在就静默忽略;否则抛出错误(默认)dropForeignKeys- 表示删表时是否删除外键。默认truedropIndices- 表示删表时是否删除索引。默认true
删除一张表。
createView(view: View, syncWithMetadata?: boolean, oldView?: View): Promise<void>view- View 对象syncWithMetadata- 表示是否让视图与元数据同步(可选)oldView- 要被替换的旧 View 对象(可选)
创建一个新视图。
dropView(view: View|string, ifExists?: boolean): Promise<void>view- 要删除的 View 对象或视图名ifExists- 设为true时,如果视图不存在就静默忽略;否则抛出错误(默认)
删除一个视图。
renameTable(oldTableOrName: Table|string, newTableName: string): Promise<void>oldTableOrName- 要重命名的旧 Table 对象或表名newTableName- 新表名
重命名一张表。
addColumn(table: Table|string, column: TableColumn): Promise<void>table- Table 对象或表名column- 新列
添加一个新列。
addColumns(table: Table|string, columns: TableColumn[]): Promise<void>table- Table 对象或表名columns- 新列
添加一个新列。
renameColumn(table: Table|string, oldColumnOrName: TableColumn|string, newColumnOrName: TableColumn|string): Promise<void>table- Table 对象或表名oldColumnOrName- 旧列。接受 TableColumn 对象或列名newColumnOrName- 新列。接受 TableColumn 对象或列名
重命名一个列。
changeColumn(table: Table|string, oldColumn: TableColumn|string, newColumn: TableColumn): Promise<void>table- Table 对象或表名oldColumn- 旧列。接受 TableColumn 对象或列名newColumn- 新列。接受 TableColumn 对象
修改表中的一个列。
changeColumns(table: Table|string, changedColumns: { oldColumn: TableColumn, newColumn: TableColumn }[]): Promise<void>table- Table 对象或表名changedColumns- 被修改列的数组。oldColumn- 旧 TableColumn 对象newColumn- 新 TableColumn 对象
修改表中的多个列。
dropColumn(table: Table|string, column: TableColumn|string, ifExists?: boolean): Promise<void>table- Table 对象或表名column- 要删除的 TableColumn 对象或列名ifExists- 设为true时,如果列不存在就静默忽略;否则抛出错误(默认)
删除表中的一个列。
dropColumns(table: Table|string, columns: TableColumn[]|string[], ifExists?: boolean): Promise<void>table- Table 对象或表名columns- 要删除的 TableColumn 对象或列名数组ifExists- 设为true时,如果这些列不存在就静默忽略;否则抛出错误(默认)
删除表中的多个列。
createPrimaryKey(table: Table|string, columnNames: string[]): Promise<void>table- Table 对象或表名columnNames- 将作为主键的列名数组
创建一个新主键。
updatePrimaryKeys(table: Table|string, columns: TableColumn[]): Promise<void>table- Table 对象或表名columns- 将被更新的 TableColumn 对象数组
更新复合主键。
dropPrimaryKey(table: Table|string, constraintName?: string, ifExists?: boolean): Promise<void>table- Table 对象或表名constraintName- 约束名(可选)ifExists- 设为true时,如果主键不存在就静默忽略;否则抛出错误(默认)
删除一个主键。
createUniqueConstraint(table: Table|string, uniqueConstraint: TableUnique): Promise<void>table- Table 对象或表名uniqueConstraint- 要创建的 TableUnique 对象
创建新的唯一约束。
注意:对 MySQL 无效,因为 MySQL 把唯一约束存储为唯一索引。请改用
createIndex()方法。
createUniqueConstraints(table: Table|string, uniqueConstraints: TableUnique[]): Promise<void>table- Table 对象或表名uniqueConstraints- 要创建的 TableUnique 对象数组
创建新的唯一约束。
注意:对 MySQL 无效,因为 MySQL 把唯一约束存储为唯一索引。请改用
createIndices()方法。
dropUniqueConstraint(table: Table|string, uniqueOrName: TableUnique|string, ifExists?: boolean): Promise<void>table- Table 对象或表名uniqueOrName- 要删除的 TableUnique 对象或唯一约束名ifExists- 设为true时,如果约束不存在就静默忽略;否则抛出错误(默认)
删除一个唯一约束。
注意:对 MySQL 无效,因为 MySQL 把唯一约束存储为唯一索引。请改用
dropIndex()方法。
dropUniqueConstraints(table: Table|string, uniqueConstraints: TableUnique[], ifExists?: boolean): Promise<void>table- Table 对象或表名uniqueConstraints- 要删除的 TableUnique 对象数组ifExists- 设为true时,如果这些约束不存在就静默忽略;否则抛出错误(默认)
删除多个唯一约束。
注意:对 MySQL 无效,因为 MySQL 把唯一约束存储为唯一索引。请改用
dropIndices()方法。
createCheckConstraint(table: Table|string, checkConstraint: TableCheck): Promise<void>table- Table 对象或表名checkConstraint- TableCheck 对象
创建一个新的检查约束。
注意:MySQL 不支持检查约束。
createCheckConstraints(table: Table|string, checkConstraints: TableCheck[]): Promise<void>table- Table 对象或表名checkConstraints- TableCheck 对象数组
创建一个新的检查约束。
注意:MySQL 不支持检查约束。
dropCheckConstraint(table: Table|string, checkOrName: TableCheck|string, ifExists?: boolean): Promise<void>table- Table 对象或表名checkOrName- TableCheck 对象或检查约束名ifExists- 设为true时,如果约束不存在就静默忽略;否则抛出错误(默认)
删除一个检查约束。
注意:MySQL 不支持检查约束。
dropCheckConstraints(table: Table|string, checkConstraints: TableCheck[], ifExists?: boolean): Promise<void>table- Table 对象或表名checkConstraints- TableCheck 对象数组ifExists- 设为true时,如果这些约束不存在就静默忽略;否则抛出错误(默认)
删除多个检查约束。
注意:MySQL 不支持检查约束。
createExclusionConstraint(table: Table|string, exclusionConstraint: TableExclusion): Promise<void>table- Table 对象或表名exclusionConstraint- TableExclusion 对象
创建一个新的排他约束。
注意:只有 PostgreSQL 支持排他约束。
createExclusionConstraints(table: Table|string, exclusionConstraints: TableExclusion[]): Promise<void>table- Table 对象或表名exclusionConstraints- TableExclusion 对象数组
创建新的排他约束。
注意:只有 PostgreSQL 支持排他约束。
dropExclusionConstraint(table: Table|string, exclusionOrName: TableExclusion|string, ifExists?: boolean): Promise<void>table- Table 对象或表名exclusionOrName- TableExclusion 对象或排他约束名ifExists- 设为true时,如果约束不存在就静默忽略;否则抛出错误(默认)
删除一个排他约束。
注意:只有 PostgreSQL 支持排他约束。
dropExclusionConstraints(table: Table|string, exclusionConstraints: TableExclusion[], ifExists?: boolean): Promise<void>table- Table 对象或表名exclusionConstraints- TableExclusion 对象数组ifExists- 设为true时,如果这些约束不存在就静默忽略;否则抛出错误(默认)
删除多个排他约束。
注意:只有 PostgreSQL 支持排他约束。
createForeignKey(table: Table|string, foreignKey: TableForeignKey): Promise<void>table- Table 对象或表名foreignKey- TableForeignKey 对象
创建一个新外键。
createForeignKeys(table: Table|string, foreignKeys: TableForeignKey[]): Promise<void>table- Table 对象或表名foreignKeys- TableForeignKey 对象数组
创建新的外键。
dropForeignKey(table: Table|string, foreignKeyOrName: TableForeignKey|string, ifExists?: boolean): Promise<void>table- Table 对象或表名foreignKeyOrName- TableForeignKey 对象或外键名ifExists- 设为true时,如果外键不存在就静默忽略;否则抛出错误(默认)
删除一个外键。
dropForeignKeys(table: Table|string, foreignKeys: TableForeignKey[], ifExists?: boolean): Promise<void>table- Table 对象或表名foreignKeys- TableForeignKey 对象数组ifExists- 设为true时,如果这些外键不存在就静默忽略;否则抛出错误(默认)
删除多个外键。
createIndex(table: Table|string, index: TableIndex): Promise<void>table- Table 对象或表名index- TableIndex 对象
创建一个新索引。
createIndices(table: Table|string, indices: TableIndex[]): Promise<void>table- Table 对象或表名indices- TableIndex 对象数组
创建新的索引。
dropIndex(table: Table|string, index: TableIndex|string, ifExists?: boolean): Promise<void>table- Table 对象或表名index- TableIndex 对象或索引名ifExists- 设为true时,如果索引不存在就静默忽略;否则抛出错误(默认)
删除一个索引。
dropIndices(table: Table|string, indices: TableIndex[], ifExists?: boolean): Promise<void>table- Table 对象或表名indices- TableIndex 对象数组ifExists- 设为true时,如果这些索引不存在就静默忽略;否则抛出错误(默认)
删除多个索引。
clearTable(tableName: string, options?: {cascade?: boolean}): Promise<void>tableName- 表名options- 额外选项cascade- 表示是否同时清空带外键的表中的行(仅 PostgreSQL/CockroachDB 和 Oracle 支持;其他数据库在设为true时会抛出错误)。默认false
清空表中的所有内容。
注意:该操作使用 SQL 的 TRUNCATE 查询,无法在事务中回滚。
enableSqlMemory(): void启用 query runner 的特殊模式,在该模式下 sql 查询不会被执行,而是被记录到 query runner 内部的一个特殊变量中。你可以用 getMemorySql() 方法获取记录下来的 sql。
disableSqlMemory(): void禁用 query runner 的特殊模式,在该模式下 sql 查询不会被执行。先前记录的 sql 会被清空。
clearSqlMemory(): void清空所有已记录的 sql 语句。
getMemorySql(): SqlInMemory- 返回
SqlInMemory对象,其中包含upQueries和downQueries两个 sql 语句数组
获取内存中存储的 sql。sql 中的参数已经被替换过了。
executeMemoryUpSql(): Promise<void>执行已记录的 up sql 查询。
executeMemoryDownSql(): Promise<void>执行已记录的 down sql 查询。
In order to use an API to change a database schema you can use QueryRunner.
import {
MigrationInterface,
QueryRunner,
Table,
TableIndex,
TableColumn,
TableForeignKey,
} from "typeorm"
export class QuestionRefactoringTIMESTAMP implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "question",
columns: [
{
name: "id",
type: "int",
isPrimary: true,
},
{
name: "name",
type: "varchar",
},
],
}),
true,
)
await queryRunner.createIndex(
"question",
new TableIndex({
name: "IDX_QUESTION_NAME",
columnNames: ["name"],
}),
)
await queryRunner.createTable(
new Table({
name: "answer",
columns: [
{
name: "id",
type: "int",
isPrimary: true,
},
{
name: "name",
type: "varchar",
},
{
name: "created_at",
type: "timestamp",
default: "now()",
},
],
}),
true,
)
await queryRunner.addColumn(
"answer",
new TableColumn({
name: "questionId",
type: "int",
}),
)
await queryRunner.createForeignKey(
"answer",
new TableForeignKey({
columnNames: ["questionId"],
referencedColumnNames: ["id"],
referencedTableName: "question",
onDelete: "CASCADE",
}),
)
}
async down(queryRunner: QueryRunner): Promise<void> {
const table = await queryRunner.getTable("answer")
const foreignKey = table.foreignKeys.find(
(fk) => fk.columnNames.indexOf("questionId") !== -1,
)
await queryRunner.dropForeignKey("answer", foreignKey)
await queryRunner.dropColumn("answer", "questionId")
await queryRunner.dropTable("answer")
await queryRunner.dropIndex("question", "IDX_QUESTION_NAME")
await queryRunner.dropTable("question")
}
}getDatabases(): Promise<string[]>Returns all available database names including system databases.
getSchemas(database?: string): Promise<string[]>database- If database parameter specified, returns schemas of that database
Returns all available schema names including system schemas. Useful for SQLServer and Postgres only.
getTable(tableName: string): Promise<Table|undefined>tableName- name of a table to be loaded
Loads a table by a given name from the database.
getTables(tableNames: string[]): Promise<Table[]>tableNames- name of a tables to be loaded
Loads a tables by a given names from the database.
hasDatabase(database: string): Promise<boolean>database- name of a database to be checked
Checks if database with the given name exist.
hasSchema(schema: string): Promise<boolean>schema- name of a schema to be checked
Checks if schema with the given name exist. Used only for SqlServer and Postgres.
hasTable(table: Table|string): Promise<boolean>table- Table object or name
Checks if table exist.
hasColumn(table: Table|string, columnName: string): Promise<boolean>table- Table object or namecolumnName- name of a column to be checked
Checks if column exist in the table.
createDatabase(database: string, ifNotExists?: boolean): Promise<void>database- database nameifNotExists- when set totrue, silently ignores if the database already exists; otherwise throws an error (default)
Creates a new database.
dropDatabase(database: string, ifExists?: boolean): Promise<void>database- database nameifExists- when set totrue, silently ignores if the database does not exist; otherwise throws an error (default)
Drops database.
createSchema(schemaPath: string, ifNotExists?: boolean): Promise<void>schemaPath- schema name. For SqlServer can accept schema path (e.g. 'dbName.schemaName') as parameter. If schema path passed, it will create schema in specified databaseifNotExists- when set totrue, silently ignores if the schema already exists; otherwise throws an error (default)
Creates a new table schema.
dropSchema(schemaPath: string, ifExists?: boolean, isCascade?: boolean): Promise<void>schemaPath- schema name. For SqlServer can accept schema path (e.g. 'dbName.schemaName') as parameter. If schema path passed, it will drop schema in specified databaseifExists- when set totrue, silently ignores if the schema does not exist; otherwise throws an error (default)isCascade- Iftrue, automatically drop objects (tables, functions, etc.) that are contained in the schema. Used only in Postgres.
Drops a table schema.
createTable(table: Table, ifNotExists?: boolean, createForeignKeys?: boolean, createIndices?: boolean): Promise<void>table- Table object.ifNotExists- when set totrue, silently ignores if the table already exists; otherwise throws an error (default)createForeignKeys- indicates whether foreign keys will be created on table creation. DefaulttruecreateIndices- indicates whether indexes will be created on table creation. Defaulttrue
Creates a new table.
dropTable(table: Table|string, ifExists?: boolean, dropForeignKeys?: boolean, dropIndices?: boolean): Promise<void>table- Table object or table name to be droppedifExists- when set totrue, silently ignores if the table does not exist; otherwise throws an error (default)dropForeignKeys- indicates whether foreign keys will be dropped on table deletion. DefaulttruedropIndices- indicates whether indexes will be dropped on table deletion. Defaulttrue
Drops a table.
createView(view: View, syncWithMetadata?: boolean, oldView?: View): Promise<void>view- View objectsyncWithMetadata- indicates whether to sync view with metadata (optional)oldView- old View object to be replaced (optional)
Creates a new view.
dropView(view: View|string, ifExists?: boolean): Promise<void>view- View object or view name to be droppedifExists- when set totrue, silently ignores if the view does not exist; otherwise throws an error (default)
Drops a view.
renameTable(oldTableOrName: Table|string, newTableName: string): Promise<void>oldTableOrName- old Table object or name to be renamednewTableName- new table name
Renames a table.
addColumn(table: Table|string, column: TableColumn): Promise<void>table- Table object or namecolumn- new column
Adds a new column.
addColumns(table: Table|string, columns: TableColumn[]): Promise<void>table- Table object or namecolumns- new columns
Adds a new column.
renameColumn(table: Table|string, oldColumnOrName: TableColumn|string, newColumnOrName: TableColumn|string): Promise<void>table- Table object or nameoldColumnOrName- old column. Accepts TableColumn object or column namenewColumnOrName- new column. Accepts TableColumn object or column name
Renames a column.
changeColumn(table: Table|string, oldColumn: TableColumn|string, newColumn: TableColumn): Promise<void>table- Table object or nameoldColumn- old column. Accepts TableColumn object or column namenewColumn- new column. Accepts TableColumn object
Changes a column in the table.
changeColumns(table: Table|string, changedColumns: { oldColumn: TableColumn, newColumn: TableColumn }[]): Promise<void>table- Table object or namechangedColumns- array of changed columns.oldColumn- old TableColumn objectnewColumn- new TableColumn object
Changes a columns in the table.
dropColumn(table: Table|string, column: TableColumn|string, ifExists?: boolean): Promise<void>table- Table object or namecolumn- TableColumn object or column name to be droppedifExists- when set totrue, silently ignores if the column does not exist; otherwise throws an error (default)
Drops a column in the table.
dropColumns(table: Table|string, columns: TableColumn[]|string[], ifExists?: boolean): Promise<void>table- Table object or namecolumns- array of TableColumn objects or column names to be droppedifExists- when set totrue, silently ignores if the columns do not exist; otherwise throws an error (default)
Drops columns in the table.
createPrimaryKey(table: Table|string, columnNames: string[]): Promise<void>table- Table object or namecolumnNames- array of column names which will be primary
Creates a new primary key.
updatePrimaryKeys(table: Table|string, columns: TableColumn[]): Promise<void>table- Table object or namecolumns- array of TableColumn objects which will be updated
Updates composite primary keys.
dropPrimaryKey(table: Table|string, constraintName?: string, ifExists?: boolean): Promise<void>table- Table object or nameconstraintName- constraint name (optional)ifExists- when set totrue, silently ignores if the primary key does not exist; otherwise throws an error (default)
Drops a primary key.
createUniqueConstraint(table: Table|string, uniqueConstraint: TableUnique): Promise<void>table- Table object or nameuniqueConstraint- TableUnique object to be created
Creates new unique constraint.
Note: does not work for MySQL, because MySQL stores unique constraints as unique indexes. Use
createIndex()method instead.
createUniqueConstraints(table: Table|string, uniqueConstraints: TableUnique[]): Promise<void>table- Table object or nameuniqueConstraints- array of TableUnique objects to be created
Creates new unique constraints.
Note: does not work for MySQL, because MySQL stores unique constraints as unique indexes. Use
createIndices()method instead.
dropUniqueConstraint(table: Table|string, uniqueOrName: TableUnique|string, ifExists?: boolean): Promise<void>table- Table object or nameuniqueOrName- TableUnique object or unique constraint name to be droppedifExists- when set totrue, silently ignores if the constraint does not exist; otherwise throws an error (default)
Drops a unique constraint.
Note: does not work for MySQL, because MySQL stores unique constraints as unique indexes. Use
dropIndex()method instead.
dropUniqueConstraints(table: Table|string, uniqueConstraints: TableUnique[], ifExists?: boolean): Promise<void>table- Table object or nameuniqueConstraints- array of TableUnique objects to be droppedifExists- when set totrue, silently ignores if the constraints do not exist; otherwise throws an error (default)
Drops unique constraints.
Note: does not work for MySQL, because MySQL stores unique constraints as unique indexes. Use
dropIndices()method instead.
createCheckConstraint(table: Table|string, checkConstraint: TableCheck): Promise<void>table- Table object or namecheckConstraint- TableCheck object
Creates a new check constraint.
Note: MySQL does not support check constraints.
createCheckConstraints(table: Table|string, checkConstraints: TableCheck[]): Promise<void>table- Table object or namecheckConstraints- array of TableCheck objects
Creates a new check constraint.
Note: MySQL does not support check constraints.
dropCheckConstraint(table: Table|string, checkOrName: TableCheck|string, ifExists?: boolean): Promise<void>table- Table object or namecheckOrName- TableCheck object or check constraint nameifExists- when set totrue, silently ignores if the constraint does not exist; otherwise throws an error (default)
Drops a check constraint.
Note: MySQL does not support check constraints.
dropCheckConstraints(table: Table|string, checkConstraints: TableCheck[], ifExists?: boolean): Promise<void>table- Table object or namecheckConstraints- array of TableCheck objectsifExists- when set totrue, silently ignores if the constraints do not exist; otherwise throws an error (default)
Drops check constraints.
Note: MySQL does not support check constraints.
createExclusionConstraint(table: Table|string, exclusionConstraint: TableExclusion): Promise<void>table- Table object or nameexclusionConstraint- TableExclusion object
Creates a new exclusion constraint.
Note: only PostgreSQL supports exclusion constraints.
createExclusionConstraints(table: Table|string, exclusionConstraints: TableExclusion[]): Promise<void>table- Table object or nameexclusionConstraints- array of TableExclusion objects
Creates new exclusion constraints.
Note: only PostgreSQL supports exclusion constraints.
dropExclusionConstraint(table: Table|string, exclusionOrName: TableExclusion|string, ifExists?: boolean): Promise<void>table- Table object or nameexclusionOrName- TableExclusion object or exclusion constraint nameifExists- when set totrue, silently ignores if the constraint does not exist; otherwise throws an error (default)
Drops an exclusion constraint.
Note: only PostgreSQL supports exclusion constraints.
dropExclusionConstraints(table: Table|string, exclusionConstraints: TableExclusion[], ifExists?: boolean): Promise<void>table- Table object or nameexclusionConstraints- array of TableExclusion objectsifExists- when set totrue, silently ignores if the constraints do not exist; otherwise throws an error (default)
Drops exclusion constraints.
Note: only PostgreSQL supports exclusion constraints.
createForeignKey(table: Table|string, foreignKey: TableForeignKey): Promise<void>table- Table object or nameforeignKey- TableForeignKey object
Creates a new foreign key.
createForeignKeys(table: Table|string, foreignKeys: TableForeignKey[]): Promise<void>table- Table object or nameforeignKeys- array of TableForeignKey objects
Creates a new foreign keys.
dropForeignKey(table: Table|string, foreignKeyOrName: TableForeignKey|string, ifExists?: boolean): Promise<void>table- Table object or nameforeignKeyOrName- TableForeignKey object or foreign key nameifExists- when set totrue, silently ignores if the foreign key does not exist; otherwise throws an error (default)
Drops a foreign key.
dropForeignKeys(table: Table|string, foreignKeys: TableForeignKey[], ifExists?: boolean): Promise<void>table- Table object or nameforeignKeys- array of TableForeignKey objectsifExists- when set totrue, silently ignores if the foreign keys do not exist; otherwise throws an error (default)
Drops foreign keys.
createIndex(table: Table|string, index: TableIndex): Promise<void>table- Table object or nameindex- TableIndex object
Creates a new index.
createIndices(table: Table|string, indices: TableIndex[]): Promise<void>table- Table object or nameindices- array of TableIndex objects
Creates new indexes.
dropIndex(table: Table|string, index: TableIndex|string, ifExists?: boolean): Promise<void>table- Table object or nameindex- TableIndex object or index nameifExists- when set totrue, silently ignores if the index does not exist; otherwise throws an error (default)
Drops an index.
dropIndices(table: Table|string, indices: TableIndex[], ifExists?: boolean): Promise<void>table- Table object or nameindices- array of TableIndex objectsifExists- when set totrue, silently ignores if the indexes do not exist; otherwise throws an error (default)
Drops indexes.
clearTable(tableName: string, options?: {cascade?: boolean}): Promise<void>tableName- table nameoptions- additional optionscascade- Indicates whether to clear rows of tables that have foreign keys (supported by PostgreSQL/CockroachDB and Oracle only; other databases throw an error if set totrue). Defaultfalse
Clears all table contents.
Note: this operation uses SQL's TRUNCATE query which cannot be reverted in transactions.
enableSqlMemory(): voidEnables special query runner mode in which sql queries won't be executed, instead they will be memorized into a special variable inside query runner. You can get memorized sql using getMemorySql() method.
disableSqlMemory(): voidDisables special query runner mode in which sql queries won't be executed. Previously memorized sql will be flushed.
clearSqlMemory(): voidFlushes all memorized sql statements.
getMemorySql(): SqlInMemory- returns
SqlInMemoryobject with array ofupQueriesanddownQueriessql statements
Gets sql stored in the memory. Parameters in the sql are already replaced.
executeMemoryUpSql(): Promise<void>Executes memorized up sql queries.
executeMemoryDownSql(): Promise<void>Executes memorized down sql queries.