mcpField
S
supabase-database

supabase-database

github.com/nice-wolf-studioデータ・データベース19299 回インストール53 週間前に更新
リポジトリ

supabase-database について

REST API 経由で Supabase データベースを操作する開発者向けのスキルです。共有 API ヘルパーを使ったテーブル操作に加え、RPC、フィルター、バッチ処理、エラー処理を扱います。

主な機能

  1. 01テーブルデータの検索とフィルタリング
  2. 02レコードの挿入、更新、削除
  3. 03データベース RPC 関数の呼び出し
  4. 04並べ替え、件数制限、バッチ処理
  5. 05エラー処理とセキュリティ上の注意

活用シーン

  1. 01条件を指定したレコード検索と並べ替え
  2. 02レコードの一括更新や削除
  3. 03計算結果を得るためのストアドプロシージャ呼び出し

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/nice-wolf-studio/claude-code-supabase-skills --skill supabase-database

インストール後、Claude Code で/supabase-databaseを入力して読み込まれたか確認します。

詳細

Supabase データベース操作

概要

このスキルは、REST API を通じて Supabase データベースのテーブルを操作するためのツールを提供します。フィルタリングを使った SELECT クエリ、INSERT、UPDATE、DELETE 操作、および RPC 関数の呼び出しに対応しています。

前提条件

必須の環境変数:

export SUPABASE_URL="https://your-project.supabase.co"
export SUPABASE_KEY="your-anon-or-service-role-key"

ヘルパースクリプト: このスキルでは、共有の Supabase API ヘルパーを使用します。必ずソースしてください:

source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"

一般的な操作

SELECT - データのクエリ

すべてのデータを選択:

source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"

# Get all rows from a table
supabase_get "/rest/v1/your_table?select=*"

特定の列を選択:

# Get only id and name columns
supabase_get "/rest/v1/users?select=id,name,email"

結果をフィルタリング:

# Equality filter
supabase_get "/rest/v1/users?select=*&[email protected]"

# Greater than
supabase_get "/rest/v1/products?select=*&price=gt.100"

# Less than or equal
supabase_get "/rest/v1/orders?select=*&quantity=lte.10"

# Pattern matching (LIKE)
supabase_get "/rest/v1/users?select=*&name=like.*John*"

# In list
supabase_get "/rest/v1/products?select=*&category=in.(electronics,books)"

# Is null
supabase_get "/rest/v1/users?select=*&deleted_at=is.null"

並べ替えと件数制限:

# Order by column (ascending)
supabase_get "/rest/v1/posts?select=*&order=created_at.asc"

# Order by column (descending)
supabase_get "/rest/v1/posts?select=*&order=created_at.desc"

# Limit results
supabase_get "/rest/v1/posts?select=*&limit=10"

# Pagination (offset)
supabase_get "/rest/v1/posts?select=*&limit=10&offset=20"

# Range pagination
supabase_get "/rest/v1/posts?select=*" -H "Range: 0-9"

複雑なクエリ:

# Multiple filters (AND)
supabase_get "/rest/v1/products?select=*&category=eq.electronics&price=gt.100"

# OR filter
supabase_get "/rest/v1/users?select=*&or=(status.eq.active,status.eq.pending)"

# Nested filters
supabase_get "/rest/v1/users?select=*,posts(*)&posts.published=eq.true"
INSERT - データの追加

単一の行を挿入:

source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"

supabase_post "/rest/v1/users" '{
  "name": "John Doe",
  "email": "[email protected]",
  "age": 30
}'

複数の行を挿入:

supabase_post "/rest/v1/users" '[
  {
    "name": "Alice Smith",
    "email": "[email protected]"
  },
  {
    "name": "Bob Jones",
    "email": "[email protected]"
  }
]'

Upsert(存在する場合は挿入または更新):

# Use Prefer: resolution=merge-duplicates header
curl -s -X POST \
    "${SUPABASE_URL}/rest/v1/users" \
    -H "apikey: ${SUPABASE_KEY}" \
    -H "Authorization: Bearer ${SUPABASE_KEY}" \
    -H "Content-Type: application/json" \
    -H "Prefer: resolution=merge-duplicates" \
    -d '{
      "id": 1,
      "name": "Updated Name",
      "email": "[email protected]"
    }'
UPDATE - データの変更

フィルターに一致する行を更新:

source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"

# Update specific row by id
supabase_patch "/rest/v1/users?id=eq.123" '{
  "name": "Updated Name",
  "email": "[email protected]"
}'

# Update multiple rows
supabase_patch "/rest/v1/products?category=eq.electronics" '{
  "discount": 10
}'
DELETE - データの削除

フィルターに一致する行を削除:

source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"

# Delete specific row by id
supabase_delete "/rest/v1/users?id=eq.123"

# Delete multiple rows
supabase_delete "/rest/v1/logs?created_at=lt.2023-01-01"
RPC - データベース関数の呼び出し

ストアドプロシージャを実行:

source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"

# Call function without parameters
supabase_post "/rest/v1/rpc/function_name" '{}'

# Call function with parameters
supabase_post "/rest/v1/rpc/calculate_total" '{
  "user_id": 123,
  "start_date": "2023-01-01",
  "end_date": "2023-12-31"
}'

フィルター演算子のリファレンス

演算子説明例
eq等しいid=eq.123
neq等しくないstatus=neq.deleted
gtより大きいage=gt.18
gte以上price=gte.100
ltより小さいquantity=lt.10
lte以下score=lte.50
likeパターン一致(大文字と小文字を区別)name=like.*John*
ilikeパターン一致(大文字と小文字を区別しない)email=ilike.*@gmail.com
is値(null、true、false)との完全一致を確認deleted_at=is.null
inリスト内にあるstatus=in.(active,pending)
not条件を否定status=not.in.(deleted,banned)
or論理 ORor=(status.eq.active,status.eq.pending)
and論理 ANDand=(age.gte.18,age.lte.65)

レスポンスの整形

JSON を整形して出力(jq が必要):

supabase_get "/rest/v1/users?select=*" | jq '.'

特定のフィールドを抽出:

# Get just the names
supabase_get "/rest/v1/users?select=name" | jq -r '.[].name'

結果の件数を取得:

# Add Prefer: count=exact header for total count
curl -s -X GET \
    "${SUPABASE_URL}/rest/v1/users?select=*" \
    -H "apikey: ${SUPABASE_KEY}" \
    -H "Authorization: Bearer ${SUPABASE_KEY}" \
    -H "Prefer: count=exact" \
    -I | grep -i content-range

一般的なパターン

レコードが存在するか確認
result=$(supabase_get "/rest/v1/users?select=id&[email protected]")
if [[ "$result" == "[]" ]]; then
    echo "User does not exist"
else
    echo "User exists"
fi
存在しない場合に作成
# Check first
existing=$(supabase_get "/rest/v1/users?select=id&[email protected]")

if [[ "$existing" == "[]" ]]; then
    # Create new user
    supabase_post "/rest/v1/users" '{
        "email": "[email protected]",
        "name": "Test User"
    }'
    echo "User created"
else
    echo "User already exists"
fi
バッチ操作
# Process multiple records
ids=(123 456 789)

for id in "${ids[@]}"; do
    supabase_patch "/rest/v1/users?id=eq.$id" '{
        "updated_at": "'"$(date -u +%Y-%m-%dT%H:%M:%SZ)"'"
    }'
done

エラー処理

ヘルパースクリプトは HTTP エラーを自動的に処理して表示します。戻り値を確認してください:

if supabase_get "/rest/v1/users?select=*"; then
    echo "Query successful"
else
    echo "Query failed"
    exit 1
fi

セキュリティに関する注意

  • クライアント側の操作には anon key を使用してください(Row Level Security が適用されます)
  • 管理者操作には service role key を使用してください(RLS を回避します。慎重に使用してください)
  • Supabase ダッシュボードで必ず Row Level Security ポリシーを適用してください
  • キーをバージョン管理にコミットしないでください

API ドキュメント

Supabase REST API の完全なドキュメント:https://supabase.com/docs/guides/api

コメント