Skip to main content

Kysely: Type-Safe SQL Query Builder

Kysely (pronounced “Key-Seh-Lee”) is a type-safe and autocompletion-friendly TypeScript SQL query builder. Inspired by Knex.js, Kysely brings the power of SQL with the safety of TypeScript to your database operations.

Type-Safe Queries

Kysely ensures you only reference tables and columns that exist in your database schema at compile time.

Full Autocompletion

Get intelligent autocompletion for table names, column names, and even inferred aliases.

Universal Runtime

Works seamlessly with Node.js, Deno, Bun, Cloudflare Workers, and web browsers.

Why Kysely?

Kysely makes sure you only refer to tables and columns that are visible to the part of the query you’re writing. The result type only has the selected columns with correct types and aliases. As an added bonus, you get autocompletion for all that stuff.

Intelligent Type Inference

Through the pure magic of modern TypeScript, Kysely is able to parse aliases and add them to the result row type. Kysely can infer column names, aliases, and types from:
  • Selected subqueries
  • Joined subqueries
  • WITH statements
  • Complex expressions
  • And much more
import { db } from './database'

const result = await db
  .selectFrom('person')
  .innerJoin('pet', 'pet.owner_id', 'person.id')
  .select([
    'person.id',
    'person.first_name',
    'pet.name as pet_name'
  ])
  .where('person.id', '=', 1)
  .executeTakeFirst()

// result has type:
// {
//   id: number
//   first_name: string
//   pet_name: string
// } | undefined

Supported Databases

Kysely supports all major SQL databases with first-class TypeScript support:

PostgreSQL

Full support with the PostgresDialect using the pg driver

MySQL

Full support with the MysqlDialect using the mysql2 driver

SQLite

Full support with the SqliteDialect using the better-sqlite3 driver

MS SQL Server

Full support with the MssqlDialect using the tedious driver

Get Started

Installation

Install Kysely with npm, yarn, pnpm, or bun

Quick Start

Get up and running with your first query in minutes

API Reference

Comprehensive API documentation with examples

Key Features

Schema Migrations

Kysely includes a powerful migration system for managing database schema changes:
import { Kysely, sql } from 'kysely'

export async function up(db: Kysely<any>): Promise<void> {
  await db.schema
    .createTable('user')
    .addColumn('user_id', 'uuid', (col) =>
      col.primaryKey().defaultTo(sql`gen_random_uuid()`)
    )
    .addColumn('first_name', 'text')
    .addColumn('last_name', 'text')
    .addColumn('email', 'text', (col) => col.unique())
    .addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql`NOW()`))
    .execute()
}

export async function down(db: Kysely<any>): Promise<void> {
  await db.schema.dropTable('user').execute()
}

Escape Hatches

When you need raw SQL for complex queries, Kysely provides the sql template tag:
import { sql } from 'kysely'

const result = await db
  .selectFrom('person')
  .select([
    'id',
    sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
  ])
  .execute()
All API documentation is written in the TypeScript definition files. Simply hover over any method in your IDE to see comprehensive documentation.

Community and Support

  • Discord: Join our Discord server for help and discussions
  • GitHub: Report issues or contribute on GitHub
  • Documentation: Visit kysely.dev for comprehensive guides
Kysely requires TypeScript 4.6 or later for full type-safety features.

Build docs developers (and LLMs) love