Skip to main content
Kysely’s type system is built around defining a database interface that represents your database schema. This interface provides compile-time type safety for all your queries.

Basic Database Interface

A database interface is a TypeScript type where each key represents a table name and each value is an interface describing that table’s columns:

ColumnType<SelectType, InsertType, UpdateType>

The ColumnType type allows you to specify different types for select, insert, and update operations on a single column.

Examples

Read-only column (like database-generated timestamps):
Unupdateable column:

Generated<T>

A shortcut for database-generated columns that are optional in inserts and updates:

Usage

  • SELECT: Returns string (for user_id) or Date (for created_at)
  • INSERT: Optional (string | undefined or Date | undefined)
  • UPDATE: Same as select type

GeneratedAlways<T>

For columns that are always database-generated and cannot be inserted or updated:
Useful for PostgreSQL GENERATED ALWAYS AS IDENTITY columns:

JSONColumnType<SelectType, InsertType, UpdateType>

For JSON columns that are inserted/updated as stringified JSON:

Example

Selectable<R>, Insertable<R>, Updateable<R>

These utility types extract the appropriate type for each operation:

Complete Example

Here’s a complete database interface showing various column types:

Best Practices

Use Generated<T> for auto-increment IDs and timestamp columns that have database defaults.
Use GeneratedAlways<T> for identity columns or computed columns that the database always generates.
Define nullable columns as string | null, not string | undefined. SQL databases distinguish between NULL and missing values.
Export Selectable, Insertable, and Updateable type variants from your table definitions for use throughout your application.