DEV Community

Alex Spinov
Alex Spinov

Posted on

Kysely Has a Free API You Should Know About

Kysely is a type-safe SQL query builder for TypeScript. It gives you raw SQL power with full TypeScript inference — every column, every join, every subquery is type-checked.

Why Kysely Over ORMs

A team using Prisma hit its limitations — complex joins, CTEs, and window functions weren't supported. They didn't want to drop to raw SQL strings. Kysely gives them SQL expressiveness with TypeScript safety.

Key Features:

  • Type-Safe SQL — Every query fully typed
  • No Code Generation — Types from database schema
  • Raw SQL Power — CTEs, window functions, subqueries
  • Multiple Dialects — PostgreSQL, MySQL, SQLite
  • Migrations — Built-in migration system

Quick Start

npm install kysely
Enter fullscreen mode Exit fullscreen mode
import { Kysely, PostgresDialect } from "kysely"

interface Database {
  users: { id: number; name: string; email: string }
  posts: { id: number; title: string; user_id: number }
}

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

// Fully typed!
const users = await db
  .selectFrom("users")
  .select(["id", "name", "email"])
  .where("name", "like", "%Alice%")
  .orderBy("name")
  .execute()
Enter fullscreen mode Exit fullscreen mode

Joins

const postsWithAuthors = await db
  .selectFrom("posts")
  .innerJoin("users", "users.id", "posts.user_id")
  .select(["posts.title", "users.name as author"])
  .execute()
Enter fullscreen mode Exit fullscreen mode

Why Choose Kysely

  1. SQL power — no ORM limitations
  2. Type safety — catch errors at compile time
  3. No codegen — types from your schema definition

Check out Kysely docs to get started.


Need database solutions? Check out my Apify actors or email spinov001@gmail.com for custom solutions.

Top comments (0)