Seedworks Labs Wiki The engineering referenceStudio website ↗
Coconut

Storage & data model

SQLite tables, migrations, identities, backups and platform variants.

Engineering documentation • Reviewed 2026-11-29

Database startup#

getDatabase caches the asynchronous opening operation, so callers share one initialization path. Before returning the handle it:

  1. Resolves the platform-specific database name.
  2. Acquires database access—particularly important for browser ownership.
  3. Opens SQLite with expo-sqlite.
  4. Enables WAL and disables recursive triggers.
  5. Applies pending migrations.
  6. Recovers a stale pull guard.
  7. Wraps the handle in the platform's database coordination layer.

A failed open is normalized into a usable database-access error, and a rejected cached promise is released for a later retry. Closing a connection is not the same as deleting a dataset.

Persistent aggregates#

schema.ts contains the initial local schema; migrations.ts evolves it. Consult both: the initial schema alone omits later features.

Tables / family Role and relationships Repository
settings Key/value domain preferences; only selected keys travel to cloud settings.ts
months Month key, allowance, opening/closing reserve and lifecycle state months.ts
fixed_expenses → month_fixed_expenses Recurring templates and historical month-specific expected/actual amounts fixedExpenses.ts
budgets → month_budgets Reusable envelopes and per-month planned allocations budgets.ts, monthPlan.ts
transactions Spending with budget references, date, rating and optional provenance/attachment data transactions.ts
income One-off monthly receipts income.ts
yearly_subscriptions and month subscription data Period-based templates and month-specific deductions subscriptions.ts, monthSubscriptions.ts
reserve_transfers Reserve history including automatic closing movements reserve.ts
merchant_suggestions Remembered merchant/budget assistance reserve.ts
month_budget_plan_events and plan fields Changes made when explicitly replanning monthPlan.ts
shared_spaces, shared_space_members Collaborative containers and member identities sharedSpaces.ts
shared_periods, shared_period_reports Shared reporting periods and closed report payloads sharedPeriods.ts
shared_expenses, shared_expense_splits Payer totals and per-member obligations sharedExpenses.ts
sync_state, sync_meta Connection, enablement, status, identity and watermarks/guards syncState.ts, syncMeta.ts
sync_outbox, sync_tombstones, shared_sync_tombstones Pending uploads and deletion propagation Migrations and sync modules

This is a relationship map, not a full DDL dump. The source inventory gives exact files for the current columns and functions.

Local IDs versus portable identities#

Most local domain relationships use integer primary keys for efficient joins. Sync-facing rows additionally carry a stable uuid and updated_at.

Example: device A's budget integer ID 4 may be device B's ID 19. The cloud relationship must refer to the budget UUID, not copy 4. Push serialization resolves local ID to UUID; pull reconstructs UUID to local ID before writing children.

models/index.ts defines the TypeScript shapes. Repository row mappers handle SQL representation rather than leaking raw database rows into the UI. database/queries.ts assembles named read models such as getMonthData.

Migrations and repair#

The migration runner reads PRAGMA user_version, sorts pending numeric migration IDs, and executes each migration and its new version marker in a transaction. Some migrations include an isAlreadyApplied check to handle preexisting shapes.

After numbered migrations, startup also checks legacy sync-state/subscription shapes. The subscription repair can rebuild older yearly_subscriptions layouts into the current period model and recreate triggers. That is existing compatibility behavior—not an invitation to edit applied migrations.

When adding a sync table:

  1. Append a new local migration.
  2. Declare the new table's change-capture triggers at the migration where it actually exists.
  3. Record its introduction in sync specifications (addedIn where used).
  4. Add serialization and parent references.
  5. Add the matching cloud migration in Supabase.
  6. Review RLS and restrictive OAuth boundaries.
  7. Test upgrading an old database, not only opening a new one.

Regenerating an earlier migration's trigger SQL using today's entire table list can reference tables that did not exist at that migration step.

Change capture#

SQLite triggers record inserts/updates in the outbox and deletes in tombstones. A pull guard prevents remotely applied writes from becoming new outgoing edits. Startup recovery matters because a stale guard could otherwise suppress legitimate local change capture.

Outbox contents are pending transport work, not the authoritative domain state. A full repair sync can enumerate the actual local data rather than assuming every historical row was captured by triggers.

Browser-specific storage#

The hosted app uses account-bound cache names and identity binding. database-name.web.ts and hosted-bootstrap.ts are the first files to inspect for account-cache behavior.

database-access.web.ts acquires a Web Lock before opening SQLite. One active Coconut tab/window per origin owns the database access for the page/worker lifetime—even if a particular connection closes. A second tab displays a close-other-tabs condition rather than contending for OPFS/VFS handles.

database-coordination.web.ts and sync-batch.web.ts coordinate local work around asynchronous sync application. The native variants preserve the same import boundary without assuming browser APIs.

Do not “repair” browser contention by clearing storage. Close the other tab and retry; a failed VFS initialization may require a reload. Browser data may contain unsynced financial records.

Backup and restore#

utils/backup.ts implements transfer, and backup-validation.ts contains validation. The backup UI is app/planning/backup.tsx.

A backup is a separate recovery mechanism from sync. Sync propagates changes, including deletions; it is not an immutable history. Treat exported financial files as sensitive. Validate compatibility and preserve the current database before authorizing any restore that replaces it.

This wiki does not execute a restore, inspect real financial records or copy the workspace's SQL data dump into the public site.

Shared Spaces explains the shared rows and personal mirrors. Sync explains how local data travels and how missing foreign keys are repaired.