sqlite-data

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

SQLite Data

SQLite Data

SQLiteData provides type-safe SQLite access through Swift macros, simplifying database modeling and queries while handling CloudKit sync, migrations, and async patterns automatically.
SQLiteData通过Swift宏提供类型安全的SQLite访问,自动处理CloudKit同步、迁移和异步模式,简化数据库建模与查询操作。

Reference Loading Guide

参考文档加载指南

ALWAYS load reference files if there is even a small chance the content may be required. It's better to have the context than to miss a pattern or make a mistake.
ReferenceLoad When
Table ModelsDefining tables with
@Table
, setting up primary keys, columns, or enums
Queries - BasicsUsing
@FetchAll
,
@FetchOne
,
@Selection
, filtering, ordering, or joins
Queries - AdvancedUsing
@Fetch
with
FetchKeyRequest
, dynamic queries, recursive CTEs, or direct reads
WritesInserting, updating, upserting, deleting records, or managing transactions
Views - SwiftUIUsing
@FetchAll
/
@FetchOne
in SwiftUI views,
@Observable
models, or animations
Views - IntegrationUIKit integration, dynamic query loading, TCA integration, or
observe {}
MigrationsCreating database migrations with
DatabaseMigrator
or
#sql()
macro
CloudKit SyncSetting up CloudKit private database sync, sharing, or sync delegates
DependenciesInjecting database/sync engine via
@Dependency
, bootstrap patterns, or TCA integration
TestingSetting up test databases, seeding data, or writing assertions for SQLite code
Advanced - QueriesImplementing triggers, custom database functions, or full-text search (FTS5)
Advanced - OptimizationPerformance tuning, indexes, custom aggregates, JSON aggregation, or self-joins
Schema CompositionUsing
@Selection
column groups, single-table inheritance, or database views
只要有极小概率需要用到相关内容,就务必加载参考文件。 拥有上下文总比遗漏模式或犯错要好。
参考文档加载场景
表模型使用
@Table
定义表、设置主键、列或枚举时
查询 - 基础使用
@FetchAll
@FetchOne
@Selection
、过滤、排序或关联查询时
查询 - 进阶
@Fetch
FetchKeyRequest
配合使用、动态查询、递归CTE或直接读取时
写入操作插入、更新、插入或更新(upsert)、删除记录,或管理事务时
视图 - SwiftUI在SwiftUI视图中使用
@FetchAll
/
@FetchOne
@Observable
模型或动画时
视图 - 集成UIKit集成、动态查询加载、TCA集成或使用
observe {}
迁移使用
DatabaseMigrator
#sql()
宏创建数据库迁移时
CloudKit同步设置CloudKit私有数据库同步、共享或同步代理时
依赖注入通过
@Dependency
注入数据库/同步引擎、引导模式或TCA集成时
测试设置测试数据库、填充测试数据或为SQLite代码编写断言时
进阶 - 查询实现触发器、自定义数据库函数或全文搜索(FTS5)时
进阶 - 优化性能调优、索引、自定义聚合、JSON聚合或自关联时
Schema组合使用
@Selection
列组、单表继承或数据库视图时

Core Workflow

核心工作流

When working with SQLiteData:
  1. Define table models with
    @Table
    macro
  2. Use
    @FetchAll
    /
    @FetchOne
    property wrappers in views or
    @Observable
    models
  3. Access database via
    @Dependency(\.defaultDatabase)
  4. Perform writes in
    database.write { }
    transactions
  5. Set up migrations before first use
使用SQLiteData时的步骤:
  1. 使用
    @Table
    宏定义表模型
  2. 在视图或
    @Observable
    模型中使用
    @FetchAll
    /
    @FetchOne
    属性包装器
  3. 通过
    @Dependency(\.defaultDatabase)
    访问数据库
  4. database.write { }
    事务中执行写入操作
  5. 在首次使用前设置迁移

Common Mistakes

常见错误

  1. N+1 query patterns — Loading records one-by-one in a loop (e.g., fetching user then fetching all their posts separately) kills performance. Use joins or batch fetches instead.
  2. Missing migrations on schema changes — Modifying
    @Table
    without creating a migration causes crashes at runtime. Always create migrations for schema changes before deploying.
  3. Improper transaction handling — Long-running transactions outside of
    database.write { }
    block can cause deadlocks or data loss. Keep write blocks short and focused.
  4. Ignoring CloudKit sync delegates — Setting up CloudKit sync without implementing
    SyncDelegate
    means you miss error handling and conflict resolution. Implement all delegate methods for production.
  5. Over-fetching in SwiftUI views — Using
    @FetchAll
    without filtering/limiting can load thousands of records, freezing the UI. Use predicates, limits, and sorting to keep in-memory footprint small.
  1. N+1查询模式 —— 在循环中逐个加载记录(例如先获取用户再单独获取其所有帖子)会严重影响性能。应改用关联查询或批量获取。
  2. Schema变更时遗漏迁移 —— 修改
    @Table
    却未创建迁移会导致运行时崩溃。部署前务必为所有Schema变更创建迁移。
  3. 事务处理不当 —— 在
    database.write { }
    块外执行长时间运行的事务可能导致死锁或数据丢失。保持写入块简短且聚焦。
  4. 忽略CloudKit同步代理 —— 设置CloudKit同步却未实现
    SyncDelegate
    ,会错过错误处理和冲突解决。生产环境中务必实现所有代理方法。
  5. SwiftUI视图中过度获取数据 —— 使用
    @FetchAll
    却不进行过滤/限制可能加载数千条记录,导致UI冻结。请使用谓词、限制条件和排序来减少内存占用。