ScriptsApr 16, 2026·3 min read

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.

TL;DR
Kysely provides compile-time type checking for SQL queries in TypeScript without code generation steps.
§01

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.

§02

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.

§03

How to use

  1. Install Kysely with your database driver:
npm install kysely pg
  1. 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() })
});
  1. 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}>
§04

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();
§05

Related on TokRepo

§06

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

How is Kysely different from Prisma?+

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.

Does Kysely support migrations?+

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.

What databases does Kysely support?+

Kysely officially supports PostgreSQL, MySQL, and SQLite through dialect packages. Community dialects exist for other databases. The architecture is extensible for custom dialects.

Does Kysely generate code?+

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.

Can I use raw SQL with Kysely?+

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)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets