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:
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>
table
string
required
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
table
string
required
The name of the table to drop
returns
DropTableBuilder
A builder for dropping tables

Examples

await db.schema
  .dropTable('person')
  .execute()

createIndex

Create a new index.
createIndex(indexName: string): CreateIndexBuilder
indexName
string
required
The name of the index to create
returns
CreateIndexBuilder
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
indexName
string
required
The name of the index to drop
returns
DropIndexBuilder
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
schema
string
required
The name of the schema to create
returns
CreateSchemaBuilder
A builder for creating schemas

Examples

await db.schema
  .createSchema('some_schema')
  .execute()

dropSchema

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

Examples

await db.schema
  .dropSchema('some_schema')
  .execute()

alterTable

Alter a table.
alterTable(table: string): AlterTableBuilder
table
string
required
The name of the table to alter
returns
AlterTableBuilder
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
viewName
string
required
The name of the view to create
returns
CreateViewBuilder
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
viewName
string
required
The name of the view to drop
returns
DropViewBuilder
A builder for dropping views

Examples

await db.schema
  .dropView('dogs')
  .ifExists()
  .execute()

refreshMaterializedView

Refresh a materialized view.
refreshMaterializedView(viewName: string): RefreshMaterializedViewBuilder
viewName
string
required
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
typeName
string
required
The name of the type to create
returns
CreateTypeBuilder
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
typeName
string
required
The name of the type to drop
returns
DropTypeBuilder
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
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.
withoutPlugins(): SchemaModule
returns
SchemaModule
A new SchemaModule instance without plugins

withSchema

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

Build docs developers (and LLMs) love