The SchemaModule class provides methods for building and managing database schema. It allows you to create, alter, and drop tables, indexes, schemas, views, and types.
Overview
The schema module is accessed through the schema property of the Kysely instance:
const db = new Kysely<Database>(config)
db.schema // SchemaModule instance
Methods
createTable
Create a new table.
createTable<TB extends string>(table: TB): CreateTableBuilder<TB, never>
The name of the table to create
returns
CreateTableBuilder<TB, never>
A builder for creating table definitions
Examples
await db.schema
.createTable('person')
.addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
.addColumn('first_name', 'varchar', col => col.notNull())
.addColumn('last_name', 'varchar', col => col.notNull())
.addColumn('gender', 'varchar')
.execute()
Create a table with a foreign key:
await db.schema
.createTable('pet')
.addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
.addColumn('owner_id', 'integer', col => col
.references('person.id')
.onDelete('cascade')
)
.execute()
Create a table with a table-level foreign key constraint (for MySQL 5.X):
await db.schema
.createTable('pet')
.addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
.addColumn('owner_id', 'integer')
.addForeignKeyConstraint(
'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
(constraint) => constraint.onDelete('cascade')
)
.execute()
dropTable
Drop a table.
dropTable(table: string): DropTableBuilder
The name of the table to drop
A builder for dropping tables
Examples
await db.schema
.dropTable('person')
.execute()
createIndex
Create a new index.
createIndex(indexName: string): CreateIndexBuilder
The name of the index to create
A builder for creating indexes
Examples
await db.schema
.createIndex('person_full_name_unique_index')
.on('person')
.columns(['first_name', 'last_name'])
.execute()
dropIndex
Drop an index.
dropIndex(indexName: string): DropIndexBuilder
The name of the index to drop
A builder for dropping indexes
Examples
await db.schema
.dropIndex('person_full_name_unique_index')
.execute()
createSchema
Create a new schema.
createSchema(schema: string): CreateSchemaBuilder
The name of the schema to create
A builder for creating schemas
Examples
await db.schema
.createSchema('some_schema')
.execute()
dropSchema
Drop a schema.
dropSchema(schema: string): DropSchemaBuilder
The name of the schema to drop
A builder for dropping schemas
Examples
await db.schema
.dropSchema('some_schema')
.execute()
alterTable
Alter a table.
alterTable(table: string): AlterTableBuilder
The name of the table to alter
A builder for altering tables
Examples
await db.schema
.alterTable('person')
.alterColumn('first_name', (ac) => ac.setDataType('text'))
.execute()
createView
Create a new view.
createView(viewName: string): CreateViewBuilder
The name of the view to create
A builder for creating views
Examples
await db.schema
.createView('dogs')
.orReplace()
.as(db.selectFrom('pet').selectAll().where('species', '=', 'dog'))
.execute()
dropView
Drop a view.
dropView(viewName: string): DropViewBuilder
The name of the view to drop
A builder for dropping views
Examples
await db.schema
.dropView('dogs')
.ifExists()
.execute()
refreshMaterializedView
Refresh a materialized view.
refreshMaterializedView(viewName: string): RefreshMaterializedViewBuilder
The name of the materialized view to refresh
returns
RefreshMaterializedViewBuilder
A builder for refreshing materialized views
Examples
await db.schema
.refreshMaterializedView('my_view')
.concurrently()
.execute()
createType
Create a new type.
Only some dialects like PostgreSQL have user-defined types.
createType(typeName: string): CreateTypeBuilder
The name of the type to create
A builder for creating types
Examples
await db.schema
.createType('species')
.asEnum(['dog', 'cat', 'frog'])
.execute()
dropType
Drop a type.
Only some dialects like PostgreSQL have user-defined types.
dropType(typeName: string): DropTypeBuilder
The name of the type to drop
A builder for dropping types
Examples
await db.schema
.dropType('species')
.ifExists()
.execute()
withPlugin
Returns a copy of this schema module with the given plugin installed.
withPlugin(plugin: KyselyPlugin): SchemaModule
A new SchemaModule instance with the plugin installed
withoutPlugins
Returns a copy of this schema module without any plugins.
withoutPlugins(): SchemaModule
A new SchemaModule instance without plugins
withSchema
Sets the schema to be used for all subsequent operations.
withSchema(schema: string): SchemaModule
A new SchemaModule instance that uses the specified schema