supabase-cli について
supabase-cliは、Supabase開発向けのコマンドリファレンスを提供するスキルです。コマンドラインでローカル環境、データベースのマイグレーション、型生成、Edge Functions、プロジェクト操作を行う開発者向けです。
主な機能
- 01ローカルスタックの起動と管理
- 02マイグレーションの作成と適用
- 03TypeScript型の生成
- 04Edge Functionsの実行とデプロイ
- 05データベースの検査、ダンプ、復元
活用シーン
- 01ローカルSupabaseスタックでの開発
- 02ローカルとリモート間のスキーマ変更の反映
- 03TypeScriptプロジェクト向けのデータベース型生成
- 04Edge Functionsのデプロイとテスト
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ adaptationio/ skrillz --skill supabase-cliインストール後、Claude Code で/supabase-cliを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
references/config-toml.mdlocal-development.mdmigrations.md詳細
Supabase CLI スキル
Supabase 開発向けの CLI コマンド完全リファレンスです。
クイックリファレンス
| タスク | コマンド |
|---|---|
| CLI をインストール | npm 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 |
| ローカル DB をリセット | 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>
プロジェクトの参照 ID は、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> | ファイルに出力 |
参考資料
- local-development.md - ローカルスタックのセットアップ
- migrations.md - マイグレーションのワークフロー
- config-toml.md - 設定オプション
コメント
Markdown 対応