gitmyhub

kysely

TypeScript ★ 14k updated 2d ago

A type-safe TypeScript SQL query builder

A TypeScript SQL query builder that catches database errors at compile time by letting you describe your schema as types, so missing tables, wrong columns, and bad values are flagged in your editor before the code ever runs.

TypeScriptNode.jsDenoBunsetup: moderatecomplexity 2/5

Kysely (pronounced Key-Seh-Lee) is a library for building SQL queries in TypeScript. Its defining feature is that it checks your queries at compile time, before the code ever runs. When you reference a table or column that does not exist, or when you try to assign a value of the wrong type to a column, TypeScript catches the error immediately in your editor rather than at runtime.

The way this works is that you describe your database schema to Kysely as TypeScript types. From that point on, every time you write a query, the library uses those types to verify that the tables and columns you reference are real and that the values you use are the right kind. The result of each query is also typed, so you know exactly which columns will come back and what their types are, including column aliases you create inside the query. Your code editor can also use these types to autocomplete table names, column names, and method names as you type.

Kysely covers the common SQL operations: SELECT, INSERT, UPDATE, DELETE, JOIN, subqueries, CTEs (the WITH keyword), transactions, and more. For cases where the static type system cannot express what you need, there is an escape hatch that lets you write raw SQL strings and still integrate them with the typed query builder.

The library runs on Node.js, Deno, Bun, Cloudflare Workers, and in web browsers. It has built-in dialects for PostgreSQL, MySQL, Microsoft SQL Server, and SQLite. The community has built additional dialects for other databases. It is available on npm and JSR. Documentation lives on kysely.dev and the project has a Discord server for questions.

Where it fits