Supabase 作为一个开源的 Firebase 替代品,提供了数据库、认证、实时订阅等强大功能。本文将分享一些实用的开发技巧和最佳实践。
1. 数据库设计技巧
-
RLS (Row Level Security) 最佳实践
- 始终启用 RLS
- 为每张表设置合适的策略
sql -- 示例:只允许用户访问自己的数据 create policy "Users can only access their own data" on todos for all using (auth.uid() = user_id);
-
关系设计
- 善用外键约束
- 使用 junction tables 处理多对多关系
- 合理使用级联删除
-
索引优化
- 为常用查询字段创建索引
- 使用部分索引减少索引大小
- 注意复合索引的列顺序
2. 认证与授权
-
自定义认证流程
typescript const { user, error } = await supabase.auth.signUp({ email: 'user@example.com', password: 'password', options: { data: { first_name: 'John', last_name: 'Doe' } } })
-
OAuth 集成
- 支持 Google、GitHub、Discord 等
- 配置回调 URL
- 处理用户元数据
-
角色管理
- 使用 custom claims
- 实现基于角色的访问控制
- 动态权限管理
3. 实时功能开发
-
实时订阅
typescript const subscription = supabase .channel('custom-channel') .on( 'postgres_changes', { event: '*', schema: 'public', table: 'messages' }, (payload) => { console.log('Change received!', payload) } ) .subscribe()
-
优化实时性能
- 合理使用过滤条件
- 及时清理不需要的订阅
- 处理断线重连
4. 存储优化
-
文件上传
typescript const { data, error } = await supabase.storage .from('bucket-name') .upload('file-path', file, { cacheControl: '3600', upsert: false })
-
文件访问控制
- 设置存储桶策略
- 生成签名 URL
- 控制文件访问权限
5. 性能优化技巧
-
查询优化
typescript // 使用 select() 只获取需要的字段 const { data } = await supabase .from('posts') .select('id, title, user:user_id(name)') .eq('status', 'published') .order('created_at', { ascending: false }) .limit(10)
-
批量操作
- 使用 upsert 批量更新
- 事务处理
- 优化大数据集操作
-
缓存策略
- 使用客户端缓存
- 实现数据预加载
- 合理设置缓存时间
6. 开发工具集成
-
类型安全
typescript // 使用 supabase-js-v2 类型生成 supabase gen types typescript --project-id your-project-id > types/supabase.ts
-
开发环境配置
- 本地开发设置
- 环境变量管理
- CI/CD 集成
7. 监控和调试
-
日志管理
- 使用 Supabase 仪表板
- 设置告警规则
- 错误追踪
-
性能监控
- 查询性能分析
- 资源使用监控
- 用户会话追踪
实用代码片段
-
带重试的数据获取
typescript async function fetchWithRetry( fn: () => Promise<any>, retries = 3 ) { try { return await fn() } catch (error) { if (retries > 0) { await new Promise(resolve => setTimeout(resolve, 1000)) return fetchWithRetry(fn, retries - 1) } throw error } }
-
批量更新优化
typescript async function batchUpsert( table: string, data: any[], batchSize = 1000 ) { for (let i = 0; i < data.length; i += batchSize) { const batch = data.slice(i, i + batchSize) await supabase.from(table).upsert(batch) } }
总结
Supabase 提供了强大的功能集,合理使用这些特性可以大大提高开发效率。建议:
- 始终关注安全性,正确配置 RLS
- 优化数据库查询和实时订阅
- 实现适当的错误处理和重试机制
- 保持代码类型安全
- 监控应用性能
注意事项:
- 定期备份重要数据
- 关注 Supabase 版本更新
- 遵循数据保护相关法规
- 合理规划数据库结构