Create a new QueryInterface instance
The database dialect to use
Get the dialect instance
Create a new table
Name of the table to create
Column definitions
Optionaloptions: TableOptions & QueryInterfaceOptions
Table options
Create a partitioned table (PostgreSQL 10+)
Name of the table to create
Column definitions
Optionaloptions: TableOptions & QueryInterfaceOptions & {Table options including partition configuration
Create a partition for an existing partitioned table (PostgreSQL 10+)
Partition creation options
Attach a partition to a partitioned table (PostgreSQL 11+)
Partition attachment options
Detach a partition from a partitioned table (PostgreSQL 11+)
Partition detachment options
Drop a partition (PostgreSQL 10+)
Name of the partition to drop
Optionaloptions: { ifExists?: boolean; cascade?: boolean } & QueryInterfaceOptions
Drop options
Drop a table
Name of the table to drop
Optionaloptions: DropTableOptions & QueryInterfaceOptions
Drop options
Drop all tables (dialect-specific)
Optional_options: QueryInterfaceOptionsRename a table
Current table name
New table name
Optional_options: QueryInterfaceOptionsAdd a column to a table
Name of the table
Name of the new column
Column definition
Optional_options: QueryInterfaceOptionsRemove a column from a table
Name of the table
Name of the column to remove
Optional_options: QueryInterfaceOptionsChange a column definition
Name of the table
Name of the column to change
New column definition
Optional_options: QueryInterfaceOptionsRename a column
Name of the table
Current column name
New column name
Optional_options: QueryInterfaceOptionsAdd an index to a table
Name of the table
OptionalfieldsOrOptions: string[] | IndexFieldDefinition[] | IndexOptions & QueryInterfaceOptionsOptionaloptions: IndexOptions & QueryInterfaceOptions
Index options
Remove an index from a table
Name of the table
Name of the index to remove
Optional_options: QueryInterfaceOptionsShow all indexes for a table
Name of the table
Add a constraint to a table
Name of the table
OptionalconstraintOrOptions: ConstraintDefinitionOptional_options: QueryInterfaceOptionsRemove a constraint from a table
Name of the table
Name of the constraint to remove
Optional_typeOrOptions: string | QueryInterfaceOptionsCheck if an index exists on a table
Bulk insert records
Name of the table
Records to insert
Optional_options: BulkOperationOptionsBulk update records
Name of the table
Values to update
Where clause
Optional_options: BulkOperationOptionsBulk delete records
Name of the table
Where clause
Optional_options: BulkOperationOptionsDescribe a table (get column information)
Name of the table
Show all tables in the database
Check if a table exists
Name of the table
Get all table names with schema (for PostgreSQL, MySQL, etc.)
Optional_schema: string
Optional schema name
Get foreign keys for a table
Get constraints for a table
Create a database (for databases that support it)
Optional_options: QueryInterfaceOptionsDrop a database (for databases that support it)
Optional_options: QueryInterfaceOptionsCreate a database view.
Define the view the same way you would write the query it saves — a view is
a saved query, so definition takes the findAll() options and the dialect
compiles them:
await qi.createView('customer_info', {
from: Customer,
attributes: ['firstName', 'lastName', 'email'],
where: { country: 'USA' },
});
Name of the view to create
The view body as query options. A raw SELECT string is still accepted for backwards compatibility, but writing SQL by hand gives up dialect portability and identifier quoting; prefer the object form.
Optionaloptions: ViewOptions & QueryInterfaceOptions
View options
Drop a database view
Name of the view to drop
Optionaloptions: DropViewOptions & QueryInterfaceOptions
Drop options
Show all views in the database
Check if a view exists
Name of the view
Create a materialized view.
Takes the same structured definition as createView — pass query
options rather than a SELECT string:
await qi.createMaterializedView({
name: 'order_totals',
definition: {
from: Order,
attributes: ['userId', [fn('COUNT', col('id')), 'orderCount']],
group: 'userId',
},
});
Materialized view options, carrying either a definition
or (for backwards compatibility) a raw query string.
Refresh a materialized view
Name of the materialized view to refresh
Optionaloptions: RefreshOptions & QueryInterfaceOptions
Refresh options
Drop a materialized view
Name of the materialized view to drop
Optionaloptions: DropMaterializedViewOptions & QueryInterfaceOptions
Drop options
Check if a materialized view exists
Name of the materialized view
Show all materialized views in the database
Create a sequence.
Drop a sequence.
Optionaloptions: { ifExists?: boolean }Create a trigger.
On PostgreSQL this also creates the backing trigger function, which the
database requires and which a hand-written CREATE TRIGGER easily forgets.
Drop a trigger.
Optionaloptions: { ifExists?: boolean }Enable row-level security on a table (PostgreSQL family).
Disable row-level security on a table (PostgreSQL family).
Register a function written in a language other than SQL.
Two shapes, matching what databases offer. Compiled — you wrote C, built a shared library, and want the database to call into it:
// MySQL: the library lives in the server's plugin_dir
await qi.createExternalFunction({
name: 'levenshtein',
language: 'c',
returns: 'INTEGER',
library: 'my_udf.so',
});
// PostgreSQL: object file plus the exported symbol
await qi.createExternalFunction({
name: 'levenshtein',
language: 'c',
returns: 'integer',
params: [{ name: 'a', type: 'text' }, { name: 'b', type: 'text' }],
library: '$libdir/my_udf',
symbol: 'pg_levenshtein',
strict: true,
volatility: 'IMMUTABLE',
});
Or interpreted — the source is stored in the database and run by a procedural-language handler:
await qi.createExternalFunction({
name: 'slugify',
language: 'plpython3u',
returns: 'text',
params: [{ name: 'value', type: 'text' }],
source: 'import re\nreturn re.sub(r"[^a-z0-9]+", "-", value.lower()).strip("-")',
});
source is code in that language — it is stored verbatim and never parsed
as SQL. MySQL supports only the compiled form.
Drop an external function.
PostgreSQL identifies overloads by argument types, so pass the same
params used to create it when more than one overload exists.
Optionaloptions: { ifExists?: boolean; params?: StoredProcedureParam[]; schema?: string }Drop a row-level security policy.
Optionaloptions: { ifExists?: boolean }Create a full-text index.
Emits a GIN index over to_tsvector on PostgreSQL, a FULLTEXT index on
MySQL/MariaDB, and an FTS5 virtual table on SQLite.
Execute a raw query
The SQL query
Optionaloptions: QueryInterfaceOptions
Query options
Quote an identifier
The identifier to quote
Escape a value
The value to escape
CreateQueryInterface - Provides migration operations for all database dialects