Design a cold-start safe Expo and Supabase architecture
Designs a cold-start safe Expo and Supabase app with light Edge Functions and a separate worker for AI jobs.
You are a senior architect for Expo and Supabase apps.
Task:
Design and implement a cold-start safe architecture for an app whose users start AI generations, with an Expo (React Native) client, Supabase Postgres, Storage and Realtime, light Supabase Edge Functions that only gate requests and enqueue jobs, and a separate worker service for the heavy AI generation and storage writes.
Context:
The solution is for a developer who will build this. The point is that a slow Edge Function start never blocks the app and no AI call runs inside an Edge Function. The app generates [for example images or text], and its credit model is [credits, a paid flag, or both].
Rules:
- Keep the solution production-friendly but minimal and do not overbuild.
- The app launch must not wait for any Edge call.
- A failed job still creates a generation record that shows the original input.
- Part 1: the database schema as SQL migrations for jobs, generations and entitlements (credits and is_paid), with indexes and notes on row-level security.
- Part 2: three Edge Functions with minimal imports and no heavy SDKs: ping (HEAD or GET), enqueue_generation (validate auth, check is_paid and credits, create the job, return the job id) and get_job_status (a light read).
- Part 3: the Expo client flow, with a non-blocking warm-up ping at app start, a Generate button with optimistic UI and a placeholder, job updates through Realtime with a polling fallback, and the finished generation replacing the placeholder in the gallery list.
- Part 4: the worker, described by its interface and minimal endpoints and logic for fetching queued jobs, running the AI generation, uploading to storage, updating the job and inserting the generation, with a retry policy and idempotency.
Result:
Five sections in this order: A, architecture summary; B, migrations in SQL; C, Edge function file structure with key code blocks; D, Expo integration notes with key code blocks; E, worker outline with pseudo-code.