DEV Community

Arhan Khan
Arhan Khan

Posted on

Kysely for Beginners: Type-Safe SQL Without an ORM

ORMs promise to hide SQL from you, but they often end up hiding the database itself — and then you fight the ORM instead of writing queries. Kysely takes the opposite approach: it is a type-safe SQL query builder. You write real SQL, in TypeScript, with full autocomplete and compile-time checks.

Here is a simple query:

import { Kysely, PostgresDialect } from 'kysely';

interface Database {
  users: { id: number; name: string; email: string; created_at: Date };
  posts: { id: number; author_id: number; title: string };
}

const db = new Kysely<Database>({ dialect: new PostgresDialect({ pool }) });

const activeUsers = await db
  .selectFrom('users')
  .select(['id', 'name', 'email'])
  .where('created_at', '>', new Date('2026-01-01'))
  .orderBy('created_at', 'desc')
  .limit(10)
  .execute();
Enter fullscreen mode Exit fullscreen mode

If you mistype a column name, TypeScript tells you immediately. If you select a column that does not exist, the code does not compile. And the result type is inferred automatically — no manual interfaces for every query result.

Kysely supports PostgreSQL, MySQL, and SQLite, plus joins, transactions, migrations, and raw SQL when you need an escape hatch. It is deliberately small: no models, no magic, no hidden queries. What you write is what runs.

Kysely is for TypeScript developers who know SQL and want the safety of types without giving up control of their queries. If you have ever been burned by an ORM generating a terrible query, Kysely is worth a serious look.

Top comments (0)