API Reference

Kysely

Type-safe SQL query builder for TypeScript with schema-aware autocompletion, migrations, transactions, and no runtime ORM magic.

Official Documentation ↗
CompatibilityThis documentation covers version Kysely 0.27+; dialect packages and TypeScript schema types must match the database. Check the official docs for the latest API surface.

Installation

npmnpm install kysely
pnpmpnpm add kysely
yarnyarn add kysely

Methods

Kysely is a TypeScript query builder, not an ORM. It gives autocompletion and compile-time checks for tables and columns while keeping SQL explicit. The schema type is the contract: if the database changes and the type does not, the compiler catches many mismatches.

Dialect and pool

Kysely delegates connections to a dialect and driver. In Node.js with PostgreSQL, provide a pg pool and size it with the database connection budget. Kysely does not automatically close the pool; call db.destroy() during shutdown after in-flight queries finish.

SQL remains visible

Typed queries do not remove the need to inspect SQL. Use .compile() in tests or logs, and check query plans for new predicates. Prefer parameterized values and allowlist dynamic identifiers; raw interpolation is still an injection risk.

Security notes

Quoted from the official documentation (https://kysely.dev/); this is not a complete threat model.

  • However, we type safety and sanitization for DX and security was a must - hence Kysely. (https://kysely.dev/)

FAQ

Does Kysely replace SQL?
No. It builds SQL with typed expressions. You still need indexes, query plans, transactions, and an understanding of the generated SQL.
Is Kysely an ORM?
It is a type-safe query builder, not an ORM. It does not load object graphs or manage identity; you write queries explicitly.
How do I avoid SQL injection with Kysely?
Use its expression and value APIs; do not interpolate user input into raw SQL. For dynamic identifiers, validate against an allowlist before using sql.ref.