What is SQL to Kysely Type Generator?
Kysely is a strictly type-safe SQL query builder for TypeScript that catches query errors at compile time rather than runtime. Unlike ORMs that abstract away SQL, Kysely embraces raw SQL semantics while providing full autocompletion and type inference through a central Database interface. Every table becomes a property on this interface, and every column gets a precise TypeScript type — including nullable columns, auto-generated values, and JSON payloads. The Generated<> wrapper is Kysely's signature feature: it marks columns whose values are produced by the database (SERIAL sequences, DEFAULT expressions, trigger-populated fields), making them optional during INSERT but required in SELECT results. This converter parses your CREATE TABLE statements and produces the complete type scaffolding — per-table interfaces, the composite Database type, and optional Insertable/Updatable helper aliases — so you can start writing type-safe queries immediately without hand-mapping dozens of columns.
How to Use
- Paste one or more CREATE TABLE statements into the SQL input area
- Choose your database dialect — PostgreSQL, MySQL, or SQLite — to match type mappings correctly
- Toggle the Generated<> option to wrap auto-increment and DEFAULT columns, and the Insertable/Updatable option to emit helper types for INSERT and UPDATE operations
- Click "Generate Kysely Types" to produce the TypeScript interfaces
- Copy the output into a types.ts file in your project and import the Database type when creating a Kysely instance
Why Use This Tool?
Tips & Best Practices
- Always enable Generated<> for SERIAL and DEFAULT columns — omitting it forces you to pass values the database would generate itself
- Use Insertable<T> as the parameter type in repository functions to accept only the fields the database expects during INSERT
- For JSON and JSONB columns, replace the generated unknown type with a specific interface (e.g., Generated<UserMeta>) for stronger typing
- When adding a new table, regenerate the entire file rather than appending — the Database interface must contain every table to preserve type safety
Frequently Asked Questions
How does Kysely map SQL types to TypeScript?
Integer-family types (INTEGER, BIGINT, SERIAL) map to number; string types (VARCHAR, TEXT, CHAR) map to string; BOOLEAN maps to boolean; TIMESTAMP and DATE map to Date; JSON and JSONB map to unknown (replace with your own interface for stronger typing); BYTEA and BLOB map to Buffer; UUID maps to string. Nullable columns receive a | null union unless already wrapped in Generated<>.
When should I NOT use Kysely?
Avoid Kysely if your project needs a full ORM with change tracking, lazy loading, or automatic migration generation — consider MikroORM or TypeORM instead. Kysely also does not support Internet Explorer or environments without ES2020 features, and its type inference can slow the TypeScript compiler on schemas with hundreds of tables.
What does Generated<> actually do under the hood?
Generated<T> is an alias for ColumnType<T, T | undefined, T | undefined>. The three type parameters represent the select type (T), the insert type (T | undefined), and the update type (T | undefined). This means the column is always T when reading, but optional when writing — exactly matching how auto-generated columns behave in the database.
What are Insertable and Updatable helper types?
Insertable<TableType> extracts the insert shape of a table, making Generated columns optional. Updatable<TableType> extracts the update shape, making every column optional since UPDATE statements typically modify only a subset of fields. These helpers prevent you from accidentally passing read-only or auto-generated values in write operations.
Is my SQL data sent to a server?
No. All parsing and type generation runs entirely in your browser using a client-side regex parser. Your SQL schema never leaves your device, and no network requests are made during the conversion process.
Can I extend the generated Database interface with custom types?
Yes. After generating the base types, you can replace unknown (used for JSON columns) with your own interfaces, add intersection types for computed columns, or augment the Database interface with additional tables. Kysely's type system is designed to be extended manually after scaffolding.
Real-world Examples
E-commerce product catalog with JSON metadata
A products table with a JSONB metadata column and a SERIAL primary key, converted to Kysely types with Generated<> and Insertable/Updatable helpers.
CREATE TABLE products ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, price DECIMAL(10,2) NOT NULL, metadata JSONB, created_at TIMESTAMP DEFAULT NOW() );
import type { ColumnType, Generated, Insertable, Updatable } from 'kysely';
export interface ProductsTable {
id: Generated<number>;
name: string;
price: number;
metadata: unknown;
created_at: Generated<Date>;
}
export interface Database {
products: ProductsTable;
}
export type InsertableProducts = Insertable<Database['products']>;
export type UpdatableProducts = Updatable<Database['products']>;Blog with foreign key relationship
A comments table referencing a posts table, showing how nullable foreign keys and default values are handled in the Kysely type output.
CREATE TABLE posts ( id SERIAL PRIMARY KEY, title VARCHAR(255) NOT NULL, body TEXT ); CREATE TABLE comments ( id SERIAL PRIMARY KEY, post_id INTEGER NOT NULL REFERENCES posts(id), author VARCHAR(100) NOT NULL, content TEXT NOT NULL, created_at TIMESTAMP DEFAULT NOW() );
import type { ColumnType, Generated, Insertable, Updatable } from 'kysely';
export interface PostsTable {
id: Generated<number>;
title: string;
body: string | null;
}
export interface CommentsTable {
id: Generated<number>;
post_id: number;
author: string;
content: string;
created_at: Generated<Date>;
}
export interface Database {
posts: PostsTable;
comments: CommentsTable;
}
export type InsertablePosts = Insertable<Database['posts']>;
export type UpdatablePosts = Updatable<Database['posts']>;
export type InsertableComments = Insertable<Database['comments']>;
export type UpdatableComments = Updatable<Database['comments']>;