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
A type-safe and autocompletion-friendly typescript SQL query builder for node.js. Heavily inspired by
knex but not intended to be a clone.
Kysely's typings only allow you to use tables that are available in the database and refer to
columns of the tables that are joined to the query. The result type always only contains the selected
columns with correct types and aliases. This allows tools like vscode autocompletion to make your life
so much easier.
As you can see 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 a column pet_name
to the result row type. Kysely is also able to
infer colum names and types from selected subqueries, joined subqueries, with
statements and pretty much
anything you can think of. Typescript is always there for you to tell what kind of query you can build and
offer completions.
Of course there are cases where things cannot be typed at compile time, and Kysely offers escape
hatches for these situations. With typescript you can always cast something to any
if the types
fail you. with Kysely you can also explicitly tell it to ignore the typings, but the default is always
type safety!
All you need to do is define an interface for each table in the database and pass those
interfaces to the Kysely
constructor:
import { Kysely } from 'kysely'
interface Person {
id: number
first_name: string
last_name: string
gender: 'male' | 'female' | 'other'
}
interface Pet {
id: string
name: string
owner_id: number
species: 'dog' | 'cat'
}
interface Movie {
id: string
stars: number
}
interface Database {
person: Person
pet: Pet
movie: Movie
}
const db = new Kysely<Database>({
dialect: 'postgres',
host: 'localhost',
database: 'kysely_test',
})
async function demo() {
const [person] = await db
.query('person')
.innerJoin('pet', 'pet.owner_id', 'person.id')
.select(['first_name', 'pet.name as pet_name'])
.where('person.id', '=', 1)
.execute()
person.pet_name
}
Work in progress
This whole library is still just a proof of concept and you can't yet start using it for anything
serious. Only a small subset of postgres dialect is implemented.
However, I'd say the concept is pretty much proven! Typescript is amazing! Let me know if this is something
you'd use and I'll continue working on this.
Why not just contribute to knex
Kysely is very similar to knex, but it also attempts to fix things that I personally find not-so-good
in knex. Bringing the type system and the changes to knex would mean very significantly breaking the
backwards compatibility. That's not possible at this point of the project. Knex was also originally
written for javascript and the typescript typings were added afterwards. That always leads to
compromises in the types. Designing a library for typescript from the ground up produces much
better and simpler types.