Type safe SQL query builder
Kysely is a TypeScript-first SQL query builder that provides compile-time type safety and autocompletion for database queries. Unlike traditional ORMs that abstract away SQL, Kysely embraces SQL syntax while layering TypeScript's type system on top. It compiles directly to raw SQL with a 1:1 mapping, meaning developers write queries that look like SQL but get full IDE support and type checking.
The package was created to solve the friction between type-safe TypeScript development and traditional query builders like Knex.js that lack native type inference. With 2.4 million weekly downloads, Kysely has become the go-to solution for teams that want SQL control without sacrificing TypeScript benefits. It supports PostgreSQL, MySQL, SQLite, and MS SQL Server through official dialects, with zero runtime dependencies and compatibility across Node.js, Deno, Bun, Cloudflare Workers, and browsers.
Kysely infers result types based on selected columns, handles complex joins and subqueries with proper type narrowing, and catches schema mismatches at compile time. Developers typically use kysely-codegen to automatically generate TypeScript interfaces from existing database schemas, keeping type definitions synchronized with actual database structure. This approach eliminates entire classes of runtime errors related to column names, data types, and query structure.
The library targets teams that need explicit SQL control—those building high-performance backends, working with complex queries, or migrating from raw SQL to type-safe code. It's particularly valuable in large codebases where refactoring database schemas requires confidence that all affected queries are updated correctly.
import { Kysely, PostgresDialect } from 'kysely';
import { Pool } from 'pg';
// Define your database schema types
interface Database {
users: {
id: number;
email: string;
created_at: Date;
};
posts: {
id: number;
user_id: number;
title: string;
content: string;
published: boolean;
};
}
const db = new Kysely<Database>({
dialect: new PostgresDialect({
pool: new Pool({
host: 'localhost',
database: 'myapp',
}),
}),
});
// Type-safe query with join and filtering
const publishedPosts = await db
.selectFrom('posts')
.innerJoin('users', 'users.id', 'posts.user_id')
.select([
'posts.id',
'posts.title',
'posts.content',
'users.email as author_email',
])
.where('posts.published', '=', true)
.orderBy('posts.created_at', 'desc')
.limit(10)
.execute();
// Result type is inferred:
// Array<{ id: number; title: string; content: string; author_email: string }>
// Insert with type checking
await db
.insertInto('posts')
.values({
user_id: 1,
title: 'Getting Started with Kysely',
content: 'Kysely makes SQL type-safe...',
published: true,
})
.execute();
// Update with conditional logic
await db
.updateTable('posts')
.set({ published: true })
.where('user_id', '=', 1)
.where('created_at', '>', new Date('2024-01-01'))
.execute();Building REST APIs with complex joins: When constructing APIs that require multiple table joins with filtering and pagination, Kysely provides type-safe query composition where join conditions and selected columns are validated at compile time, preventing runtime errors from typos or schema changes.
Migrating legacy SQL codebases to TypeScript: Teams with existing SQL-heavy applications can adopt Kysely incrementally, wrapping raw SQL in type-safe builders without rewriting business logic or learning ORM conventions, while gaining immediate feedback on schema mismatches.
Multi-tenant SaaS applications: Applications requiring dynamic query construction based on tenant permissions or feature flags benefit from Kysely's composable query building, where conditional joins and filters maintain type safety even when query structure varies at runtime.
Data analytics and reporting dashboards: Complex aggregations, window functions, and CTEs (Common Table Expressions) needed for analytics queries are fully supported with proper type inference, allowing developers to build sophisticated reports while catching errors before deployment.
Microservices with shared database types: In monorepo architectures where multiple services access the same database, Kysely's generated types can be shared across services, ensuring consistent database interaction patterns and catching breaking changes during schema evolution.
Drizzle ORM package for SQL databases
Prisma is an open-source database toolkit. It includes a JavaScript/TypeScript ORM for Node.js, migrations and a modern GUI to view and edit the data in your database. You can use Prisma in new projects or add it to an existing one.
npm install kyselypnpm add kyselybun add kysely