Skip to main content
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:

Methods

createTable

Create a new table.
table
string
required
The name of the table to create
returns
CreateTableBuilder<TB, never>
A builder for creating table definitions

Examples

Create a table with a foreign key:
Create a table with a table-level foreign key constraint (for MySQL 5.X):

dropTable

Drop a table.
table
string
required
The name of the table to drop
returns
DropTableBuilder
A builder for dropping tables

Examples

createIndex

Create a new index.
indexName
string
required
The name of the index to create
returns
CreateIndexBuilder
A builder for creating indexes

Examples

dropIndex

Drop an index.
indexName
string
required
The name of the index to drop
returns
DropIndexBuilder
A builder for dropping indexes

Examples

createSchema

Create a new schema.
schema
string
required
The name of the schema to create
returns
CreateSchemaBuilder
A builder for creating schemas

Examples

dropSchema

Drop a schema.
schema
string
required
The name of the schema to drop
returns
DropSchemaBuilder
A builder for dropping schemas

Examples

alterTable

Alter a table.
table
string
required
The name of the table to alter
returns
AlterTableBuilder
A builder for altering tables

Examples

createView

Create a new view.
viewName
string
required
The name of the view to create
returns
CreateViewBuilder
A builder for creating views

Examples

dropView

Drop a view.
viewName
string
required
The name of the view to drop
returns
DropViewBuilder
A builder for dropping views

Examples

refreshMaterializedView

Refresh a materialized view.
viewName
string
required
The name of the materialized view to refresh
returns
RefreshMaterializedViewBuilder
A builder for refreshing materialized views

Examples

createType

Create a new type. Only some dialects like PostgreSQL have user-defined types.
typeName
string
required
The name of the type to create
returns
CreateTypeBuilder
A builder for creating types

Examples

dropType

Drop a type. Only some dialects like PostgreSQL have user-defined types.
typeName
string
required
The name of the type to drop
returns
DropTypeBuilder
A builder for dropping types

Examples

withPlugin

Returns a copy of this schema module with the given plugin installed.
plugin
KyselyPlugin
required
The plugin to install
returns
SchemaModule
A new SchemaModule instance with the plugin installed

withoutPlugins

Returns a copy of this schema module without any plugins.
returns
SchemaModule
A new SchemaModule instance without plugins

withSchema

Sets the schema to be used for all subsequent operations.
schema
string
required
The schema name to use
returns
SchemaModule
A new SchemaModule instance that uses the specified schema