Scripts2026年7月2日·1 分钟阅读

SQLDelight — Type-Safe SQL for Kotlin Multiplatform

SQLDelight generates type-safe Kotlin APIs from SQL statements, enabling compile-time verified database queries across Android, iOS, JVM, and JavaScript targets.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
SQLDelight Overview
直接安装命令
npx -y tokrepo@latest install 5772c14d-75f0-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

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.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产