Zum Hauptinhalt springen
info
Dieser Teil ist für Entwickler und nur auf Englisch verfügbar.

On-device database

The mobile app keeps a SQLite database on the phone: a mirror of the server library, the camera roll, the sync and upload queues and the offline outbox. The desktop app keeps the same database for the folders the user added. Both use one package for it, packages/local-db (@librephotos/local-db), so the schema, the SQL and the change handling are written once and tested once in Node.

The package owns:

PartWhere
Drizzle schema and row typessrc/schema.ts, src/types.ts
Migrationsmigrations/*.sql, migrations/migrations.json, src/migrate.ts
Queriessrc/queries/ (timeline, albums, people, search, memories, counts, the desktop folder library, the upload queue, ...)
Writers for the server mirrorsrc/writers.ts
Atomic writessrc/batch.ts (runBatch), runWrite in src/database.ts
The change hubsrc/change-hub.ts
Logout and resetsrc/reset.ts (clearMirror, clearEntity, wipeLocalData)
Node adapter and the test database@librephotos/local-db/better-sqlite3, @librephotos/local-db/testing

The platform adapters are separate entry points, so importing the package never pulls in a native module by accident. Like packages/api-client, the package is TypeScript source that Metro, jest, vitest and Vite compile directly; there is no build step.

Platform code stays in the apps. An app provides exactly one thing: an executor.

The executor contract​

SqlExecutor (src/executor.ts) is the one seam between the shared code and a platform's SQLite. Every runtime implements it and nothing else: expo-sqlite on the phone, rusqlite behind Tauri commands on the desktop, better-sqlite3 in Node tests and tools. Keep it this small: every method is one more thing the Rust side has to implement and keep in step.

interface SqlExecutor {
exec(sql: string, params: SqlValue[], method: "run" | "all" | "get" | "values"): Promise<SqlResult>;
batch(statements: { sql: string; params: SqlValue[]; method: SqlMethod }[]): Promise<SqlResult[]>;
}
  • exec runs one statement. Parameters are positional (?) and already normalised to a SqlValue (string, number, bigint, Uint8Array or null; booleans arrive as 0 or 1, dates as numbers).
  • method says what the caller wants back, in Drizzle sqlite-proxy's shapes:
    • "run": { rows: [] }, plus changes and lastInsertRowId when the driver reports them;
    • "all" and "values": { rows: Row[] }, every result row;
    • "get": { rows: Row | undefined }, the first row. The statement still steps to the end, because an INSERT ... RETURNING writes a row for every step.
  • A row is a positional array, never an object. Positional rows keep duplicate column names apart and are what Drizzle's query builder maps.
  • columns, the statement's column names in result order, is required for "all" and "get". Every query in the package is raw SQL whose rows are read by name, and only the engine knows the names (sqlite3_column_name, rusqlite's Statement::column_names()).
  • A statement with RETURNING is sent as "all" or "get": it writes and returns rows.
  • batch runs its statements in order inside one transaction and returns one result per statement. It is all or nothing: if a statement fails, everything the batch did is rolled back and the promise rejects. No other exec or batch call may run on the connection in between.

createSerialQueue() runs async tasks one at a time, in call order; a failed task does not stop the queue. An executor over a single async connection (the phone's) wraps every exec and batch in it, which is what keeps one caller's BEGIN ... COMMIT from interleaving with another caller's statements. On the desktop a Mutex<Connection> does the same.

RuntimeExecutorWhere
Node (tests, tools, scripts)createBetterSqliteExecutor(db)@librephotos/local-db/better-sqlite3
PhonecreateExpoExecutor(sqlite)apps/mobile/src/db/client.ts
Desktopan executor over the Tauri commands db_exec and db_batch (rusqlite)the desktop app

The better-sqlite3 adapter takes an open Database instead of opening one, so the package never loads the native addon itself (better-sqlite3 is an optional peer dependency). Its connection is synchronous, so every call is atomic and isolated on its own, and a batch is one better-sqlite3 transaction. It caches prepared statements by SQL text in a small LRU, because a seed batch repeats one INSERT a thousand times.

Async everywhere, and batches instead of transactions​

import { createDatabase, runMigrations } from "@librephotos/local-db";

await runMigrations(executor);
const db = createDatabase(executor);
const page = await timelinePage(db, { limit: 120 });

createDatabase is cheap (no I/O) and returns an AppDatabase: Drizzle's async SQLite database (drizzle-orm/sqlite-proxy) over the executor. Every query module and writer takes one. Two kinds of reads go through it and want different row shapes:

  • raw SQL (db.all<Row>(sql`...`), db.get<Row>(...)), which is how every query in the package is written, wants objects keyed by column name;
  • Drizzle's query builder (db.select().from(t)) maps positional rows onto its fields itself.

sqlite-proxy hands both to the same callback and cannot tell them apart, so the callback passes the executor's whole result through and a prepared query that knows whether it has builder fields decodes it. The executor always answers positionally; objects are built once, in src/database.ts (rowsToObjects, a later duplicate column name wins). Raw items inside db.batch([...]) are decoded the same way.

Every call is a promise. Both the package and the mobile app lint with no-floating-promises, no-misused-promises and await-thenable: a forgotten await is a write that may not have happened yet, or a promise tested for truthiness.

There is deliberately no "begin a transaction and hand it back" call. An async executor shared by concurrent callers cannot hold a transaction open while other callers keep issuing statements. So:

  • Atomic writes are one batch: runBatch(db, statements) or db.batch([...]). db.transaction() rejects, and a lint rule (no-restricted-syntax) bans calling it in the package and the app. better-sqlite3's own synchronous sqlite.transaction stays allowed in Node code.
  • A read-then-write that must be atomic is one statement: UPDATE ... WHERE id = (SELECT ...) RETURNING .... claimNextJob, claimNextUpload and claimExistenceChecks pick a row and move it to running, uploading or checking in the statement that selects it, so a claim either wins outright or yields nothing, and two workers can never be handed the same row even though they interleave between statements.
  • A write that reports what it changed uses runWrite(db, query) or RETURNING, never a follow-up SELECT changes(): on a shared async connection another caller's statement can run in between, and the second query would read its count.
  • A batch cannot read between its statements, so every statement computes what it needs in SQL. The mobile app's offline actions (apps/mobile/src/mutations/actions.ts) recount with a subquery and pick a temporary album id as MIN(id) - 1. That id is chosen before the batch, so a concurrent create can pick the same one; the INSERT then fails on the primary key, nothing is written, and the action retries once.

Where the apps rely on this:

  • Offline mutations: each action writes the optimistic change to the mirror and inserts the zod-validated outbox row in one batch. If either fails both roll back, so the UI never shows a write that was not queued, and live queries re-fire on commit. Validation runs before anything is sent: a bad payload throws and nothing is written. The replay trigger fires after the write committed, so replay sees the row.
  • Noch nicht erschienen (kommt nach 1.7.0) Pending photo edits (src/queries/photo-edits.ts): a favorite, hide, trash, archive or rating action first copies the photo's five editable columns into photo_edit_base (capturePhotoEditBaseSql, INSERT OR IGNORE, so the copy is the server's value even when an earlier edit is still queued), in the same batch as the mirror write and the outbox row. While an outbox row of the photo is live, upsertRemotePhotosSql keeps every column such a row writes (photoEditKept) and writes the server's values into photo_edit_base instead (refreshPhotoEditBaseSql); the other columns are updated as usual. When a row is sent, settlePhotoEditsSql drops the copies no live row needs any more; when it is dropped, it first puts the copied values back into the columns that row wrote, unless a later live row writes them too. Without it, a pull that ran while the user edited overwrote the edit on the phone, and a refused edit stayed on the phone with no pull ever correcting it.
  • Noch nicht erschienen (kommt nach 1.7.0) Pending album and person edits (src/queries/album-edits.ts): the batch that writes a page of albums (upsertUserAlbumsSql) ends with reapplyAlbumEditsSql, which puts the live outbox rows back on top: the last add or remove of each photo, the last title and cover, and a deletion. upsertPersonsSql does the same for a new name. Clearing the mirror (Repair, a 410) keeps the albums made offline, which have negative ids and exist only on the phone until their album_create is sent. The integrity check counts albums with expectedServerAlbums: albums made offline are not expected on the server, and albums whose deletion is still queued may or may not be there. Before, a deletion waiting in its backoff made the counts differ, and the reseed brought the album back. verification/mobile-sync/AlbumEdits.tla checks this.
  • Noch nicht erschienen (kommt nach 1.7.0) The cached photo details (src/queries/detail.ts): the viewer shows remote_photo_detail, and the answer of the server only reaches the screen through it. fetched_at is the time the answer was asked for. putPhotoDetail ignores an answer asked for before the cached row was last written, and puts a caption that is still in the outbox on top of a newer one. Saving a caption and sending it (captionSentSql) both move fetched_at forward, so an answer that was on its way meanwhile can't bring the old caption back. An ignored answer is asked for again (fetchIntoCache in apps/mobile/src/features/viewer/usePhotoDetail.ts), because the rest of it, such as a new place, would otherwise stay off the screen until the details count as old. verification/mobile-sync/CaptionDetail.tla checks this.
  • Delta sync: the page applier (apps/mobile/src/sync/remote/applier.ts) writes a page's rows, its tombstones and the advanced resume cursor in one batch. A crash mid-page leaves the cursor where it was, and the page is fetched and applied again (the writers are idempotent). Sharing has no tombstones on the server, so that set is reconciled by a periodic reseed of its small feed.
  • Shared uploads land whole or not at all (enqueueSharedUploads).
  • Diagnostics, sync-log lines and upload progress are display only: they are written in order on the serial connection and never awaited, and a failure there is never fatal.

Writers​

src/writers.ts upserts server rows into the remote mirror with ON CONFLICT DO UPDATE on the server id, so a reseed or a pull-to-refresh converges without duplicates. There are two layers:

  • *Sql(rows) builds the statements and runs nothing. A caller that must write several things atomically (the page applier) concatenates them and runs them as one batch.
  • upsert*(db, rows) and delete*(db, ids) run them, each chunk of BATCH_SIZE (1,000) rows as its own batch, so a seed of 100,000 photos never holds one giant transaction and live queries show progress.

deleteOwnedRemotePhotosByHash drops the owner's photos with the given image hashes after the server deleted them for good. It matches the server, which deletes every row of the requester's that carries the hash (image_hash is not unique) and only theirs, so another user's shared photo with the same hash stays. Each statement selects by owner and hash itself, so the batch is atomic without reading ids first.

Mapping API responses onto the writers' inputs is the app's job (apps/mobile/src/sync/remote/mappers.ts, pig-photo.ts): the package does not depend on the API client. The seed's timeline list does not carry the media categories; the delta feed does. The delta feed's photo rows also carry thumbhash, the base64 ThumbHash the web grid shows until a thumbnail loads (null until the server has sampled it, sent to a library share's recipient like the thumbnail itself); the mirror does not store it yet. A new ThumbHash, from a rotation, an edit or the upgrade job that samples the missing ones, moves the photo's last_modified, so the feed sends the row again. is_favorite is materialised from rating >= favorite_min_rating when a row is mapped.

The schema​

src/schema.ts is the Drizzle schema and the single source drizzle-kit generates the SQL migrations from. Its tables fall into three groups:

  • remote mirror: flat summaries of server state (own and shared photos, albums, people, things, tags, places, shared users). Disposable: it can always be re-seeded from the API.
  • local media: what is on this device. On the phone that is the camera roll (local_album.source = 'device'); on the desktop the folders the user added (source = 'folder'), whose files are referenced in place. One set of tables serves both, so the timeline, hashing and backup code is the same.
  • app state: sync cursors, the offline outbox, the upload, job and thumbnail queues, logs.

Timestamps are integer milliseconds since the epoch (Drizzle mode: "number"), never Date, so they round-trip identically through every driver. Flags are integers with mode: "boolean" for the query builder; raw SQL sees 0 and 1.

Columns whose meaning is not in their name:

ColumnMeaning
local_asset.idPhone: the MediaLibrary asset id. Desktop: any stable id; the file path is used.
local_asset.type'image', 'video' or 'raw'.
local_asset.uriPhone: ph:// or content://. Desktop: the file's absolute path.
local_asset.hashThe server's image hash (MD5 of the bytes plus the user id); null until hashed.
local_asset.hash_state'icloud' (HASH_STATE_ICLOUD): the bytes are in iCloud, not on the phone. HASH_STATE_UNREADABLE: the desktop could not read the file, and it is skipped until it changes.
local_asset.created_atWhen the picture was taken (or else the file time): the timeline's sort key.
local_asset.bucket_day'YYYY-MM-DD' of created_at in the user's calendar, written by the app (the phone: the device time zone; the desktop: the picture's wall-clock date). Null falls back to SQLite's localtime. Groups the timeline by day and gives the search index its date words.
local_asset.folderDesktop only: the folder as the user sees it, the added folder's own name and then the subfolders ("Pictures/Holiday 2019/Day 1"). Searchable word by word.
local_asset.taken_atDesktop only: the date-taken text from the metadata; null when created_at is the file time.
local_asset.formatFormat name ("JPEG", "HEIC", "CR3", ...).
local_asset.meta_stateDesktop metadata pass: null (not tracked, the phone), 'pending', 'done' or 'failed'.
local_album.backup_selection0 none, 1 selected, 2 excluded.
local_album.path, added_atDesktop folder root: its absolute path and when it was added. Null for a device album.
upload_queue.upload_id, upload_offsetResumable chunked upload: the server's upload id and the bytes it acknowledged, so a restart continues from the offset.
remote_photo.archivedOff the timeline and Memories (#545).

The local full-text index (local_asset_fts, its terms view and triggers) is hand-written SQL in migrations/0001_local_search.sql, because drizzle-kit does not model FTS5.

Migrations​

npm run db:generate --workspace packages/local-db

runs drizzle-kit generate, which writes plain SQL migrations and the journal into migrations/, and then node scripts/build-migrations.mjs, which turns them into migrations/migrations.json: an ordered array of { tag, statements[] }. Every runtime applies that file and nothing else: the TypeScript runner imports it, the desktop's Rust side embeds it with include_str!. A statement is one chunk between drizzle-kit's --> statement-breakpoint markers, so a trigger body with inner ; stays whole; full-line -- comments are dropped, so each entry is exactly one statement. npm run db:check (build-migrations.mjs --check) exits 1 when the file is stale or missing, and CI runs it.

Bookkeeping is one table every runtime shares:

CREATE TABLE IF NOT EXISTS __local_db_migrations (tag TEXT PRIMARY KEY, applied_at INTEGER NOT NULL);

runMigrations(executor) runs before any query:

  1. Create the bookkeeping table if it is missing and read the applied tags.
  2. For each migration in array order whose tag is not applied, run its statements and insert its tag in one batch, so it lands whole or not at all.
  3. If that fails (a database from an older, squashed schema, a half-written file), wipe and rebuild. The remote mirror is disposable, so only outbox and upload_queue (PRESERVED_TABLES) must survive; sign-in lives outside the database. Their rows are read out, then every view is dropped (views reference tables), then every virtual table (FTS5 shadow tables such as x_data and x_idx go with their table x), then every other table, the bookkeeping table included. The schema is rebuilt from the migrations, recording each, and the saved rows are put back with only the columns the new tables still have (INSERT OR IGNORE). The caller then runs a full seed.

Noch nicht erschienen (kommt nach 1.7.0) The drops, the rebuild and the restore of the saved rows are one batch, so a kill during the wipe leaves the old database or the new one with its outbox, never an empty outbox; the columns kept are the ones src/schema.ts declares. runMigrations runs once per executor at a time: the mobile app calls it from DatabaseGate and from the background task, and a second call gets the running one. A migration batch that fails because another connection applied the same migration first (its tag is now recorded) is skipped instead of wiping. Before, two calls at boot raced on the same migration and the loser wiped a healthy database.

The desktop implements the same algorithm against the same file, so either app can open a database the other migrated. The mobile app has never shipped, so its history was squashed into one baseline; from now on migrations only append.

Adding a column​

  1. Add it to the table in src/schema.ts (and to the row type in src/types.ts if a query returns it).
  2. Run npm run db:generate --workspace packages/local-db and commit the new SQL file, the journal and migrations.json.
  3. If the column is searchable, change the local_asset_search_terms view and the triggers in a new hand-written migration, as 0001_local_search.sql did.
  4. Fill it where the rows are written: a writer and the app's mapper for mirror tables, the scan or the file seams for local media.
  5. Test the queries that read it, and run the checks below; db:check fails when migrations.json was not rebuilt.

The change hub​

createChangeHub({ subscribe, defer?, quietMs?, maxWaitMs?, deferCapMs? }) turns raw change events into a few flushes per second, scoped by table, and live queries watch it with hub.watch(tables | null, (changed) => ...). It is plain TypeScript with no React and no platform code, tested in Node with fake timers against a real better-sqlite3 database.

A runtime feeds it either its own update hook or trackChanges(executor):

  • the phone: expo-sqlite's addDatabaseChangeListener, bridged to the hub's subscribe in DatabaseGate.tsx;
  • the desktop: rusqlite's update hook forwarded as Tauri events, or trackChanges;
  • Node tests: trackChanges, which wraps an executor and reports every successful write once per statement (not per row). writtenTables(sql) gives the tables a statement writes: none for a read, several for a CTE or trigger body, and null for a change that names no table (DDL, PRAGMA), which wakes every watcher. Writes that happen inside triggers are not reported, so watch the table the statement names (local_asset, not local_asset_fts).

Why it exists​

An update hook is sqlite3_update_hook: it fires once per changed row, not once per commit. A camera-roll scan of a library of about 2,900 photos produced roughly that many events each for the asset upserts, the album links and the hash pass, with queue and log churn on top: well over 10,000 events. Before the hub, each one re-ran every mounted live query, and the most expensive of them, the merged timeline (a UNION ALL that sorts every remote photo and local asset), ran ten thousand times on the JS thread. That was the jank behind "scanning the camera roll makes the app very unresponsive".

The hub absorbs that storm:

  • trailing debounce (quietMs, default 90 ms): a burst becomes one flush;
  • max wait (maxWaitMs, default 450 ms): under a continuous stream the debounce would never settle, so a flush is forced at least this often. Photos keep appearing while a scan runs;
  • table scoping: a watcher may declare the tables its query reads, so a write to local_asset does not re-run a query that only reads remote_photo. A watcher that declares nothing is woken by everything, and an event that names no table wakes everyone: both defaults fail safe, with an extra re-run and never a missed one;
  • interaction deferral (defer, capped by deferCapMs, default 1.5 s): re-querying mid-gesture reflows a list under the user's finger, so a flush waits while they scroll, but never longer than the cap.

Noch nicht erschienen (kommt nach 1.7.0) A statement that deletes every row of a table carries a WHERE (DELETE FROM t WHERE true). Without one, SQLite takes its truncate shortcut, which does not call the update hook, so the phone's live queries did not hear about a reset (clearMirror, clearEntity, wipeLocalData). A test in reset.test.ts fails on such a statement anywhere in the package.

Coalescing cannot drop the final update. A flush carries no data; it is a signal to re-read, and the re-read sees the database as it is when it runs, so merging ten flushes loses nothing but work. And a flush always happens after the last change: every change re-arms the timer, which only another change or close() cancels. After the last change at time T a flush runs by T + quietMs (T + deferCapMs mid-gesture) and sees the final state.

Live queries in the mobile app​

Screens read synced entities through useReactiveQuery (apps/mobile/src/db/provider.tsx), which re-runs a query when the tables it reads change:

const { data, loading } = useReactiveQuery(["people"], (db) => people(db), [] as PersonRow[], {
tables: ["person"],
});

The hook is a TanStack Query observer on a QueryClient of its own, private to the database provider and separate from the app's network queries. Each hub flush invalidates the live queries that read a changed table: an active one re-reads, an inactive one is marked stale for its next mount. Its semantics:

  • cached per key: a screen that remounts paints its last result in its first frame and re-reads in the background, so there is no empty flash. Results nobody shows stay cached for ten minutes;
  • previous value while re-reading: after a change the current data stays on screen until the new rows arrive. keepPrevious extends that to a key change, such as a growing page or a new search term;
  • first-load state: loading is true until there is a result for the current key. Show an empty state only when loading is false, or "no photos" flashes before the rows;
  • no stale overwrite: a read that finishes after a newer read of the same key started answers with the newer read's result, so old rows can never replace newer ones (TanStack only cancels an in-flight read once the query has data, not on its first load);
  • never paused: networkMode: "always", because the reads are local.

The rules for callers:

  • The key names the query and every input it reads (an album id, a filter, the chosen sort). Results are cached per key, so two queries must never share one, and an input missing from the key means a stale answer. A filter object goes into the key by its fields, so one rebuilt on every render keeps its key.
  • tables lists the tables the query reads; only writes to them re-run it. Omitting it is always correct (any change re-runs the query). Declare only tables you checked against the query's SQL: an under-declared list shows stale data. The Memories card in the timeline header declares remote_photo only, so the camera-roll scan's local_asset churn does not re-run it while the user scrolls.
  • placeholder is data until the first result; the first render's value is kept, so an inline [] stays one stable array.

DatabaseGate.tsx opens the database, runs the migrations, and reads the timeline's first page and day buckets into the live-query cache (prefetchTimeline) before it renders the app, waiting at most WARM_UP_BUDGET_MS, so the timeline paints rows in its first frame after a cold start. It is mounted inside the signed-in area, so the mirror is always migrated before any live query runs.

Grid tiles find their offline thumbnail through one loader (apps/mobile/src/components/useThumbSource.ts): one query per tile would be hundreds of bridge round trips per screen, so every photo id requested in the same tick is answered by one IN (...) query, cached per photo as a live query on thumb_cache. While that lookup runs the tile shows its dominant colour, so it never loads the server thumbnail first and then the cached file.

How the apps use it​

Mobile​

src/db/ in apps/mobile is the app glue only:

  • client.ts: the phone's executor. expo-sqlite's async API over one database file, opened with journal_mode = WAL (readers never wait for the writer) and busy_timeout (a background task's connection and the app's wait for each other instead of failing), with the change listener on. Every call goes through a serial queue, because a batch's BEGIN ... COMMIT must not interleave with another caller's statement. expo-sqlite's own withExclusiveTransactionAsync is not used: it opens a second connection, which the change listener does not watch. Prepared statements are kept in a small LRU, and every method steps every row, "get" included, so a RETURNING statement writes them all.
  • DatabaseGate.tsx: open, migrate, warm up, and bridge the change listener to the hub. The listener forwards each event's table name, so the hub can skip queries that do not read it.
  • provider.tsx: DbProvider and useReactiveQuery.

Noch nicht erschienen (kommt nach 1.7.0) Three rules keep a sync run from losing work:

  • A trigger during a run is not lost. syncAll is single-flight: a second call gets the running promise (startRun in apps/mobile/src/sync/orchestrator.ts). It also records its job list in the run's RunState. A request that arrives while the run enqueues is enqueued before the worker starts; one that arrives while the worker runs is enqueued before the worker claims its next job (takeRequests), or makes drain run the worker again when it was already done. The outbox has the highest priority, so an edit made during a first backup of thousands of photos is sent after the job that is running, not after the backup. Checking the requests and closing the run is one synchronous step, and a call after that starts a new run behind the closing one. When a pass stops early (its job budget, a cancel), the requests it did not run go into the job queue before the run closes, so the next run does them. Before, an edit made while the worker ran (after outbox_replay was done) stayed in the outbox until some later trigger.
  • A job and its chained jobs are one batch. completeJob (sync/jobs/queue.ts) marks the job done and inserts the jobs its handler returned in one runBatch. A kill in between used to leave the job done and its chain (the next delta page, the next scan chunk) unqueued until the next full sync; now the job stays running and the boot reclaim runs it again.
  • An integrity reseed sweeps. When the counts drift, scheduleReseed (src/reset.ts) clears the entity's cursor and sets sync_state.sweep. Every page applied while it is set records its ids in sync_seen (recordSeenSql, in the page's batch), and when the entity is caught up sweepUnseen deletes, in one batch, the rows that were not seen (and their album memberships, photo details and photo_edit_base copies), then clears sync_seen and the flag. Offline-created albums (negative ids) are kept. A kill before the sweep keeps the flag and the seen ids, so the next pass sweeps. Before, the reseed only upserted, so a row the server no longer had stayed and the drift was reported on every sync. Sharing has no counts, so it is never swept. A refused album or person edit (dropOutboxRow), or one the user discards in Sync status (discardParkedOutbox), schedules the same reseed of user_album or person, and a refused or discarded offline album create deletes its temporary album.

Noch nicht erschienen (kommt nach 1.7.0) The local data belongs to one account, the pair of server address and user id recorded in app_meta (localDataAccount). apps/mobile/src/sync/account.ts keeps it that way, one step at a time:

  • Logging out (leaveAccount) refuses new sync runs at once, writes the keychain marker (logoutFlag in lib/tokenStorage.ts, which holds the refresh token it marks), clears the tokens, and only then writes app_meta.logout_pending (markLogoutPending), whose write can wait seconds behind a running drain in the database queue. The refresh token is read before all that, so the server can still be told after the tokens are gone. The logout then cancels the running drain and waits for it (stopSync), tells the server (best effort, with a timeout; the app shows a message when it could not), deletes the thumbnail files and the expo-image caches, and wipes every table (wipeLocalData), which drops the database marker with the data. When nothing was left behind, the keychain marker is deleted last; if that delete fails, the logout counts as unfinished and the next start finishes it. The mirror, the outbox, the upload queue and the local_asset hashes, which carry the user id, all go.
  • Clearing the tokens is checked by reading them back (tokensGone): a token still there after clearTokens counts as a failed step, so the keychain marker stays.
  • If neither marker can be written, nothing durable tells the next start that a logout was asked for, so the logout stops the drain and wipes before it tells the server. When the keychain refuses the marker, a kill before the very next keychain call (the token clear) is the one case in which the next start signs in again, and a kill before the database marker or the wipe leaves the data until the next sign-in and logout (MobileLogoutNoMarkerLeft in verification/mobile-device).
  • Every step of the logout is tried even when an earlier one fails, and the app is signed out in any case. A step that failed (the keychain, a cache, the wipe itself) leaves the logout marked: the wipe writes logout_pending again in its own batch (keepLogoutPending), and both markers are written again after it. The login screen then says that the app tries again when it starts, and only then: when no marker could be kept, it says that some data was left and that signing in and logging out again removes it (dataLeftBehind). A logout the app was killed in the middle of is finished on the next start too: DatabaseGate first looks for either marker (enterStoredAccount), tells the server with the refresh token the keychain marker holds (best effort, up to 10 seconds; the login screen says so when it could not, serverNotTold), and then finishes the logout. The background task does the same when tokens are still stored, and a refresh the server refused does it without telling (endedByServer). A sign-in that finds a marker wipes and deletes the keychain marker before it stores the tokens; if that delete fails, the sign-in goes on, and the next start drops the marker because a newer refresh token is stored than the one it holds. When the start-up check fails, the app signs out and says so. When opening the database or running its migrations fails (and the rebuild after a failed migration fails too, as with a full disk), DatabaseGate shows an error with Retry instead of its spinner.
  • The outbox replay (sync/outbox/replay.ts) treats a 401 as the end of the session, not as a refused edit: it puts the row back as it was, without counting an attempt, and stops the pass. Every queued edit then waits for the next sign-in: the same account replays them, another account or a pending logout wipes them with the rest of the data. Before, each row got its own 401 and was dropped with a toast, so a session the server ended lost every offline edit (from 1.8.0, with the password-bound sessions, a password change on the web ends the phone's session too). Only a 401 the transport marks as the end of the session (ApiError.sessionEnded: the refresh was refused, the tokens were cleared, or another session's tokens are stored) keeps a row this way. A 401 that a freshly refreshed token got as well is counted like a server error, so that row parks after MAX_ATTEMPTS instead of coming back at the head of the outbox after every sign-in. A kept row is replayed silently after the same account signs in again; asking first ("unsent changes from your previous session: Send or Discard") is a follow-up.
  • Noch nicht erschienen (kommt nach 1.7.0) The replay keeps edits of the same thing in order. Two rows overlap when they write the same field of the same photo, or touch the same album or person. nextOutboxRow walks the live rows in id order. It skips a parked row and every later row that overlaps a skipped one, and stops at the first other row: it returns that row, or nothing while that row waits out its backoff. So a later edit of the same photo never reaches the server first, and a parked row holds back only the rows that overlap it.
  • Noch nicht erschienen (kommt nach 1.7.0) An outage never costs an edit. A network failure is retried without counting an attempt; that is an error the api-client's fetch threw, which it marks (isNetworkFailure). So are 408, 429, 502, 503 and 504, and 520 to 524 and 530, with which a tunnel or CDN in front of the server says that it is down, and an answer that something else gave while the server was away (next point). outbox.retries drives the backoff, 15 s doubling up to 16 minutes, unless the server sends Retry-After (ApiError.retryAfterMs), which is used when it is finite and capped at those 16 minutes. A network failure always waits 15 s and doesn't raise retries: the phone is offline, nothing spares the server by waiting longer, and one gateway error after a long time offline must not get the longest wait. A 500 counts an attempt, because one edit can make the server fail every time: after eight the edit is parked, and the app tries it again at its next start, or when the user presses Retry. An outage writes one retry: row to the sync log, not one for each try, and a drain wake that changed nothing writes none, so the 500 rows of the log still show what happened before.
  • Noch nicht erschienen (kommt nach 1.7.0) Only LibrePhotos can refuse an edit. While the container restarts, a proxy such as Traefik answers 404 or 403 with its own page, and the portal of a hotel network answers 200 with HTML. packages/api-client/src/answers.ts tells such an answer from the server's own (isForeignAnswer), for the phone and the desktop app. A 4xx is the server's own when its body is the API's error list, {"errors": [{"field", "message", "code"}]}, or, for /api/upload/, has a detail; api/tests/infra/test_refusal_bodies.py pins those bodies. A 200 with text or HTML, with an empty body where one is expected, or with JSON that does not parse is a ForeignAnswerError, thrown by the transport, so a portal's page is not taken for the empty answer to a deletion either. A thumbnail is kept on the phone only when the answer's type is an image or a video's clip (isMedia in lib/download.ts); before, a portal's page was stored as the thumbnail. The body's form is all the apps look at: JSON that another service answers with a 200 still completes a favourite, a rating, a caption, an album's deletion or an upload, and an empty 200 or a 204 completes a deletion. An edit that got a foreign answer stays in the outbox.
  • Noch nicht erschienen (kommt nach 1.7.0) Whether that try counts depends on the server. A foreign answer can also come from a server that is up: Django answers a route it does not have (an older server) with an HTML page, and a filter in a proxy can block one edit every time. So the app asks GET /api/imagetag/, which every version has (serverAnswers). Only when LibrePhotos answers that, with the tag or with an error of its own, does the try count, and the edit parks after eight. Otherwise it is an outage and the try does not count. judgeFailure is the whole rule: refused, counted or outage. Before, a 404 from a proxy dropped the edit, and so did JSON that was cut short. verification/mobile-sync/ForeignAnswer.tla checks it.
  • Noch nicht erschienen (kommt nach 1.7.0) A waiting row brings its own replay. A pass that leaves a row in its backoff chains an outbox_replay job that is due when the row is (outboxWaitsUntil), and the drain wake (sync/drain-wake.ts) starts a run then. A job that became due between the end of a run and arming the wake starts one a second later, except after a background pass that stopped on its job budget. Before, the connectivity trigger found the row still waiting and nothing looked again until the next trigger. verification/mobile-sync/OutboxWake.tla checks this.
  • Noch nicht erschienen (kommt nach 1.7.0) Any other 5xx counts, and so does the 401 above and any other error, a bug in the code included. At MAX_ATTEMPTS (8) the row parks (failed with attempts >= MAX_ATTEMPTS) and keeps the phone's value.
  • Noch nicht erschienen (kommt nach 1.7.0) Sync status shows the parked rows with Retry and Discard. Retry (retryParkedOutbox, which DatabaseGate also runs at every app start) gives each parked row one more try: its counted attempts go to MAX_ATTEMPTS - 1, so the next server error parks it again. Discard (discardParkedOutbox) is one batch: it deletes the parked rows, puts the server values back on every photo they touched, and only then deletes the photo_edit_base copies no live row needs. A refused offline album create deletes its temporary album, unless the create is still queued.
  • Noch nicht erschienen (kommt nach 1.7.0) A 4xx of the server's own drops the row at once, with a toast and the server values put back, and so does a payload that does not parse. For an album or a person the phone has no server values to put back, so the same run reads the albums or people again (reread), and Discard starts a sync for it; before, a deletion the server refused kept the album off the screen until the next full sync. Before, network failures counted, and a row parked after 8 of them was never sent; and a row in its backoff was skipped, so the older edit reached the server last and won.
  • The model verification/mobile-sync/OutboxReplay.tla checks these rules for one flag on two photos: order, the outage, parking, Retry, Discard and the revert.
  • A refused refresh in the Android background task, which runs before any auth status is known, ends the session like one in the app: onLoggedOut ignores a 401 only while the status is "unauthenticated". Without the marker, a session the server ended only stops syncing and keeps the data for the same account's next sign-in. The marker and the account record survive the rebuild after a failed migration (PRESERVED_META_KEYS in migrate.ts).
  • Signing in (enterAccount) waits for all that, then wipes again when the data belongs to another server or user, or when a logout is still pending, records the new account in the same batch, and only then stores the tokens; the login screen flips to the app after it. A logout that starts while a sign-in is still being set up wins: that sign-in never becomes active, and the login screen reports the sign-in as failed. A 401 of an old request that lands while nobody is signed in is ignored. The device is then scanned and hashed again for the new account, so its photos can be backed up there; backup starts off for every account.
  • Noch nicht erschienen (kommt nach 1.7.0) Stored tokens always lie next to the address of the server that gave them, and no request takes them to another one. The login form sends the credentials through signInClient, which has no tokens, and the app keeps its server until the typed one accepts. Then startSession deletes the old tokens, stores the new address, uses it, and stores the new tokens, in that order, so a kill or a keychain error in between leaves the app signed out. hydrateServerUrl reads the stored address and never replaces one in use. The background pass calls it too, because Android runs the pass in a process that has no screen and so never ran bootstrap; without it every queued edit failed for lack of an address, eight passes parked it, and nothing was synced. The pass does nothing while another server's address is in use.
  • Noch nicht erschienen (kommt nach 1.7.0) Android runs the background pass in a process that may never have shown a screen, so two things work differently there. apps/mobile/index.js, the app's entry file, defines the task before it loads the router, because expo-router loads a screen's code only when the screen is shown; before, the task did not exist in a process that the system started for the pass. And no JavaScript timer fires while no screen is open, not even a setTimeout of 0 ms. expo-task-manager starts a headless task against this, but that task ends at once and takes the timers with it. So the pauses of a run (createPause in sync/pause.ts) use a timer only while the app is open, go on at once when it is not, and end when the user leaves the app; before, a pass stopped for good after its first job and kept the next ones from running. Code that a pass waits for must not depend on a timer. The time limits for a request are timers too, so in a pass only the system ends a request that hangs; what happens to it after that is the next point.
  • Noch nicht erschienen (kommt nach 1.7.0) A request that never gets an answer does not hold up the sync for good. React Native and Expo set no time limit on a request, so lib/requests.ts does. runClient sends every request of a run with the run's abort signal, and gives up waiting as soon as the run is stopped, whether or not the native request reacts. stopSync, and with it a logout or a sign-in, therefore waits for no request: uploads, thumbnail downloads and a photo that iCloud does not deliver included. A stopped run puts the edit it was sending back in line (releaseOutboxRow) and the photo back to pending. That edit may have arrived; sending it again changes nothing, an album's creation included: the server has one album per title and owner and answers with it. limited ends a request after two minutes (ten for the end of an upload) as a network failure; the app's own client uses it too, so a token refresh that hangs does not hold up every later request. The limit is a timer. Because timers do not fire in a background pass, each new run and each new pass first ends the requests that are over their limit (expireRequests). A request that hangs in a pass therefore costs that pass, which the system ends after ten minutes, and not the next one. Two waits have no limit, because their length depends on the size of the file: the transfer of a file, and the download of an original from iCloud (untilStopped in sync/upload/worker.ts). On iOS, one that gets stuck holds its run, and every later sync with it, until the user logs out or signs in, or the app is restarted. verification/mobile-sync/HungRequest.tla checks the requests that have a limit. Before, the run looked at its signal only between jobs, and one hanging request kept the next sync, the logout and the sign-in waiting until the app was restarted.
  • Noch nicht erschienen (kommt nach 1.7.0) useSessionBootstrap restores the session at start. iOS can start the app in the background while the phone is locked and the keychain can't be read; the app then tries again when it is opened. If the keychain can't be read in the open app either, the login screen says so instead of a spinner that never ends. verification/mobile-device/ServerAddress.tla checks both this and the address.
  • Noch nicht erschienen (kommt nach 1.7.0) ForgetSignedOutUser clears what is only in memory when nobody is signed in: both query caches and the timeline filter, so the next account starts with its own saved filter.
  • Every sync run uses the account active when it started: runSync refuses to run without one, and uploads and thumbnail downloads only ever get that account's access token, sent to its server. startRun registers a run at once, a repair included (its clean-up runs inside the run), so a logout always waits for it.
  • pullEntityStep checks the abort signal again before it applies a page, so a page fetched before a cancel is dropped.

The mobile lint rules keep it that way: only client.ts, DatabaseGate.tsx and the key-value preferences store (its own file) may import expo-sqlite, and nothing imports drizzle-orm/expo-sqlite or better-sqlite3. The app's jest node project runs the sync and mutation tests on the package's test database; the database's own tests live in the package.

Desktop​

The desktop's folder library maps onto the same tables as the camera roll (src/queries/library.ts). A folder the user added is a local_album with source = 'folder' and its absolute path; each file under it is a local_asset whose uri is the file path, linked to the root through local_album_asset. Files are referenced in place. The desktop's file seams hand their findings to the writers as data:

  • walk batches and the watcher's created and modified files go to upsertWalkedFiles. Rows are matched by path. A new file gets its path as id and meta_state = 'pending'; a known file whose size or mtime changed is new content, so its hash and metadata are asked for again; an unchanged one keeps both, so a rescan costs nothing. Each chunk of 500 files is one batch of three statements over the chunk as a table, which makes a 2,000-file walk several times faster than statements per file;
  • removed paths (a file or a whole folder) go to removeLocalPaths, which forgets each path and everything below it;
  • renames go to renameLocalPaths: the row stays, so its hash, metadata and upload state survive, and a folder rename moves every file below it;
  • metadata reads for the files pendingMetadata hands out go to applyFileMetadata; paths that could not be read are not asked for again until they change;
  • MD5s go to setFileHashes, stored as the server matches photos (image_hash is the MD5 plus the user id). markHashUnreadable parks a file the desktop could not read.

A file sorts by its date taken as wall-clock milliseconds (takenMs, its day read with UTC getters) once its metadata is read, and by its mtime (its day in the local time zone) until then or when it has no date. A file's searchable folder starts at the added folder's own name: words above it ("C:", "Users") would match every file.

A loose file, one the user opened from outside the library and asked about or to upload (upsertLooseFile), is a local_asset without a root link: it shares the hash cache and the upload queue but never shows in the library views. Removing a root, or a path under it, keeps the files that have a live upload, as loose files.

local_asset_fts (FTS5) indexes every local asset under its name, its kind, its folder words, date words from bucket_day (year, month names, yyyy-mm, yyyy-mm-dd), "undated" when the metadata pass found no date, and its format, camera and lens. The terms are defined once, in the view local_asset_search_terms, and triggers on local_asset keep the index in sync, so nothing in the package writes it except rebuildLocalSearch, which you run after a VACUUM (it may renumber the rowids the index uses).

searchLocalAssets(db, query) splits a query into words the way FTS5's unicode61 tokenizer splits text (lower case, on anything that is not a letter or digit), and every word must prefix-match one of a file's words: "march 2023", "canon raw", "holiday 2019 heic".

The upload queue​

src/queries/upload-queue.ts owns which local assets get queued, which run next and the state transitions; the workers in the apps drive the state machine.

  • Enqueue rule (enqueueBackups): a local asset that is hashed, is in a backup-selected album, is in no excluded album, and whose hash has no remote_photo counterpart. Existing rows, in any state, are left alone.
  • iCloud exception: an asset the hash pass parked as hash_state = 'icloud' has no hash because its bytes are not on the phone. Refusing to queue it would drop most of the library on an iPhone with "Optimise iPhone Storage", so it is queued unhashed and the worker downloads it once and hashes the bytes on their way out.
  • Order: hashed assets first, then first in, first out. An unhashed row needs a download first, and letting one to the front would park the serial queue on a network fetch while files on the disk wait.
  • Retries: a failure bumps the attempts and sets a backoff of BACKOFF_BASE_MS * 2^attempts; after MAX_ATTEMPTS the row is parked as failed until the user retries.
  • Processing on the server (#2740): once the bytes are uploaded the row records the server's photo id, and the timeline shows an hourglass on that camera-roll row until the mirror has the photo with a thumbnail.
  • Concurrent workers (the desktop): claimNextUpload claims atomically, and a file whose hash another row is already uploading (two copies of one picture) waits. claimExistenceChecks claims hashed rows for a server existence check; the checker answers each with markSkipped (the server has it) or markAbsent (back to pending, marked as checked so it is not claimed for a check again). setUploadProgress records how far a resumable upload got, and resetInFlightUploads runs at startup, before any claim, so work a previous run stopped in the middle of starts over from its saved offset. switchFolderAccount rebuilds every hash for another server account and starts the queue over; cancelFolderBackups drops queued uploads of files in no backed-up root when backup is turned off. Noch nicht erschienen (kommt nach 1.7.0) The writes that settle a claim take an UploadClaim (checkClaim, uploadClaim) instead of an asset id and then change only the row that was claimed: same state, same enqueued_at. An answer that arrives after an account switch replaced the row is dropped, so a file is never marked as on the server for an account that does not have it.

Testing​

createTestDb() from @librephotos/local-db/testing is an in-memory database with the real schema, applied from the same migrations.json, behind the same async executor contract the apps implement. There are no mocks, so a query passes here exactly when it passes on the phone. It is synchronous, so it fits a plain beforeEach, and writes through its db or executor are reported to its subscribe per table, the way the apps' change listeners report them, so live-query code can be tested against it. rawCount(t, sql) counts straight on the raw connection, for assertions inside a synchronous callback (a progress seam) where the async db cannot be awaited.

In the mobile app, renderWithDb (src/test/test-utils.tsx) renders a screen over a test database with the app's providers and a mocked API client and waits for the first reads; pass the test database's subscribe to have writes re-run live queries. Reads are async (the executor resolves on a microtask, TanStack Query notifies on a macrotask), so a test calls settleDb() after a render or a press before it asserts. Package tests say "this is here" with defined(value), which fails on the spot, rather than a non-null assertion that only claims it. The vitest timeouts are long because the EXPLAIN budget test seeds 100,000 rows before it asserts anything.

Adding a query​

  1. Write it in the matching module under src/queries/ as an async function that takes an AppDatabase, in raw SQL with db.all<Row>(sql`...`) or db.get<Row>(...), and export it from src/queries/index.ts. A write that must be atomic is one statement or one runBatch.
  2. Test it in src/__tests__/ against createTestDb(). A query the timeline runs often belongs in explain.test.ts, which checks its EXPLAIN QUERY PLAN over 100,000 rows uses an index and no temporary sort.
  3. In the mobile app, read it with useReactiveQuery, with a key that carries every input and a tables list checked against the SQL.

Checks and CI​

cd packages/local-db
node ../../node_modules/typescript/bin/tsc --noEmit
node node_modules/eslint/bin/eslint.js src scripts
node node_modules/vitest/vitest.mjs run
node scripts/build-migrations.mjs --check

or npm run typecheck|lint|test|db:check --workspace packages/local-db. The local-db workflow (.github/workflows/local-db.yml) runs them, installing only this workspace's dependencies from the root lockfile; better-sqlite3 13 ships its native binaries in the package (prebuilds/), so there is no build step. The mobile workflow runs the app's own tests against the package. node scripts/check-shared-deps.mjs also fails when the mobile app's drizzle-orm does not satisfy the package's peer range, or when the root lockfile nests a second drizzle-orm under apps/mobile or the package: two copies would mean two sets of Drizzle classes in one bundle.