Add optional Supabase cloud sync with offline-first architecture#4
Open
Add optional Supabase cloud sync with offline-first architecture#4
Conversation
Adds an offline-first sync layer so clients, sessions, behavioral observations, treatment plans, and the related tables persist to Supabase in addition to local IndexedDB. Existing local data and fictitious test data upload automatically on first sign-in. - Dexie schema bumped to v4 with sync metadata (ownerId, _dirty, _deleted, _syncedAt) and indexes; hooks auto-tag every write. - src/services/sync.ts implements outbox push + pull + tombstones with last-write-wins on updatedAt. - Email/password auth via Supabase, optional: app keeps working local-only when env vars are unset. - New /sync page + bottom nav entry with pending/online indicator. - Reads filter out tombstones; deletes call softDelete(). - supabase/migrations/001_init.sql defines tables + RLS scoped by owner_id; supabase/README.md covers project setup.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds optional cloud synchronization to the ABA data collection app using Supabase, while maintaining full offline-first functionality. All writes go to local IndexedDB first for instant UI responsiveness, then are automatically synced to the cloud when online.
Key Changes
Offline-first sync engine (
src/services/sync.ts): Implements bidirectional sync with automatic push/pull, debounced flushing, and network-aware scheduling. Dirty rows are tracked locally and synced when the network is available or on manual trigger.Supabase integration (
src/services/supabase.ts,src/services/auth.ts): Optional Supabase client initialization from environment variables. If not configured, the app continues to work entirely offline with no UI changes.Database schema updates (
src/db/database.ts): Added sync metadata fields (_dirty,_deleted,_syncedAt,ownerId) to all synced tables via Dexie v4 migration. Automatic hooks tag every create/update with_dirty=1so the sync service knows what to push.Cloud schema (
supabase/migrations/001_init.sql): Created 6 tables mirroring the local schema with row-level security policies ensuring each user only sees their own data. JSONB columns store nested arrays to avoid maintaining parallel relational tables.Authentication UI (
src/pages/SyncPage.tsx): New page for sign-up/sign-in with sync status display (pending uploads, last sync time, network status, error messages). Manual sync trigger and sign-out button.Sync status subscription (
src/hooks/useSyncStatus.ts): React hook for UI components to subscribe to sync state changes without polling.Navigation integration (
src/App.tsx): Added Sync nav item with badge showing pending upload count (only visible when Supabase is configured).Soft delete support: Updated pages to use
softDelete()instead of hard deletes, and filter out_deletedrows from queries. Tombstones are synced to the cloud then hard-deleted locally.Notable Implementation Details
https://claude.ai/code/session_011BCeQtwoemDz6uQ6HKQhAT