Supabase Setup
Supabase provides the cloud backend for Tsumihon, handling authentication, database storage, and real-time synchronization across all your devices.
Overview
With Supabase configured, your library syncs in real-time across iOS, Android, and the web. Changes on one device appear on all others within about 10 seconds, powered by database triggers and Firebase Cloud Messaging (FCM).
Step 1: Create a Supabase Project
- Go to supabase.com and sign up or log in.
- Click New Project.
- Choose an organization, enter a project name, set a database password, and select a region close to you.
- Wait for the project to finish provisioning.
Step 2: Get Your API Credentials
- In your Supabase dashboard, go to Settings → API.
- Copy the Project URL (e.g.,
https://your-project.supabase.co). - Copy the anon/public key (the publishable key).
You'll add these to your app's .env file:
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_PUBLISHABLE_KEY=your-anon-key-here
Step 3: Configure Firebase for Push Notifications
Real-time sync uses FCM to notify other devices of changes. In your Supabase dashboard:
- Go to Edge Functions → Secrets.
- Add the following secrets:
FIREBASE_PROJECT_ID— Your Firebase project ID (e.g.,tsumihon-12345)FIREBASE_CLIENT_EMAIL— Service account email from the Firebase ConsoleFIREBASE_PRIVATE_KEY— The full private key, includingBEGINandENDlines
Step 4: Run the Database Migration
Apply the migration to create all required tables, triggers, and RLS policies:
# Using the Supabase CLI
supabase db push
# Or apply manually via the Supabase Dashboard SQL Editor
# File: supabase/migrations/20260131130000_create_book_sync_trigger.sql
Step 5: Deploy the Edge Function
The sync notification edge function sends FCM messages when books change:
supabase functions deploy send-sync-notification
Step 6: Configure the App
Add your Supabase credentials to the .env file in the project root (create from .env.example if needed):
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_PUBLISHABLE_KEY=your-anon-key-here
Verification
To verify everything works:
- Open the app on two devices logged into the same account.
- Add a book on Device A.
- The book should appear on Device B within ~10 seconds.
- Delete a book on Device B.
- It should disappear from Device A within ~10 seconds.
How Sync Works
- User adds/edits/deletes a book on Device A.
- Supabase stores the change with
last_modified_device_id. - A database trigger fires and calls the
send-sync-notificationedge function. - The edge function gets all devices for the user, filters out Device A.
- It sends an FCM push notification to Device B.
- Device B receives the notification, triggers a refresh, and the UI updates.