supabase-usage

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Supabase 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:
  • mcp__supabase__list_tables
    - List all tables in the database
  • mcp__supabase__get_table_schema
    - Get schema for a specific table
  • mcp__supabase__execute_sql
    - Run read-only SQL queries
Workflow:
  1. Start with
    list_tables
    to understand database structure
  2. Use
    get_table_schema
    to inspect columns and types
  3. Use
    execute_sql
    for custom queries (read-only)

可用于数据库探索的工具:
  • mcp__supabase__list_tables
    - 列出数据库中的所有表
  • mcp__supabase__get_table_schema
    - 获取指定表的Schema
  • mcp__supabase__execute_sql
    - 运行只读SQL查询
工作流程:
  1. 先使用
    list_tables
    了解数据库结构
  2. 使用
    get_table_schema
    检查列与数据类型
  3. 使用
    execute_sql
    执行自定义查询(只读)

Best Practices

最佳实践

DO

建议做法

  • ✓ Enable RLS on all public tables
  • ✓ Use
    (select auth.uid())
    in RLS policies for performance
  • ✓ Add indexes on RLS-checked columns
  • ✓ Specify roles with
    TO authenticated
    in policies
  • ✓ Use
    on delete cascade
    for foreign keys to auth.users
  • ✓ Use cursor-based pagination for large datasets
  • ✓ Select only needed columns:
    .select('id, name')
    not
    .select('*')
  • ✓ 为所有公开表启用RLS
  • ✓ 在RLS策略中使用
    (select auth.uid())
    以提升性能
  • ✓ 为RLS检查的列添加索引
  • ✓ 在策略中指定角色(如
    TO authenticated
  • ✓ 为关联
    auth.users
    的外键设置
    on delete cascade
  • ✓ 对大型数据集使用基于游标的分页
  • ✓ 仅选择所需列:使用
    .select('id, name')
    而非
    .select('*')

DON'T

避免做法

  • ✗ Store sensitive data without RLS
  • ✗ Use
    auth.uid()
    directly in policies (use
    (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

常用过滤条件

FilterJavaScriptPython
Equals
.eq('col', val)
.eq("col", val)
Not equals
.neq('col', val)
.neq("col", val)
Greater than
.gt('col', val)
.gt("col", val)
Greater or equal
.gte('col', val)
.gte("col", val)
Less than
.lt('col', val)
.lt("col", val)
Less or equal
.lte('col', val)
.lte("col", val)
Pattern match
.ilike('col', '%val%')
.ilike("col", "%val%")
In list
.in('col', [a,b])
.in_("col", [a,b])
Is null
.is('col', null)
.is_("col", "null")
OR
.or('a.eq.1,b.eq.2')
.or_("a.eq.1,b.eq.2")
过滤类型JavaScriptPython
等于
.eq('col', val)
.eq("col", val)
不等于
.neq('col', val)
.neq("col", val)
大于
.gt('col', val)
.gt("col", val)
大于等于
.gte('col', val)
.gte("col", val)
小于
.lt('col', val)
.lt("col", val)
小于等于
.lte('col', val)
.lte("col", val)
模式匹配
.ilike('col', '%val%')
.ilike("col", "%val%")
包含在列表中
.in('col', [a,b])
.in_("col", [a,b])
为空
.is('col', null)
.is_("col", "null")
.or('a.eq.1,b.eq.2')
.or_("a.eq.1,b.eq.2")

Auth Tables Quick Reference

认证表速查

TableKey Columns
auth.users
id, email, phone, created_at, last_sign_in_at, raw_user_meta_data
auth.sessions
id, user_id, created_at, updated_at
auth.identities
id, user_id, provider, identity_data
表名关键列
auth.users
id, email, phone, created_at, last_sign_in_at, raw_user_meta_data
auth.sessions
id, user_id, created_at, updated_at
auth.identities
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/update

sql
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/update

Additional Resources

额外资源

For detailed patterns and code examples, consult:
  • references/auth.md
    - Authentication with JS/Python SDK, user profiles
  • references/rls.md
    - Row Level Security policies and performance tips
  • references/relationships.md
    - Table relationships and nested queries
  • references/query-patterns.md
    - Filtering, pagination, counting, indexes
如需详细模式和代码示例,请参考:
  • references/auth.md
    - 使用JS/Python SDK进行身份验证、用户配置文件
  • references/rls.md
    - 行级安全(RLS)策略与性能优化技巧
  • references/relationships.md
    - 表关系与嵌套查询
  • references/query-patterns.md
    - 过滤、分页、计数、索引