Guide · Open source
How to use Kysely: install, write typed SQL queries and tips
Updated · written with AI from the project's own documentation and checked against our catalogue data
Kysely is a TypeScript SQL query builder. Instead of writing SQL as plain strings, you chain method calls, and TypeScript checks that the tables and columns you reference exist and returns result types that match what you selected. This guide explains how to install it, what it is good for, and where it stops, so you can decide whether it fits your project.
Quick start
- 1Check prerequisites. Confirm you have Node.js or another JavaScript runtime, and a supported database such as PostgreSQL, MySQL, SQLite or MSSQL.
- 2Install the package. Run npm install kysely in your project directory.
- 3Pick your dialect. Open the getting-started docs at kysely.dev and select the dialect that matches your database.
- 4Connect to the database. Follow the dialect guide to set up the connection for your database.
- 5Write a typed query. Chain methods to select, filter and join tables, and let your editor autocomplete table and column names.
- 6Use raw SQL where needed. For queries the types cannot express, use the sql template tag or DynamicModule as described in the API docs.
What Kysely is and who it is for
Kysely (pronounced “Key-Seh-Lee”) is a type-safe, autocompletion-friendly SQL query builder for TypeScript. It is inspired by Knex.js and is mainly developed for Node.js, though the project says it also runs in Deno, Bun, Cloudflare Workers and web browsers.
Kysely makes sure you only refer to tables and columns that are visible to the part of the query you are writing, and the result type only includes the selected columns with correct types and aliases.
- Good fit: TypeScript developers who want SQL control with compile-time checks, and Node.js backends querying PostgreSQL, MySQL, SQLite or MSSQL.
- Poor fit: projects not using TypeScript, people who want a full ORM with models, migrations and relationships handled for them, and beginners who are still learning SQL.
Install Kysely with npm
Kysely is installed through a package manager. You need Node.js or another JavaScript runtime. TypeScript is recommended, because the main benefit depends on the type system. You also need a supported database such as PostgreSQL, MySQL, SQLite or MSSQL.
- Prerequisites: a JavaScript runtime (Node.js or similar), TypeScript (recommended), and a supported database.
npm install kyselyConnect Kysely to your database
The project’s documentation site has a getting-started guide for each supported dialect. Pick the one that matches your database, since the setup differs between PostgreSQL, MySQL, SQLite, MSSQL and PGlite. The README links to these per-dialect pages rather than listing setup code, so follow them for the exact connection steps.
- Start from kysely.dev and choose your dialect in the getting-started docs.
- Use the API documentation at kysely-org.github.io/kysely-apidoc for method reference.
Write and read typed queries
You build queries by chaining methods instead of writing raw SQL strings. Because Kysely knows your table definitions, your editor autocompletes table and column names. Selected subqueries, joined subqueries and with statements also have their column names and types inferred.
The README notes that Kysely can parse an alias such as pet.name as pet_name and add that column to the result row type.
- Hover over any class or method in your IDE to read its documentation, since API docs live in the typing files.
- Autocompletion and inferred result types are the main reason to choose Kysely.
Use raw SQL when the types cannot express a query
Some queries cannot be typed at compile time. For these, Kysely offers escape hatches: the sql template tag and DynamicModule. The API docs describe both, so you can keep the rest of your queries typed while dropping to SQL where needed.
- Use the sql template tag for SQL fragments the builder cannot type.
- Check the DynamicModule documentation for dynamic cases.
Get help and report problems
If you cannot find something you need, the README asks you to open an issue on the GitHub repository or join the Discord server. The project is MIT licensed, so you can use it in commercial and private projects under those terms.
- Issues: github.com/kysely-org/kysely/issues
- Discord: discord.gg/xyBJ3GwvAm
What people use it for
- Building a Node.js backend API that queries PostgreSQL with typed results.
- Writing reports that join and alias several tables while keeping result types correct.
- Running typed queries against SQLite in a project using TypeScript.
- Adding raw SQL fragments to a typed query where the builder cannot express the case.
Frequently asked questions
Is Kysely free to use?
Yes. Kysely is released under the MIT licence, as stated in the project’s licence badge and repository. Check the LICENSE file in the repository for the full terms.
What are the requirements for Kysely?
You need Node.js or another JavaScript runtime and a supported database such as PostgreSQL, MySQL, SQLite or MSSQL. TypeScript is recommended, because the main benefit depends on the type system.
Is Kysely an ORM?
No. Kysely is a query builder. It does not handle models, migrations or relationships for you. If you want those features, the project lists Prisma, TypeORM and Drizzle ORM as comparable projects.
How does Kysely compare to Knex.js?
Kysely is inspired by Knex.js. Its main difference, according to the README, is type safety: it checks table and column references at compile time and infers result types.
Can I use Kysely without TypeScript?
The project recommends TypeScript, and its main benefit depends on the type system. If you do not use TypeScript, you lose most of what makes Kysely distinctive, so consider a different tool.
What do I do when a query cannot be typed?
Use Kysely’s escape hatches: the sql template tag and DynamicModule. The API documentation at kysely-org.github.io/kysely-apidoc describes both.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
Kysely suits TypeScript developers who want to write SQL with compile-time checks, autocompletion and typed results across PostgreSQL, MySQL, SQLite or MSSQL. If you want an ORM that manages models, migrations and relationships, or you are still learning SQL, choose a different tool or start with plain SQL.
Install options & downloads