Last Updated: 3/20/2026
Generating Types
Kysely requires a Database interface that maps table names to table schema interfaces. For small projects, defining this manually is straightforward. For production applications, you’ll want to generate it automatically from your database schema to stay in sync as your schema evolves.
Why Generate Types?
Manually maintaining your Database interface has drawbacks:
- Drift — Your TypeScript types can fall out of sync with your actual database schema.
- Tedium — Adding a column means updating both your migration and your TypeScript interface.
- Error-prone — It’s easy to misspell a column name or use the wrong type.
Generating types from your database schema ensures your TypeScript code always matches your database structure. If you try to query a column that doesn’t exist, TypeScript will catch it at compile time.
kysely-codegen
kysely-codegen connects to your database, introspects the schema, and generates a Database interface. It works with all built-in Kysely dialects: PostgreSQL, MySQL, SQLite, and MSSQL.
Installation
npm install --save-dev kysely-codegenUsage
Run the CLI tool, passing your database connection string:
# PostgreSQL
npx kysely-codegen --dialect postgres --url postgres://user:pass@localhost:5432/mydb
# MySQL
npx kysely-codegen --dialect mysql --url mysql://user:pass@localhost:3306/mydb
# SQLite
npx kysely-codegen --dialect sqlite --url mydb.db
# MSSQL
npx kysely-codegen --dialect mssql --url Server=localhost;Database=mydb;User Id=sa;Password=pass;This generates a file (by default src/db/types.ts) containing your Database interface:
import type { ColumnType } from 'kysely'
export interface Database {
person: PersonTable
pet: PetTable
}
export interface PersonTable {
id: Generated<number>
first_name: string
last_name: string | null
created_at: ColumnType<Date, string | undefined, never>
}
export interface PetTable {
id: Generated<number>
name: string
owner_id: number
species: 'dog' | 'cat' | 'bird'
}Configuration
You can customize the output using CLI flags:
--out-file— Output file path (default:src/db/types.ts)--schema— Schema name (PostgreSQL/MSSQL only)--exclude-pattern— Regex pattern to exclude tables--include-pattern— Regex pattern to include tables--camel-case— Convert snake_case to camelCase
See the kysely-codegen README for all options.
When to Use
Use kysely-codegen when:
- You’re starting a new project and want to generate types from an existing database.
- You’re using Kysely migrations and want to regenerate types after each migration.
- You want a simple, zero-config solution that works with all Kysely dialects.
prisma-kysely
prisma-kysely generates Kysely types from your Prisma schema. If you’re already using Prisma for migrations or schema management, this lets you use Kysely for queries while keeping Prisma as your source of truth.
Installation
npm install --save-dev prisma-kyselyUsage
Add the generator to your schema.prisma:
generator kysely {
provider = "prisma-kysely"
output = "../src/db"
fileName = "types.ts"
}
model Person {
id Int @id @default(autoincrement())
firstName String @map("first_name")
lastName String? @map("last_name")
pets Pet[]
}
model Pet {
id Int @id @default(autoincrement())
name String
ownerId Int @map("owner_id")
owner Person @relation(fields: [ownerId], references: [id])
}Run the Prisma generator:
npx prisma generateThis generates a Database interface in src/db/types.ts.
When to Use
Use prisma-kysely when:
- You’re already using Prisma for schema management and migrations.
- You want to use Kysely for complex queries but keep Prisma as your schema source of truth.
- You prefer defining your schema in Prisma’s DSL rather than writing SQL migrations.
kanel-kysely
kanel-kysely is a Kysely adapter for Kanel, a mature PostgreSQL type generator. It connects to your PostgreSQL database and generates Kysely types.
Installation
npm install --save-dev kanel kanel-kyselyUsage
Create a .kanelrc.js configuration file:
const { makeKyselyHook } = require('kanel-kysely')
module.exports = {
connection: {
host: 'localhost',
user: 'postgres',
password: 'secret',
database: 'mydb',
},
preDeleteOutputFolder: true,
outputPath: './src/db',
preRenderHooks: [makeKyselyHook()],
}Run Kanel:
npx kanelThis generates Kysely-compatible types in src/db/.
When to Use
Use kanel-kysely when:
- You’re using PostgreSQL exclusively.
- You want a mature, battle-tested type generator with extensive configuration options.
- You need advanced features like custom type mappings or per-table hooks.
kysely-schema-generator
kysely-schema-generator connects to your MySQL database and generates Kysely types. Currently MySQL-only.
Installation
npm install --save-dev kysely-schema-generatorUsage
Run the CLI tool:
npx kysely-schema-generator --host localhost --user root --password secret --database mydbThis generates a Database interface.
When to Use
Use kysely-schema-generator when:
- You’re using MySQL.
- You want a simple, focused tool for MySQL type generation.
Choosing a Tool
| Tool | Dialects | Source | Best For |
|---|---|---|---|
| kysely-codegen | PostgreSQL, MySQL, SQLite, MSSQL | Database introspection | General-purpose, all dialects |
| prisma-kysely | All (via Prisma) | Prisma schema | Existing Prisma projects |
| kanel-kysely | PostgreSQL | Database introspection | PostgreSQL with advanced needs |
| kysely-schema-generator | MySQL | Database introspection | MySQL-only projects |
Workflow Integration
Integrate type generation into your development workflow:
After Migrations
Regenerate types after running migrations:
{
"scripts": {
"migrate": "kysely migrate:latest && npm run generate-types",
"generate-types": "kysely-codegen --dialect postgres --url $DATABASE_URL"
}
}Watch Mode
Some tools support watch mode for continuous regeneration during development. Check the tool’s documentation for details.
CI/CD
Add type generation to your CI pipeline to ensure types are always up-to-date:
- name: Generate types
run: npm run generate-types
- name: Type check
run: npm run type-checkWhat’s Next
- Schema Builder — Use
db.schemato create and alter tables, then regenerate types. - Migrations — Learn how to manage schema changes over time.
- Dialects — Understand how different databases handle types differently.