goodevibes avatar

sqlite-data

Use when working with SQLiteData library (@Table, @FetchAll, @FetchOne macros) for SQLite persistenc

作者 goodevibes|オープンソース

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.

Overview

Quick Reference

ReferenceLoad When
Table ModelsDefining database tables with @Table macro, setting up primary keys, columns, or enums
QueriesUsing @FetchAll, @FetchOne, @Fetch property wrappers, or building queries with joins/filters
WritesInserting, updating, upserting, or deleting records; managing transactions
ViewsIntegrating @FetchAll/@FetchOne with SwiftUI views, @Observable models, UIKit, or TCA @ObservableState
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
AdvancedImplementing triggers, full-text search (FTS5), or custom database functions
Schema CompositionUsing @Selection column groups, single-table inheritance, or database views

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

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.