SQLite remains the usable local dataset#
database/database.ts opens and migrates the local database. schema.ts defines baseline tables and seed values; migrations.ts adds homes, cloud state, shopping checks and data contexts.
Repositories take an optional existing SQLiteDatabase so orchestration can reuse a transaction handle. SQL row representation stays in the repository; screens consume typed models.
| Aggregate | Tables / repository files |
|---|---|
| Configuration | settings, ai_state, preferences; settings.ts, aiState.ts, preferences.ts |
| Ingredients | ingredients, ingredient_aliases; matching repositories |
| Recipes | recipes, recipe_ingredients, recipe_steps, recipe_tags |
| Stock and locations | homes, pantry; homes.ts, pantry.ts |
| Plan | weeks, week_day_budgets, slots, slot_events |
| Shopping | shopping_items, shopping_checks |
| Offers | stores, deals, deal_matches, scrape_runs |
| AI history | ai_runs |
| Sync | sync_state, sync_meta, sync_outbox, sync_tombstones |
| Dataset switching | data_contexts |
Money uses integer cents. Physical quantities use numeric quantity plus a unit. SQLite booleans are encoded integers. A cooking duration, ingredient quantity and price must not be confused merely because each is numeric.
Domain identity and derived data#
Domain rows have UUIDs and update timestamps so AI references and device sync do not depend on local integer IDs. Recipe-to-ingredient, slot-to-recipe and pantry-to-home relationships are local foreign keys, converted to UUID references for transport.
Ratings, recipe total time, generated shopping quantities and estimated cost are computed from underlying records. A derived shopping cache may be stored for UI convenience, but it is not an independent authoritative dataset to merge across devices.
What is synchronized#
sync/tables.ts is the authoritative client allowlist. It includes homes, stores, preferences, ingredients/aliases, recipes/ingredients/steps/tags, pantry, weeks/day budgets, slots, manual shopping items and shopping checks.
The file also declares:
- Selected columns only, rather than arbitrary local rows.
- Parent relationships requiring UUID conversion.
- Boolean conversion.
- Natural keys for singleton/default and relationship rows.
onlyManualforshopping_items.- A whitelist of synchronized setting keys.
AI credentials, model binaries, retailer caches, scrape history and AI run text are not ordinary entries in this cloud table list. Updating this list requires reviewing schema compatibility, trigger creation, parent ordering and remote authorization together.
How Banana sync differs from Coconut sync#
syncNow deduplicates concurrent calls with one pending promise. Its current runSync lists remote rows per configured table, compares them against local data, then pushes newer/missing local rows. The lastSyncAt status is not evidence this implementation uses Coconut-style incremental remote watermark queries.
Core sequence:
- Require enabled sync and an authenticated account.
- Resolve the active dataset identity and switch the local context if necessary.
- Verify/set the local identity binding.
- Fetch tombstones, pending local deletes and remote rows.
- Apply applicable tombstones and remote rows in a guarded SQLite transaction.
- Compare local rows to fetched remote rows and upsert newer/missing payloads.
- Upload local tombstones and delete corresponding remote rows.
- Synchronize allowed settings and the account username.
- Report usage and record success status/time.
transport.ts isolates the remote operations, allowing tests to inject a fake transport.
Conflict and relationship handling#
toRemotePayload rewrites local _id references as _uuid payload fields. toLocalValues requires those parents to exist locally and rejects invalid payload values instead of inventing relationships.
applyRemote compares update timestamps. A newer local record is kept; an equal timestamp with a dirty local outbox entry also retains the local edit. A remote winner is applied and the relevant queued keys are removed. First-sync special cases let untouched default stores/home/preferences yield to existing cloud data.
This is timestamp conflict handling—not field-by-field collaborative merging. Device clock errors can affect outcomes. Review offline simultaneous edits explicitly before claiming lossless multi-writer behavior.
applyTombstones deletes children before parents by reversing configured table order. A local record newer than a remote tombstone can cause that obsolete tombstone to be removed. Pending local deletes are excluded from row application/upload so a later phase does not immediately resurrect them.
normalizeShoppingCheckKeys rebuilds portable checked-item keys from week/home/ingredient UUIDs and unit, reconciling duplicates. Local integer IDs cannot form a stable key across devices.
A household is a dataset identity#
sync/households.ts wraps:
resolve_data_identityhousehold_statusinvite_householdrespond_household_invitationleave_household
These are banana schema RPCs. The shared-life migration introduces data_identities, household_members and household_invitations.
For personal data, the identity corresponds to the user UUID. For a household, it is a separate shared UUID. The existing cloud column name user_id therefore becomes a dataset owner identity, not always the signed-in person's Auth UUID.
can_access_identity allows one's personal identity or an identity where the caller has household membership. Foreign keys and RLS are changed accordingly.
Invitation and departure behavior#
Invites target another registered SeedWorks email. A caller without a household creates a shared identity and becomes its member. Inviting another account does not immediately enroll the recipient.
Acceptance is bound to the recipient and verifies that the invitation is no older than seven days and the inviter still belongs to that household. The membership primary key permits one active household per account. Declining deletes that invitation; leaving removes the caller's membership and invitations they issued.
This is explicit membership, not merging two personal recipe databases. After leaving, personal context becomes active again. Do not “restore” a household by uploading the user's personal snapshot under the shared identity.
Local context snapshots#
switchDataContext stores the active dataset's table rows as a JSON snapshot in data_contexts, keyed by identity. In one local transaction it then replaces the active tables with the target snapshot or new defaults.
It preserves specifically listed personal settings, suppresses change capture during replacement, restores only columns present in the current schema, sets active/bound identity metadata and preserves the enabled flag.
Thus “the local database” contains both active rows and saved inactive contexts. A household switch is not deletion of the personal dataset, but old context snapshots are still sensitive local data.
Triggers, realtime and refresh#
syncTriggersSql records inserts/updates in an outbox and deletes in tombstones unless pull_in_progress is active. Manual-only restrictions also apply at the trigger boundary for shopping rows.
realtime.ts, use-cloud-sync.ts, local change counts and provider refresh connect change signals to durable synchronization. A realtime notification does not itself replace the need to fetch/apply current state.
Authentication and profile#
sync/supabase.ts signs in with the existing Seed account using email/password. It is not the hosted Coconut OAuth client. Username synchronization addresses the account profile using the account UUID, even while domain data uses a household identity.
This distinction also affects authorization: native password sessions have no OAuth client_id and retain legacy platform-wide authority. See authorization boundaries.
Backup caveats#
utils/backup.ts uses SQLite's online backup API so WAL contents become a coherent staging file. It clears the staging ai_state.llm_api_key, deletes sync_meta, disables cloud sync and clears sync status, then vacuums the copy before sharing.
That does not mean the backup is nonsensitive: recipes, transcripts/audits, household snapshots and other domain data can remain. The reviewed scrub operations do not constitute a general token/secret scanner for arbitrary future columns.
Restore copies the selected database over the current one. It is destructive by design and must require deliberate user authorization; it is not a safe troubleshooting shortcut.