Menu

Kysely for Beginners: Type-Safe SQL Without an ORM
📰

Kysely for Beginners: Type-Safe SQL Without an ORM

DEV Community: backend·Arhan Khan·16 days ago
#VwysFyLL
#devtobackend#devto#dev#kysely#query
Long article (299 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.