SPB Git

Repository Public License

0002-sqlite-without-dependencies.md 4.0 KB

# ADR 0002 — Storage on the system SQLite, without a third-party library

Status: accepted (2026-11-25). Module: Persistence.

# Context

ORIX Navigator stores sessions (windows, tabs), history, bookmarks, downloads, permissions, notes, canvases, agent runs, workflows, sync state and settings. The requirements that shaped the choice:

  • the window/tab state must survive a crash, and saving it must be cheap enough to do continuously;
  • the omnibox needs full-text search and ranked suggestions over about 100 000 history URLs on every keystroke;
  • nothing may run on the main actor, and the code must be Swift 6 strict-concurrency clean on macOS and iOS;
  • the project rule is no third-party dependency (supply chain, review surface, long-term maintenance).

Options considered:

Option Why not
SwiftData / Core Data No FTS5 or custom ranking query, opaque schema and migrations, ModelContext is not Sendable, weak control over when a commit reaches the disk.
GRDB An excellent library, but a third-party dependency (≈ 60 k lines to audit and track) for a layer whose needed subset is small. Rejected on purpose.
Files (JSON / property lists) No atomic multi-entity update, no index, no search; a crash during a rewrite loses a session.
System SQLite (import SQLite3) Chosen.

# Decision

Use the SQLite that ships with the OS through its C API, wrapped by a thin layer written for this project (Sources/Persistence/SQLite, about 650 lines):

  • SQLiteConnection — one handle, a prepared-statement cache, typed binding (SQLiteConvertible) and typed reading (SQLiteRow), transaction { } with savepoints for nesting, errors carrying SQLite's code, extended code and message (SQLiteError). Not Sendable: it never leaves its actor.
  • ConnectionActor — an actor whose executor is a private serial DispatchQueue. Blocking SQLite calls run on that queue, not on the Swift cooperative pool and never on the main thread.
  • SQLiteDatabase — one writer plus a pool of read-only connections (three by default) for file databases, in WAL mode, so readers and the writer do not block each other. An in-memory database has a single connection.
  • Migrator + Schema.migrations — ordered, append-only migrations, each in its own transaction, version in PRAGMA user_version.
  • Typed stores on top (NavigatorDatabase is the façade); no SQL leaks out of the module.

Features used from the system SQLite: WAL, foreign keys, FTS5 (unicode61 tokenizer with diacritic folding and prefix indexes), JSON1 (json_group_array), recursive CTEs, UPSERT, WITHOUT ROWID, partial indexes. All of them are present in the SQLite of macOS 15 and iOS 18, the minimum deployment targets.

# Consequences

Good:

  • No dependency; the whole storage layer is reviewable in this repository.
  • Full control over durability (one committed WAL transaction per session save), over the schema, and over the queries that matter (frecency ranking is one indexed SQL statement).
  • FTS5 and JSON1 for free; the database file can be inspected with the sqlite3 command-line tool.

Costs we accept:

  • We own the unsafe C boundary (pointer lifetimes, statement finalisation). It is confined to one file and covered by tests (round trips, rollback, cache eviction, concurrency, corruption).
  • Row ⇄ model mapping is written by hand (Stores/Records.swift). Adding a model field means: a migration, one line in init(row:), one in save(_:).
  • The SQLite version is the OS's. A feature newer than the minimum OS must not be used without a fallback.
  • No encryption at rest at the SQLite level (SQLCipher would be a dependency). Protection is the platform's: iOS data protection and FileVault. See docs/PERSISTENCE.md, "Encryption at rest".

# Revisit if

  • sync (Bridge) needs change feeds or CRDT storage that outgrow a hand-written layer;
  • a requirement for per-database encryption appears (then: SQLCipher, or encrypting sensitive columns with a Keychain-held key).
Files main
Menu

Theme