supabase-usage
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSupabase Database Patterns
Supabase数据库模式
Patterns for working with Supabase databases including Auth, Row Level Security, table relationships, and query best practices.
适用于Supabase数据库的实践模式,包括身份验证(Auth)、行级安全(Row Level Security)、表关系及查询最佳实践。
Overview
概述
- MCP Tools: Query and explore database structure
- Authentication: User management, sessions, auth tables
- Row Level Security: Policy patterns for data access control
- Table Relationships: Foreign keys, joins, nested queries
- Query Patterns: Filtering, pagination, performance
- MCP Tools:查询与探索数据库结构
- 身份验证:用户管理、会话、认证表
- 行级安全(RLS):用于数据访问控制的策略模式
- 表关系:外键、连接、嵌套查询
- 查询模式:过滤、分页、性能优化
MCP Tools
MCP Tools
Available tools for database exploration:
- - List all tables in the database
mcp__supabase__list_tables - - Get schema for a specific table
mcp__supabase__get_table_schema - - Run read-only SQL queries
mcp__supabase__execute_sql
Workflow:
- Start with to understand database structure
list_tables - Use to inspect columns and types
get_table_schema - Use for custom queries (read-only)
execute_sql
可用于数据库探索的工具:
- - 列出数据库中的所有表
mcp__supabase__list_tables - - 获取指定表的Schema
mcp__supabase__get_table_schema - - 运行只读SQL查询
mcp__supabase__execute_sql
工作流程:
- 先使用了解数据库结构
list_tables - 使用检查列与数据类型
get_table_schema - 使用执行自定义查询(只读)
execute_sql
Best Practices
最佳实践
DO
建议做法
- ✓ Enable RLS on all public tables
- ✓ Use in RLS policies for performance
(select auth.uid()) - ✓ Add indexes on RLS-checked columns
- ✓ Specify roles with in policies
TO authenticated - ✓ Use for foreign keys to auth.users
on delete cascade - ✓ Use cursor-based pagination for large datasets
- ✓ Select only needed columns: not
.select('id, name').select('*')
- ✓ 为所有公开表启用RLS
- ✓ 在RLS策略中使用以提升性能
(select auth.uid()) - ✓ 为RLS检查的列添加索引
- ✓ 在策略中指定角色(如)
TO authenticated - ✓ 为关联的外键设置
auth.userson delete cascade - ✓ 对大型数据集使用基于游标的分页
- ✓ 仅选择所需列:使用而非
.select('id, name').select('*')
DON'T
避免做法
- ✗ Store sensitive data without RLS
- ✗ Use directly in policies (use
auth.uid())(select auth.uid()) - ✗ Create policies without specifying roles
- ✗ Forget indexes on frequently filtered columns
- ✗ Use offset pagination for deep pages (>1000 rows)
- ✗ Expose auth.users directly via API (use public profiles table)
- ✗ 在未启用RLS的情况下存储敏感数据
- ✗ 在策略中直接使用(应使用
auth.uid())(select auth.uid()) - ✗ 创建未指定角色的策略
- ✗ 忘记为频繁过滤的列添加索引
- ✗ 对深层分页(>1000行)使用偏移量分页
- ✗ 通过API直接暴露表(应使用公开的用户配置文件表)
auth.users
Quick Reference
速查指南
Common Filters
常用过滤条件
| Filter | JavaScript | Python |
|---|---|---|
| Equals | | |
| Not equals | | |
| Greater than | | |
| Greater or equal | | |
| Less than | | |
| Less or equal | | |
| Pattern match | | |
| In list | | |
| Is null | | |
| OR | | |
| 过滤类型 | JavaScript | Python |
|---|---|---|
| 等于 | | |
| 不等于 | | |
| 大于 | | |
| 大于等于 | | |
| 小于 | | |
| 小于等于 | | |
| 模式匹配 | | |
| 包含在列表中 | | |
| 为空 | | |
| 或 | | |
Auth Tables Quick Reference
认证表速查
| Table | Key Columns |
|---|---|
| id, email, phone, created_at, last_sign_in_at, raw_user_meta_data |
| id, user_id, created_at, updated_at |
| id, user_id, provider, identity_data |
| 表名 | 关键列 |
|---|---|
| id, email, phone, created_at, last_sign_in_at, raw_user_meta_data |
| id, user_id, created_at, updated_at |
| id, user_id, provider, identity_data |
RLS Policy Template
RLS策略模板
sql
create policy "policy_name" on table_name
to authenticated -- or anon, or specific role
for select -- select, insert, update, delete, or all
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id ); -- for insert/updatesql
create policy "policy_name" on table_name
to authenticated -- 或anon,或特定角色
for select -- select、insert、update、delete,或all
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id ); -- 适用于insert/updateAdditional Resources
额外资源
For detailed patterns and code examples, consult:
- - Authentication with JS/Python SDK, user profiles
references/auth.md - - Row Level Security policies and performance tips
references/rls.md - - Table relationships and nested queries
references/relationships.md - - Filtering, pagination, counting, indexes
references/query-patterns.md
如需详细模式和代码示例,请参考:
- - 使用JS/Python SDK进行身份验证、用户配置文件
references/auth.md - - 行级安全(RLS)策略与性能优化技巧
references/rls.md - - 表关系与嵌套查询
references/relationships.md - - 过滤、分页、计数、索引
references/query-patterns.md