kysely-schema

Schema-first development tool for Kysely


Keywords
kysely, schema, migration, database, typescript, sql
License
MIT
Install
npm install kysely-schema@0.1.5

Documentation

kysely-schema

Schema-first development tool for Kysely — Prisma's DX + Kysely's power + Zero runtime overhead

MIT License

⚠️ Development Status

This package is in early development and undergoing active testing. Expect breaking changes between minor versions. Not recommended for production use yet.

Features

  • 📝 Schema-first — Define your database schema in TypeScript
  • 🔄 Auto-migrations — Generate Kysely migration files from schema changes
  • 🎯 Type-safe — Auto-generate TypeScript types for your Kysely queries
  • 🪶 Lightweight — Zero runtime overhead, pure code generation
  • 💪 Full SQL control — Generates standard Kysely schema builder code
  • 🔌 Works with Kysely — Complements existing Kysely migrations, doesn't replace them

Quick Start

# Install
npm install -D kysely-schema-cli


# Initialize (installs kysely + db driver for you)
npx kysely-schema init
# or
npx kys init 

Define your schema in schema/index.ts:

import { defineSchema, table, column } from 'kysely-schema';

export default defineSchema({
  user: table({
    id: column.serial().primaryKey(),
    email: column.text().notNull().unique(),
    name: column.text().nullable(),
    createdAt: column.timestamp().default('now()').notNull(),
  }),

  post: table({
    id: column.serial().primaryKey(),
    title: column.text().notNull(),
    content: column.text().nullable(),
    authorId: column.integer().notNull().references('user', 'id').onDelete('cascade'),
    createdAt: column.timestamp().default('now()').notNull(),
  }),
});

Generate migrations and types:

# Generate a Kysely migration
npx kysely-schema generate-migration "initial"
# or
npx kys gm "initial"

# Generate TypeScript types
npx kysely-schema generate-types
# or
npx kys gt

# Run migration (standard Kysely)
npx kysely migrate:latest

CLI Commands

Command Alias Description
init Scaffold config, schema, migrations, and generated dirs
generate-migration <name> gm Generate a Kysely migration file from your schema
generate-types gt Generate TypeScript Database interface
diff Show schema changes since last migration
validate Validate your schema definition
dev Watch schema, auto-regenerate types, validate, and show diffs

Comparison

Feature Prisma kysely-codegen kysely-schema
Schema-first
Lightweight
Full SQL control
Auto-migrations
Type generation
Runtime overhead Heavy None None

What It Does NOT Do

  • Replace Kysely's migration runner — it generates files for it
  • Add runtime overhead — pure code generation
  • Limit SQL capabilities — generates standard Kysely code
  • Introspect databases — schema-first, not DB-first

Documentation

Contributing

This project is open source and we welcome contributions!

License

MIT