中文译文
担任资深 Expo + Supabase 架构师。
使用以下组件实现一个“冷启动安全”的架构:
- Expo(React Native)客户端
- Supabase Postgres + Storage + Realtime
- Supabase Edge Functions 仅用于轻量级门控 + 任务入队
- 一个独立的 Worker 服务,用于重型 AI 生成和存储写入
交付内容: 1)数据库 schema(SQL 迁移),用于:jobs、generations、entitlements(credits/is_paid),包括索引和 RLS 说明 2)Edge Functions:
- ping(HEAD/GET)
- enqueue_generation(验证 auth,检查 is_paid/credits,创建 job,返回 jobId)
- get_job_status(轻量读取) 保持导入最小化;不要使用重型 SDK。 3)Expo 客户端流程:
- 应用启动时进行非阻塞的预热 ping
- Generate 按钮使用乐观 UI + 占位符
- 通过 Realtime 订阅 job 更新,或实现轮询回退
- 最终生成结果替换图库列表中的占位符 4)Worker 职责(描述接口和最小端点/逻辑,不要过度构建):
- 获取已排队的 jobs
- 运行 AI 生成
- 上传到 storage
- 更新 jobs + 插入 generations
- 重试策略和幂等性
约束:
- 不要因任何 Edge 调用而阻塞应用启动
- 不要在 Edge Functions 内运行 AI 调用
- 确保失败的 jobs 仍然创建一条 generation 记录,并显示原始输入
- 保持方案生产友好但最小化
输出必须按以下结构组织: A)架构摘要 B)迁移(SQL) C)Edge function 文件结构 + 关键代码块 D)Expo 集成说明 + 关键代码块 E)Worker 大纲 + 伪代码
English original
Act as a Senior Expo + Supabase Architect.
Implement a “cold-start safe” architecture using:
- Expo (React Native) client
- Supabase Postgres + Storage + Realtime
- Supabase Edge Functions ONLY for lightweight gating + job enqueue
- A separate Worker service for heavy AI generation and storage writes
Deliver:
- Database schema (SQL migrations) for: jobs, generations, entitlements (credits/is_paid), including indexes and RLS notes
- Edge Functions:
- ping (HEAD/GET)
- enqueue_generation (validate auth, check is_paid/credits, create job, return jobId)
- get_job_status (light read) Keep imports minimal; no heavy SDKs.
- Expo client flow:
- non-blocking warm ping on app start
- Generate button uses optimistic UI + placeholder
- subscribe to job updates via Realtime or implement polling fallback
- final generation replaces placeholder in gallery list
- Worker responsibilities (describe interface and minimal endpoints/logic, do not overbuild):
- fetch queued jobs
- run AI generation
- upload to storage
- update jobs + insert generations
- retry policy and idempotency
Constraints:
- Do NOT block app launch on any Edge call
- Do NOT run AI calls inside Edge Functions
- Ensure failed jobs still create a generation record with original input visible
- Keep the solution production-friendly but minimal
Output must be structured as: A) Architecture summary B) Migrations (SQL) C) Edge function file structure + key code blocks D) Expo integration notes + key code blocks E) Worker outline + pseudo-code