mcpField
S
supabase-cli

supabase-cli

github.com/adaptationio数据与数据库55 次安装
代码仓库

关于 supabase-cli

supabase-cli 是提供 Supabase 开发命令参考的 skill,面向使用命令行管理项目的开发者。它覆盖本地环境、数据库迁移、类型生成、Edge Functions 和项目操作。

主要功能

  1. 01启动并管理本地服务栈
  2. 02创建并应用迁移
  3. 03生成 TypeScript 类型
  4. 04运行并部署 Edge Functions
  5. 05检查、转储和恢复数据库

适用场景

  1. 01基于本地 Supabase 服务栈开发
  2. 02在本地与远程数据库间同步架构变更
  3. 03为 TypeScript 项目生成数据库类型
  4. 04部署并测试 Edge Functions

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/adaptationio/skrillz --skill supabase-cli

安装后在 Claude Code 中输入/supabase-cli确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
config-toml.md
local-development.md
migrations.md

详细介绍

Supabase CLI 技能

Supabase 开发的完整 CLI 命令参考。

快速参考

任务命令
安装 CLInpm install supabase --save-dev
登录supabase login
初始化项目supabase init
链接到远程项目supabase link --project-ref <ref>
启动本地服务栈supabase start
停止本地服务栈supabase stop
检查状态supabase status
创建迁移supabase migration new <name>
推送到远程项目supabase db push
从远程项目拉取supabase db pull
重置本地数据库supabase db reset
生成类型supabase gen types typescript --local > types.ts

安装

# NPM (recommended for projects)
npm install supabase --save-dev
npx supabase [command]

# Homebrew (macOS/Linux)
brew install supabase/tap/supabase

# Scoop (Windows)
scoop bucket add supabase https://github.com/supabase/scoop-bucket.git
scoop install supabase

前置条件:Docker(本地开发所需)

项目设置

初始化项目
supabase init

创建:

  • supabase/config.toml - 配置文件
  • supabase/migrations/ - 迁移文件
  • supabase/seed.sql - 种子数据
链接到远程项目
supabase link --project-ref <project-ref>

从以下位置获取项目引用:Dashboard → Project Settings → General

本地开发

启动本地服务栈
supabase start

启动:

  • PostgreSQL 数据库
  • PostgREST API
  • GoTrue Auth
  • Realtime 服务
  • Storage 服务
  • Studio 仪表板
  • Inbucket(电子邮件测试)

输出 URL:

API URL: http://localhost:54321
DB URL: postgresql://postgres:postgres@localhost:54322/postgres
Studio URL: http://localhost:54323
Inbucket URL: http://localhost:54324
停止本地服务栈
# Stop and save data
supabase stop

# Stop and delete all data
supabase stop --no-backup
检查状态
supabase status

显示 URL、API 密钥和服务状态。

数据库命令

创建迁移
supabase migration new create_users_table

创建:supabase/migrations/<timestamp>_create_users_table.sql

根据更改生成迁移
# Make changes locally, then generate migration
supabase db diff -f my_schema_changes

# Diff against specific schemas
supabase db diff -f changes --schema public,extensions
应用迁移
# Apply pending migrations locally
supabase migration up

# Push to remote
supabase db push

# Preview what would be pushed
supabase db push --dry-run
拉取远程更改
supabase db pull

根据 Dashboard 中的更改创建迁移。

重置本地数据库
supabase db reset

重新创建数据库并应用所有迁移。

列出迁移
supabase migration list

显示本地与远程迁移状态。

回滚迁移
# Rollback last n migrations
supabase migration down --count 1

# Rollback specific number
supabase migration down --count 3
合并迁移
# Combine multiple migrations into one
supabase migration squash --version 20240101000000

# Squash all migrations up to version
supabase migration squash --version <target_version>

适用于在发布前整理迁移历史。

类型生成

生成 TypeScript 类型
# From local database
supabase gen types typescript --local > database.types.ts

# From linked remote
supabase gen types typescript --linked > database.types.ts

# From specific project
supabase gen types typescript --project-id "your-id" > database.types.ts

Edge Functions

创建函数
supabase functions new hello-world

创建:supabase/functions/hello-world/index.ts

在本地运行
# Serve all functions
supabase functions serve

# Serve specific function
supabase functions serve hello-world

# With env file and no JWT
supabase functions serve --env-file .env --no-verify-jwt
部署
# Deploy all functions
supabase functions deploy

# Deploy specific function
supabase functions deploy hello-world

# Deploy without JWT verification (for webhooks)
supabase functions deploy webhook-handler --no-verify-jwt
测试函数
curl -i --request POST \
  'http://localhost:54321/functions/v1/hello-world' \
  --header 'Authorization: Bearer <ANON_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{"name":"Functions"}'

密钥管理

# Set secret
supabase secrets set API_KEY=abc123

# Set multiple secrets
supabase secrets set API_KEY=abc123 DB_PASSWORD=secret

# Set from .env file
supabase secrets set --env-file .env

# List secrets
supabase secrets list

# Remove secret
supabase secrets unset API_KEY

项目管理

# List organizations
supabase orgs list

# List projects
supabase projects list

# Create project
supabase projects create <name> --org-id <id> --region <region>

# Delete project
supabase projects delete --project-ref <ref>

# Update remote config from local
supabase projects update-config

数据库检查

# Cache hit ratio
supabase inspect db cache-hit --linked

# Unused indexes
supabase inspect db unused-indexes --local

# Table bloat
supabase inspect db bloat --linked

# Long running queries
supabase inspect db long-running-queries --linked

# Active connections
supabase inspect db role-connections --linked

备份与恢复

备份
# Dump roles
supabase db dump --linked -f roles.sql --role-only

# Dump schema
supabase db dump --linked -f schema.sql

# Dump data
supabase db dump --linked -f data.sql --use-copy --data-only
恢复
psql --single-transaction \
  --file roles.sql \
  --file schema.sql \
  --command 'SET session_replication_role = replica' \
  --file data.sql \
  --dbname [CONNECTION_STRING]

常用标志

标志描述
--debug详细输出
--local使用本地数据库
--linked使用已链接的远程项目
--project-ref <ref>指定项目
--db-url <url>连接字符串
--dry-run预览但不执行
-f <file>输出到文件

参考资料

评论

支持 Markdown