Kysely — Type-Safe SQL Query Builder for TypeScript
A type-safe and autocompletion-friendly TypeScript SQL query builder that catches query errors at compile time without code generation.
What it is
Kysely (pronounced 'Key-Seh-Lee') is a type-safe TypeScript SQL query builder. It provides full autocompletion and compile-time error checking for your SQL queries without requiring code generation. You define your database schema as TypeScript types, and Kysely ensures every query is valid at build time.
Kysely targets TypeScript developers who want the safety of an ORM's type system but the flexibility of writing raw SQL. It supports PostgreSQL, MySQL, SQLite, and any database with a compatible dialect.
How it saves time or tokens
Kysely catches SQL errors at compile time rather than at runtime. Misspelled column names, wrong join conditions, and type mismatches are flagged by the TypeScript compiler before your code runs. Full autocompletion in your IDE means you do not need to memorize table and column names. Unlike ORMs, Kysely generates predictable SQL that maps directly to what you write, avoiding the N+1 query surprises common with Prisma or TypeORM.
How to use
- Install Kysely with your database driver:
npm install kysely pg
- Define your database types:
import { Generated, Kysely, PostgresDialect } from 'kysely';
import { Pool } from 'pg';
interface Database {
users: {
id: Generated<number>;
name: string;
email: string;
created_at: Generated<Date>;
};
posts: {
id: Generated<number>;
title: string;
user_id: number;
};
}
const db = new Kysely<Database>({
dialect: new PostgresDialect({ pool: new Pool() })
});
- Write type-safe queries:
const users = await db
.selectFrom('users')
.select(['id', 'name', 'email'])
.where('email', 'like', '%@example.com')
.execute();
// TypeScript knows users is Array<{id: number, name: string, email: string}>
Example
// Join query with full type safety
const postsWithAuthors = await db
.selectFrom('posts')
.innerJoin('users', 'users.id', 'posts.user_id')
.select(['posts.title', 'users.name as author'])
.orderBy('posts.id', 'desc')
.limit(10)
.execute();
// Insert with returning
const newUser = await db
.insertInto('users')
.values({ name: 'Alice', email: 'alice@example.com' })
.returning(['id', 'name'])
.executeTakeFirstOrThrow();
Related on TokRepo
- AI Tools for Database — Database tools and query builders
- AI Tools for Coding — Developer productivity tools
Common pitfalls
- Kysely requires you to define database types manually; there is no automatic introspection from an existing database (though community tools exist for generating types from schemas).
- Kysely is a query builder, not an ORM; it does not handle migrations, relations, or model lifecycle hooks. Pair it with a migration tool like Knex or kysely-migrations.
- Complex subqueries and CTEs are supported but require careful type annotation; the TypeScript inference can become complex for deeply nested queries.
Frequently Asked Questions
Prisma is a full ORM with schema definition, migrations, and generated client. Kysely is a lightweight query builder that gives you type-safe SQL without the ORM layer. Kysely produces predictable SQL and avoids the N+1 problems common with ORMs.
Kysely has basic migration support via the Migrator class. For more advanced migrations, you can pair it with tools like kysely-ctl or use standalone migration tools.
Kysely officially supports PostgreSQL, MySQL, and SQLite through dialect packages. Community dialects exist for other databases. The architecture is extensible for custom dialects.
No. Kysely works with plain TypeScript types. You define your database schema as interfaces, and the query builder infers types from them at compile time. No code generation step is needed.
Yes. Kysely provides sql template literals for raw SQL expressions within type-safe queries. You can mix raw SQL with the query builder for cases where the builder API is insufficient.
Citations (3)
- Kysely GitHub— Kysely provides type-safe SQL without code generation
- Kysely Documentation— Kysely supports PostgreSQL, MySQL, and SQLite dialects
- Kysely API Reference— Kysely query builder with TypeScript autocompletion
Related on TokRepo
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.