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.

Introduction

Kysely (pronounced "Key-Seh-Lee") is a type-safe TypeScript SQL query builder that provides full autocompletion and compile-time validation of queries. Unlike ORMs that abstract SQL away, Kysely embraces SQL syntax while adding TypeScript's type system on top, catching column name typos and type mismatches before runtime.

What Kysely Does

  • Builds fully type-safe SQL queries with compile-time column and type validation
  • Supports PostgreSQL, MySQL, and SQLite with pluggable dialect architecture
  • Provides autocompletion for table names, column names, and available operations
  • Infers result types automatically from query structure without manual type annotations
  • Runs on Node.js, Deno, Bun, and Cloudflare Workers

Architecture Overview

Kysely uses TypeScript's type system to model the database schema as an interface. The QueryBuilder chains operations while propagating types through generics, so each method narrows the result type. Dialects implement a DialectAdapter and QueryCompiler that translate the internal query AST into database-specific SQL. The plugin system allows query transformation middleware for features like camelCase conversion.

Self-Hosting & Configuration

  • Install kysely plus a dialect package (pg for Postgres, mysql2 for MySQL, better-sqlite3 for SQLite)
  • Define your database interface matching table and column types
  • Create a Kysely instance with the dialect and database type parameter
  • Use kysely-codegen to auto-generate the database interface from an existing schema
  • Configure connection pool settings through the underlying driver options

Key Features

  • Zero code generation required; types flow through TypeScript generics at build time
  • Plugin architecture for query transformation (camelCase, soft delete, logging)
  • Migration system with up/down functions and programmatic execution
  • JSON manipulation support with type-safe jsonPath expressions
  • Subquery and CTE (WITH clause) support with full type inference

Comparison with Similar Tools

  • Knex.js — similar API but no type safety; Kysely catches errors at compile time
  • Prisma — schema-first with codegen; Kysely is code-first with no generation step
  • Drizzle ORM — also type-safe but includes ORM features; Kysely stays closer to raw SQL
  • TypeORM — decorator-based ORM; Kysely is a query builder that embraces SQL directly
  • Slonik — type-safe Postgres client with tagged templates; Kysely uses a fluent builder API

FAQ

Q: Do I need code generation for Kysely? A: No. Type safety comes from TypeScript generics. However, kysely-codegen can auto-generate the database interface from your schema for convenience.

Q: Can Kysely handle complex queries like CTEs and window functions? A: Yes. Kysely supports WITH clauses, window functions, JSON operations, and subqueries with full type inference throughout.

Q: How does Kysely compare to Drizzle? A: Kysely is a pure query builder that stays close to SQL. Drizzle adds ORM features like schema declarations and relational queries on top.

Q: Does Kysely work with edge runtimes? A: Yes. Kysely runs on Cloudflare Workers, Deno Deploy, and Vercel Edge Functions with the appropriate dialect.

Sources

Discussion

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

Related Assets