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

  1. Go to supabase.com and sign up or log in.
  2. Click New Project.
  3. Choose an organization, enter a project name, set a database password, and select a region close to you.
  4. Wait for the project to finish provisioning.

Step 2: Get Your API Credentials

  1. In your Supabase dashboard, go to Settings → API.
  2. Copy the Project URL (e.g., https://your-project.supabase.co).
  3. 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:

  1. Go to Edge Functions → Secrets.
  2. Add the following secrets:
    • FIREBASE_PROJECT_ID — Your Firebase project ID (e.g., tsumihon-12345)
    • FIREBASE_CLIENT_EMAIL — Service account email from the Firebase Console
    • FIREBASE_PRIVATE_KEY — The full private key, including BEGIN and END lines

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:

  1. Open the app on two devices logged into the same account.
  2. Add a book on Device A.
  3. The book should appear on Device B within ~10 seconds.
  4. Delete a book on Device B.
  5. It should disappear from Device A within ~10 seconds.

How Sync Works

  1. User adds/edits/deletes a book on Device A.
  2. Supabase stores the change with last_modified_device_id.
  3. A database trigger fires and calls the send-sync-notification edge function.
  4. The edge function gets all devices for the user, filters out Device A.
  5. It sends an FCM push notification to Device B.
  6. Device B receives the notification, triggers a refresh, and the UI updates.
Tip: If sync isn't working, check the Edge Function logs in your Supabase dashboard for messages like "Processing INSERT notification for book..." and "Sent X/Y notifications successfully".