Quick answer
Kysely is a type-safe SQL query builder for TypeScript: you build queries with method calls instead of raw SQL strings, and the compiler checks table and column names against a type you define. It is not an ORM: no models, no hidden queries, and the SQL it generates is the SQL you asked for. This post builds a small shop orders database with Kysely and SQLite, runs real totals queries, and fixes the undefined result that catches beginners.
A shop in Karachi keeps its orders in a spreadsheet. Six orders, three cities, one question at the end of the day: which city sold the most? The spreadsheet answers it, but only after filtering, copying, and trusting nobody typed a total into the wrong column. Raw SQL fixes the arithmetic, but one missing quote in a template string breaks the query at runtime. An ORM fixes the strings, then hides the SQL you need when a query runs slowly. Kysely sits in the middle: real SQL, built with TypeScript, checked by the compiler before anything runs. This post builds that shop database and runs the numbers.
What I would check before using it
My first check is whether I actually like writing SQL. Kysely does not hide SQL, it organises it: the query here still says selectFrom, groupBy, and orderBy. If writing SQL feels like the problem, the Prisma Explained: Databases Without Writing Raw SQL post covers an ORM that writes it for you. If the strings are the problem, Kysely is the right shape. My judgement: pick Kysely when you want to read the query you run.
My second check is how much of the project is TypeScript. Kysely's whole value is type checking: a wrong column name fails at compile time, not late at night in production. In plain JavaScript that check disappears, leaving a query builder with nice syntax and no safety net. The Drizzle ORM: Your Database Schema, Written in TypeScript post covers a similar TypeScript-first approach, worth comparing before committing. In a TypeScript codebase the types come free; elsewhere, Kysely alone is no reason to switch.
My third check is who owns migrations. Kysely builds tables and runs migrations through code you write, with no built-in command to scaffold one. A team that wants hand-holding should pick a tool that generates migrations. The SQLite Explained: The Database That Lives Inside Your App post covers the database in this demo, and the PostgreSQL Explained: The Reliable Database You Default To post covers the one most teams graduate to.
My verdict, as judgement: use Kysely when the team writes TypeScript, reads SQL, and wants the compiler to catch typos. The recommendation flips if the team wants SQL hidden, because then an ORM fits better, or if nobody wants to own migrations, because then the manual approach becomes a chore.
Six shop orders, one query builder, and the result that was not there
The demo is a shop orders database: six orders from 20 to 24 November 2026, Sana and Hamza in Karachi, Daniyal and Ayesha in Lahore, Mahnoor and Bilal in Islamabad. It runs on Kysely 0.29.9 with better-sqlite3, TypeScript 7.0.2, and tsx 4.23.15 on Node 24.22.1.
$ npm install kysely better-sqlite3 tsx $ npm install -D typescript @types/better-sqlite3 The full script, shop-orders.ts. The id column uses Generated, so inserts skip it, and everything runs inside one async main function, because tsx refuses top-level await in a CommonJS project.
import { Generated, Kysely, SqliteDialect, sql } from 'kysely' import Database from 'better-sqlite3' interface OrderTable { id: Generated<number> customer: string city: string total: number ordered_at: string } interface DB { orders: OrderTable } const db = new Kysely<DB>({ dialect: new SqliteDialect({ database: new Database('shop.db') }), }) const orders = [ { customer: 'Sana', city: 'Karachi', total: 9800, ordered_at: '2026-11-20' }, { customer: 'Daniyal', city: 'Lahore', total: 13400, ordered_at: '2026-11-21' }, { customer: 'Mahnoor', city: 'Islamabad', total: 11200, ordered_at: '2026-11-22' }, { customer: 'Hamza', city: 'Karachi', total: 6750, ordered_at: '2026-11-23' }, { customer: 'Ayesha', city: 'Lahore', total: 8900, ordered_at: '2026-11-24' }, { customer: 'Bilal', city: 'Islamabad', total: 7200, ordered_at: '2026-11-24' }, ] async function main() { await db.schema .createTable('orders') .addColumn('id', 'integer', (col) => col.primaryKey().autoIncrement()) .addColumn('customer', 'text', (col) => col.notNull()) .addColumn('city', 'text', (col) => col.notNull()) .addColumn('total', 'integer', (col) => col.notNull()) .addColumn('ordered_at', 'text', (col) => col.notNull()) .execute() await db.insertInto('orders').values(orders).execute() console.log('Inserted 6 orders.') const totals = await db .selectFrom('orders') .select(['city', sql<number>`sum(total)`.as('city_total')]) .groupBy('city') .orderBy('city_total', 'desc') .execute() console.log('Totals per city:', totals) const top = await db .selectFrom('orders') .select(['city', sql<number>`sum(total)`.as('city_total')]) .groupBy('city') .orderBy('city_total', 'desc') .executeTakeFirstOrThrow() console.log('Top city:', top.city, 'Rs', top.city_total) const big = await db .selectFrom('orders') .select(['customer', 'city', 'total']) .where('total', '>', 10000) .orderBy('total', 'desc') .execute() console.log('Orders over Rs 10,000:', big) const multan = await db .selectFrom('orders') .select(['city', sql<number>`sum(total)`.as('city_total')]) .where('city', '=', 'Multan') .groupBy('city') .executeTakeFirst() if (multan === undefined) { console.log('No orders in Multan yet.') } else { console.log('Top city in Multan: Rs', multan.city_total) } await db.destroy() } main() Run it from a fresh database:
$ rm -f shop.db && npx tsx shop-orders.ts Inserted 6 orders. Totals per city: [ { city: 'Lahore', city_total: 22300 }, { city: 'Islamabad', city_total: 18400 }, { city: 'Karachi', city_total: 16550 } ] Top city: Lahore Rs 22300 Orders over Rs 10,000: [ { customer: 'Daniyal', city: 'Lahore', total: 13400 }, { customer: 'Mahnoor', city: 'Islamabad', total: 11200 } ] No orders in Multan yet. The totals check out: Lahore Rs 22,300, Islamabad Rs 18,400, Karachi Rs 16,550, grand total Rs 57,250. Daniyal's Rs 13,400 order in Lahore and Mahnoor's Rs 11,200 in Islamabad are the only two above Rs 10,000. The totals query groups rows and sums them; the SQL Joins Explained: INNER, LEFT, RIGHT, FULL Examples post covers the join types for two-table queries.
The first version crashed. The totals queries worked, then the Multan lookup found no rows, because no order came from Multan, and the next line read a property off that empty result:
$ npx tsx shop-orders.ts Orders over Rs 10,000: [ { customer: 'Daniyal', city: 'Lahore', total: 13400 }, { customer: 'Mahnoor', city: 'Islamabad', total: 11200 } ] shop-orders.ts:72 console.log('Top city in Multan: Rs', multan.city_total) ^ TypeError: Cannot read properties of undefined (reading 'city_total') executeTakeFirst() returns undefined when a query finds no rows, so reading a property throws. TypeScript's strict checks would have caught the read at compile time, with error TS18048, but tsx runs without type checking, so the crash arrived at runtime. The fix is a plain undefined check, and the fixed run ends with "No orders in Multan yet." instead of a stack trace.
Common mistakes and how to fix them
First, reading a property off an executeTakeFirst() result unchecked. The symptom is TypeError: Cannot read properties of undefined (reading 'city_total') when a query matches nothing. The fix is an undefined check before reading the result, or executeTakeFirstOrThrow() when a row must exist.
Second, leaving id as a plain number in the table type. The symptom is compile error TS2345, saying Property 'id' is missing when you insert a row without one. The fix is id: Generated, which tells Kysely the database fills it in.
Third, skipping the better-sqlite3 type declarations. The symptom is TS7016: Could not find a declaration file for module 'better-sqlite3'. The fix is npm install -D @types/better-sqlite3, the command used in this demo.
Fourth, writing top-level await in a CommonJS project. The symptom is a tsx transform error: Top-level await is currently not supported with the 'cjs' output format. The fix is an async main function, as this demo does, or "type": "module" in package.json.
Frequently Asked Questions
Is Kysely an ORM? No. Kysely builds SQL queries and maps rows to types, with no models, no relations API, and no change tracking. If you want objects that save themselves, pick an ORM; if you want readable SQL, pick Kysely.
How is Kysely different from Prisma? Prisma generates a client from a schema file, hides the SQL, and ships migrations. Kysely keeps the SQL visible and checks it against types you write. Prisma suits teams that want SQL hidden; Kysely suits teams that want it readable.
Does Kysely work with PostgreSQL and MySQL? Yes. Kysely has dialects for PostgreSQL, MySQL, SQLite, and others, and the same builder code works across them while the SQL stays portable. This demo uses SQLite because it needs no server.
Can I use Kysely without TypeScript? You can, but you lose the point. In plain JavaScript the queries still run, but nothing checks table or column names. In a JavaScript project, a plain query builder or an ORM is usually a better fit.
What to remember
- Kysely builds SQL with TypeScript method calls; it is a query builder, not an ORM.
- Table and column names are checked against a type you define, so typos fail early.
- executeTakeFirst() returns undefined when nothing matches, so check before reading properties.
- Mark autoincrement columns as Generated so inserts do not need an id.
- The totals prove the queries: Lahore Rs 22,300, Islamabad Rs 18,400, Karachi Rs 16,550, grand total Rs 57,250.
Where this leaves you
Kysely earns its place the first time a typo in a column name fails in the editor instead of in production at night. The demo showed it: one table, six real orders, three queries, totals adding to Rs 57,250. The only crash came from reading a result that was not there, and the fix was a short check, not a new library. If your project is TypeScript and your team reads SQL, try Kysely on one small feature, a report or an admin page, and see whether the compiler catches something first.
Further reading
- Kysely documentation
- Kysely on GitHub
- Prisma Explained: Databases Without Writing Raw SQL
- Drizzle ORM: Your Database Schema, Written in TypeScript
- SQLite Explained: The Database That Lives Inside Your App
- SQL Joins Explained: INNER, LEFT, RIGHT, FULL Examples
Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.
