Kysely: A Type-Safe SQL Query Builder for TypeScript
Kysely is a type-safe SQL query builder for TypeScript, designed to eliminate entire classes of errors and provide developers with a peaceful night's sleep. By using Kysely, developers can write type-safe SQL queries, which not only reduces the amount of errors but also makes it easier to reason about performance.
Kysely is a light abstraction layer over SQL, which makes it easy to learn and use. It also exposes the database schema to the TypeScript compiler, providing developers with autocompletion on table names, column names, aliases, and more. With multi-dialect support, Kysely can be used with PostgreSQL, MySQL, or SQLite, and there is a growing ecosystem of 3rd party dialects, including PlanetScale, D3, SurrealDB, and more.
Kysely runs on every environment, including node.js, the browser, serverless and edge environments, and even on Deno. It also comes with a plugin system that lets developers extend the core with their own functionality.
Developers are loving Kysely for its simplicity and power. According to Dax Raad, a member of the SST_dev core team, "Kysely is the best written TS codebase I've ever seen." Harminder Virk, the creator of AdonisJS, also praised Kysely, saying "Kysely is great. Keeping an eye on it to see how it evolves in coming months."
Here's an example of how to use Kysely to build a type-safe SQL query:
import { createQuery } from 'kysely';
const query = createQuery()
.select('id', 'name', 'email')
.from('users')
.where('id', '=', 1)
.orWhere('name', 'like', '%John%')
.limit(10)
.offset(20)
.build();
console.log(query);
// SELECT "id", "name", "email" FROM "users" WHERE "id" = $1 OR "name" LIKE $2 LIMIT 10 OFFSET 20
In the example above, the createQuery
function is used to create a new query object. The select
method is used to specify the columns to select, the from
method is used to specify the table to select from, and the where
and orWhere
methods are used to specify the conditions of the query. Finally, the limit
and offset
methods are used to specify the pagination of the query.
In conclusion, Kysely is a powerful tool for developers who want to write type-safe SQL queries in TypeScript. With its simplicity, multi-dialect support, and plugin system, Kysely is a great choice for any project.