Introduction
SQLDelight turns your SQL statements into type-safe Kotlin APIs. Instead of writing an ORM abstraction, you write real SQL and let the compiler generate the data classes and query functions, catching errors at build time rather than runtime.
What SQLDelight Does
- Generates type-safe Kotlin code from plain SQL statements in .sq files
- Validates SQL syntax and schema at compile time
- Supports Kotlin Multiplatform: Android, iOS, JVM, JS, and Native targets
- Provides coroutines-based reactive queries that re-emit on data changes
- Handles database migrations with versioned .sqm files
Architecture Overview
SQLDelight operates as a Gradle plugin that parses .sq files containing SQL statements and schema definitions. It resolves types against the declared schema, generates Kotlin data classes matching query result columns, and produces a database interface with suspend functions. At runtime, a platform-specific driver (AndroidSqliteDriver, NativeSqliteDriver, or JdbcSqliteDriver) connects the generated code to the actual database engine.
Self-Hosting & Configuration
- Add the Gradle plugin to your build configuration
- Create a sqldelight block specifying the database name and package
- Place .sq files in src/commonMain/sqldelight for shared queries
- Configure platform-specific drivers in each target's source set
- Use the generateSqlDelightInterface Gradle task to produce code
Key Features
- SQL-first approach: write real SQL, not a DSL or annotations
- Compile-time schema validation catches typos and type mismatches before runtime
- Multiplatform support with a single set of SQL files shared across targets
- Reactive queries via Flow integration for automatic UI updates
- Built-in migration tooling with verification and testing support
Comparison with Similar Tools
- Room — Android-only annotation processor; SQLDelight is multiplatform and SQL-first
- Exposed — Kotlin DSL that abstracts SQL; SQLDelight keeps you writing raw SQL with type safety
- Realm — object-oriented mobile database; SQLDelight uses standard SQLite with generated code
- JOOQ — JVM-only with a Java-centric DSL; SQLDelight targets Kotlin Multiplatform
- Drift (Moor) — Dart/Flutter equivalent; SQLDelight serves the Kotlin ecosystem
FAQ
Q: Which databases does SQLDelight support? A: SQLDelight supports SQLite (all platforms), MySQL (JVM), PostgreSQL (JVM), and HSQL/H2 (JVM). SQLite is the primary target for multiplatform projects.
Q: Can I use SQLDelight with existing databases? A: Yes. Point SQLDelight at your existing schema and it generates code for your queries. Migration files handle schema evolution.
Q: How does SQLDelight handle migrations? A: Numbered .sqm files contain ALTER TABLE and other DDL statements. SQLDelight verifies that each migration produces the expected schema at compile time.
Q: Does SQLDelight work with Jetpack Compose? A: Yes. SQLDelight queries return Kotlin Flow objects that integrate directly with Compose state collection via collectAsState.