What is kysely?
Kysely is a type-safe SQL query builder for TypeScript. It allows developers to write SQL queries in a type-safe manner, ensuring that the queries are correct at compile time. Kysely supports various SQL databases and provides a fluent API for building complex queries.
What are kysely's main functionalities?
Type-safe Query Building
Kysely allows you to build SQL queries in a type-safe manner. The above code demonstrates a simple SELECT query that retrieves the 'id' and 'first_name' columns from the 'person' table where the 'id' is equal to 1.
const result = await db.selectFrom('person').select(['id', 'first_name']).where('id', '=', 1).execute();
Insert Queries
Kysely supports type-safe insert queries. The above code demonstrates how to insert a new row into the 'person' table with the specified values.
const result = await db.insertInto('person').values({ first_name: 'John', last_name: 'Doe' }).execute();
Update Queries
Kysely allows you to perform type-safe update queries. The above code demonstrates how to update the 'first_name' column of the 'person' table where the 'id' is equal to 1.
const result = await db.updateTable('person').set({ first_name: 'Jane' }).where('id', '=', 1).execute();
Delete Queries
Kysely supports type-safe delete queries. The above code demonstrates how to delete a row from the 'person' table where the 'id' is equal to 1.
const result = await db.deleteFrom('person').where('id', '=', 1).execute();
Joins
Kysely allows you to perform type-safe join operations. The above code demonstrates an inner join between the 'person' and 'address' tables, selecting the 'id' from the 'person' table and the 'city' from the 'address' table.
const result = await db.selectFrom('person').innerJoin('address', 'person.id', 'address.person_id').select(['person.id', 'address.city']).execute();
Other packages similar to kysely
knex
Knex.js is a SQL query builder for Node.js that supports various SQL databases. It provides a fluent API for building queries but lacks the type-safety features of Kysely. Knex is widely used and has a large community.
sequelize
Sequelize is a promise-based Node.js ORM for various SQL databases. It provides a higher-level abstraction over SQL queries and includes features like model definitions, associations, and migrations. Unlike Kysely, Sequelize is more focused on being an ORM rather than just a query builder.
typeorm
TypeORM is an ORM for TypeScript and JavaScript that supports various SQL databases. It provides a high-level API for defining entities, relationships, and performing database operations. TypeORM offers type-safety but is more feature-rich compared to Kysely, as it includes migrations, caching, and more.
Kysely (pronounce “Key-Seh-Lee”) is a type-safe and autocompletion-friendly typescript SQL query builder.
Inspired by knex. Mainly developed for node.js but also
runs on all other javascript environments like deno.
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.
As shown in the gif above, through the pure magic of modern typescript, Kysely is even able to parse
the alias given to pet.name
and add the pet_name
column to the result row type. Kysely is able to infer
column names, aliases and types from selected subqueries, joined subqueries, with
statements and pretty
much anything you can think of.
Of course there are cases where things cannot be typed at compile time, and Kysely offers escape
hatches for these situations. See the sql template tag
and the DynamicModule for more info.
All API documentation is written in the typing files and you can simply hover over the module, class
or method you're using to see it in your IDE. The same documentation is also hosted here.
If you start using Kysely and can't find something you'd want to use, please open an issue or join our
discord server.
Getting started
Please visit our documentation site kysely.dev to get started. We also have a comprehensive
API documentation hosted here but you can access the same
documentation in your IDE by hovering over a class/method/property/whatever.
Contributors
Want to contribute? Check out our contribution guidelines.