Supabase CLIを触ってみた
SupabaseのCLIのインストール方法やログイン、ログアウト、管理系APIの使用方法などの基本的な操作方法について説明している記事です。CLIはnpmを使ってインストールすることが推奨されており、WindowsではScoopを使うこともできます。ログインやログアウトはコマンドから行い、Management APIの使用にはログインが必要です。また、CLIには自動補完機能もあり、Bash/Zsh/PowerShellに対応しています。
目次
Supabaseをいろいろと触ってみようと思ったので、一念発起してCLIを入れてみました。
CLIのインストール方法
基本的にはnpmを使うのがよさそうです。
npm i -D supabase
Documentではローカルインストールを推奨されました。コマンドを使う場合、npxを挟んで使う形になりそうです。
$ npx supabase --version
1.123.4
macではHomebrew経由でもインストールできる
Homebrewからもインストールできます。
brew install supabase/tap/supabase
brew install supabase/tap/supabase-beta
brew link --overwrite supabase-beta
WindowsではScoopを使う
Windowsでのインストール方法も提示されているのは、ワークショップをやる際などにも助かりそうです。
scoop bucket add supabase https://github.com/supabase/scoop-bucket.git
scoop install supabase
CLIでSupabaseにログインする
CloudflareやStripeと同様に、細かい操作をするにはログインが必要です。
% npx supabase  login
Hello from Supabase! Press Enter to open browser and login automatically.
ブラウザが立ち上がりますので、ログイン操作などを済ませましょう。次の画面が出ればログイン完了です。

ログインに成功すると、作成済みのプロジェクトなどがみれます。
% npx supabase projects list
  
    LINKED │        ORG ID        │     REFERENCE ID     │          NAME          │         REGION         │  CREATED AT (UTC)
  ─────────┼──────────────────────┼──────────────────────┼────────────────────────┼────────────────────────┼──────────────────────
           │ eairkgncwshmtocyglhu │ vgkufqrgmhctvgzhkxnh │ stripe-nextjs          │ Northeast Asia (Tokyo) │ 2023-09-24 12:47:44
           │ eairkgncwshmtocyglhu │ zcffmbecqgibgossrapl │ flutter-stripe-example │ Northeast Asia (Tokyo) │ 2023-09-19 13:18:16
           │ eairkgncwshmtocyglhu │ ntqsneagxkqxjcehmbhv │ Headless Vector Search │ Northeast Asia (Tokyo) │ 2023-12-26 00:16:52
ログアウトもコマンドから
アカウントを切り替える場合などでは、ログアウトしておきましょう。
 % npx supabase  logout       
Do you want to log out? This will remove the access token from your system. [y/N] 
アクセストークンが消えたよというメッセージが出ればログアウト完了です。
Access token deleted successfully. You are now logged out.
管理系APIを叩くと、「ログインしろ」と言われるようになります。
% npx supabase projects list
You need to be logged-in in order to use Management API commands.
Run supabase login first.
Completionも用意されている
supabase completionも用意されていました。Bash / Zsh / PowerShellなどに対応しているそうです。
 % npx supabase completion      
Generate the autocompletion script for supabase for the specified shell.
See each sub-command's help for details on how to use the generated script.
Usage:
  supabase completion [command]
Available Commands:
  bash        Generate the autocompletion script for bash
  fish        Generate the autocompletion script for fish
  powershell  Generate the autocompletion script for powershell
  zsh         Generate the autocompletion script for zsh
Flags:
  -h, --help   help for completion
Global Flags:
      --debug                             output debug logs to stderr
      --dns-resolver [ native | https ]   lookup domain names using the specified resolver (default native)
      --experimental                      enable experimental features
      --workdir string                    path to a Supabase project directory
Use "supabase completion [command] --help" for more information about a command.
実行してみると、「このスクリプトを使ってね」というテキストが表示されました。設定自体は自分でやる様子ですね。
% npx supabase completion zsh
#compdef supabase
compdef _supabase supabase
# zsh completion for supabase                             -*- shell-script -*-
__supabase_debug()
{
    local file="$BASH_COMP_DEBUG_FILE"
    if [[ -n ${file} ]]; then
        echo "$*" >> "${file}"
    fi
}
_supabase()
{
...