QueryInterface

Read this page in the documentation

QueryInterface QueryInterface is the imperative schema API: create and alter tables, manage indexes, constraints, views, sequences, triggers, policies and partitions, and run bulk data operations. It is what migrations are written against, and it normalises the dialect differences so one migration file runs everywhere. Every method takes an optional QueryInterfaceOptions: Option | Effect | --- | --- | transaction | Run inside a transaction — thread this through a migration so a failure rolls the whole thing back. | logging | Per-call logging override (false, true, or a function). | benchmark | Report elapsed time. | In a migration Tables Method | Purpose | --- | --- | createTable(name, columns, options?) | CREATE TABLE. | dropTable(name, options?) | DROP TABLE. | dropAllTables(options?) | Every table — destructive; for test teardown. | renameTable(from, to) | ALTER TABLE … RENAME TO. | showTables() / getTableNames(schema?) | List tables. | tableExists(name) | Existence check. | describeTable(name) | The table as the database reports it. | createDatabase(name) / dropDatabase(name) | Database-level operations. | Columns Method | Purpose | --- | --- | addColumn(table, name, definition, options?) | ALTER TABLE … ADD COLUMN. | removeColumn(table, name, options?) | Drop it (SQLite rebuilds the table). | changeColumn(table, name, definition, options?) | Change type/nullability/default. | renameColumn(table, from, to, options?) | Rename. | Indexes and constraints Method | Purpose | --- | --- | addIndex(table, fields, options?) | Create an index. | removeIndex(table, indexName) | Drop one. | showIndexes(table) / indexExists(table, name) | Inspect. | addConstraint(table, options) | PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK. | removeConstraint(table, name) | Drop one. | getConstraints(table) / getForeignKeysForTable(table) | Inspect. | Details and per-dialect caveats: Indexes & constraints. Schema objects Method | Purpose | --- | --- | createView / dropView / showViews / viewExists | Views. | createMaterializedView / refreshMaterializedView / dropMaterializedView / materializedViewExists / showMaterializedViews | Materialized views. | createSequence(name, options) / dropSequence(name, options?) | Sequences. | createTrigger(definition) / dropTrigger(name, table, options?) | Triggers. | enableRowLevelSecurity(table) / disableRowLevelSecurity(table) | RLS. | createPolicy(options) / dropPolicy(name, table, options?) | RLS policies. | createFullTextIndex(options) | Full-text index. | These delegate to the shared builders in src/schema-objects.ts, which throw UnsupportedSchemaObjectError — with an explanation — where the engine has no such object. See Schema objects for the SQL each one emits per dialect. Partitions createPartitionedTable, createPartition, attachPartition, detachPartition, dropPartition — RANGE, LIST and HASH, in each engine's syntax. Bulk data Method | Purpose | --- | --- | bulkInsert(table, rows, options?) | Insert many rows. | bulkUpdate(table, values, where, options?) | Update matching rows. | bulkDelete(table, where, options?) | Delete matching rows. | These are for migrations and seeders — data fix-ups, backfills, reference data — and deliberately skip the model layer, so no hooks, validations or timestamps run. Application writes belong on the model; see Bulk operations. Escape hatches query() is the way to run DDL the interface has no method for while staying inside the migration's transaction and logging. createQueryInterface Useful when you hold a dialect but not a Prorm instance — in a test, or in tooling built on the dialect layer. Related reading Migrations — where this API is normally used Schema objects Indexes & constraints Schema diffing — generating these calls from a model diff