kysely
v0.28.15 MITType safe SQL query builder
kysely Download Trends
About kysely
Kysely is a type-safe SQL query builder designed to bring robust type checking to database interactions within JavaScript and TypeScript applications. It addresses the common problem of runtime errors arising from SQL queries that don't align with the application's data models, ensuring that your queries are validated at compile time.
Its core philosophy centers on developer experience and type safety. Kysely aims to make writing SQL queries feel as secure as writing type-safe application code, empowering developers to refactor with confidence and reduce the cognitive load associated with manual type management. The primary audience includes TypeScript developers working with SQL databases who value correctness and maintainability.
Key API patterns include a fluent interface for constructing queries and a schema definition approach that maps directly to your database tables and columns. You define your database schema using TypeScript interfaces, and Kysely uses this to provide autocompletion and type checking for methods like `select()`, `where()`, and `insertInto()`.
Kysely integrates well into various Node.js, Deno, and Bun environments. It provides drivers for popular databases like PostgreSQL, MySQL, and SQLite, fitting seamlessly into existing application architectures without dictating a specific framework. Its design also allows it to be used with various ORM patterns or as a standalone query builder.
With a weekly download count exceeding 3.6 million and 13.6K GitHub stars, Kysely demonstrates significant community adoption and maturity. Its unpacked size is 3.3 MB, with a gzipped bundle size of only 37.6 kB, making it a relatively lightweight dependency despite its comprehensive features.
While Kysely offers strong type safety, it does not abstract away SQL entirely. Developers still need a solid understanding of SQL query construction and database concepts. It also relies on accurate schema definitions; any drift between the defined schema and the actual database can lead to type mismatches, though compile-time checks mitigate this significantly.
When to use
- When you need compile-time validation for SQL queries against your defined database schema.
- To improve developer productivity in TypeScript projects by leveraging autocompletion for table names, column names, and query conditions.
- When migrating from string-based SQL queries to a type-safe approach to catch errors early in the development cycle.
- For building applications that interact with multiple SQL database types like PostgreSQL, MySQL, or SQLite within a unified TypeScript codebase.
- To enable safer refactoring of database schemas and queries, providing confidence that type safety is maintained across changes.
- When you require fine-grained control over SQL query construction while still benefiting from robust type checking.
When NOT to use
- If your primary interaction with the database is simple key-value retrieval; a more minimal database client or data structure might suffice.
- When you prefer an ORM that completely abstracts away SQL syntax and schema management in favor of a fully object-oriented approach.
- If your project exclusively uses NoSQL databases, as Kysely is specifically designed for relational SQL databases.
- For environments where JavaScript runtime features are heavily restricted and manual SQL string generation is the only viable option.
- When you are building an application where zero external dependencies are a strict requirement, and all logic must be self-contained.