Cyber Monday: 2XCYBER doubles your top-up, up to $2,000.Double my top-up

nc://supabase

v0.1.0Pinned
Backnodecode-ai
ThomSharktools: add semgrep, supabase, stripe, docker and kubectl cells (#186)c504079· 2 Dec1 commit
test1 filecell.lisp6.8 KBsql.lisp7.9 KBtools.lisp22.4 KBwire.lisp15.3 KB

nodecode-supabase

A Nodecode cell: your Supabase projects, tables, SQL, row selects, migrations and logs as tools the model calls. Read-only unless you turn writes on.

Written for Nodecode from Supabase's documented Management API and PostgREST's query syntax; no code of Supabase's is in it. MIT.

Tools

Tool What it does
supabase_projects Every project the access token reaches: ref, name, region, status, Postgres version, organization, created date.
supabase_tables A project's tables in the schemas asked for (public by default): schema, name, kind, row level security on and forced, the planner's row estimate, size on disk.
supabase_query One SQL statement. A read goes to the read-only endpoint (POST /v1/projects/{ref}/database/query/read-only), which runs it as the role supabase_read_only_user; name tables with their schema (public.orders). A statement that changes anything needs write: true in the call and allow_writes in the section, and then goes to POST /v1/projects/{ref}/database/query with read_only: false.
supabase_select Rows of one table or view of the section's project through its Data API (PostgREST GET /rest/v1/{table}): columns, filters (eq, neq, gt, gte, lt, lte, like, ilike, is, in, ANDed), order, limit (up to max_rows), offset, and schema (sent as Accept-Profile). It runs as the API key's role, so a publishable key keeps row level security on.
supabase_migrations The migrations applied to a project's database, newest first: version and name.
supabase_logs Recent log entries of one source (edge_logs by default; postgres_logs, auth_logs, function_logs, function_edge_logs, storage_logs, realtime_logs, postgrest_logs, supavisor_logs), newest first, optionally holding a search text, over the last minutes (60 by default, at most 1440), from GET /v1/projects/{ref}/analytics/endpoints/logs. Or your own ClickHouse sql over the logs table in the same window.

Every tool but supabase_select takes an optional project_ref; without it the section's project_ref is used. /supabase checks the access token by listing the projects it reaches, and says whether the Data API key is set and whether writes are allowed. (help :supabase) gives the model the workflow.

Setup

  1. Create a personal access token at https://supabase.com/dashboard/account/tokens and export it as SUPABASE_ACCESS_TOKEN.
  2. For supabase_select, set project_ref and export the project's publishable key (dashboard, Project Settings, API Keys) as SUPABASE_KEY.
  3. Add a supabase section to ~/.nodecode/config.jsonc (see config.example.jsonc) and run /supabase.

Settings

Setting Default Meaning
base_url https://api.supabase.com Where the Management API answers.
access_token_env SUPABASE_ACCESS_TOKEN The environment variable holding the personal access token.
access_token none A token written in the config instead; the environment variable wins.
project_ref empty The default project, twenty lowercase letters. supabase_select always reads this one.
project_url empty Where the project's Data API answers; https://<project_ref>.supabase.co when empty.
api_key_env SUPABASE_KEY The environment variable holding the project's API key for the Data API.
api_key none A key written in the config instead; the environment variable wins.
allow_writes false Let supabase_query run a statement that changes anything when the call passes write: true.
timeout 60 One API call's ceiling, in seconds.
max_rows 100 The most rows, log entries or migrations one answer carries (and supabase_select's default and largest limit).
max_chars 30000 How many characters one answer carries in all.

Safety

  • Reads are read-only on the server. The guarantee is Supabase's read-only endpoint, which runs the statement as supabase_read_only_user. In front of it the cell reads the SQL the way PostgreSQL's lexer does (identifiers and dollar-quote tags with non-ASCII characters, $ inside identifiers, E'...' escapes, nested block comments) and refuses a call holding more than one statement, a string, quote or comment left open, an empty statement, a NUL, or a statement whose first keyword is not SELECT, WITH, VALUES, TABLE, EXPLAIN or SHOW unless the call writes. The keyword is the whole first word as PostgreSQL reads it (letters, digits, _, $), so SELECT_1 is not SELECT.
  • Writes need two switches: write: true in the call and allow_writes in the section. With either off, a statement goes only to the read-only endpoint, or is refused before anything is sent.
  • The keys ride headers only. The access token goes only to the Management API as Authorization: Bearer; the project's API key goes only to the Data API as apikey (and as Authorization: Bearer when it is a legacy JWT key). Neither ever rides a URL or a body, and every answer and error has both replaced by [redacted]; the replacement resumes past each match, so no key can make it loop, and an earlier [redacted] is left whole. Text is scrubbed before any cut (max_chars, a 300-character table cell or error body), so a key the cut would split leaves no prefix.
  • No key leaves for the wrong host. base_url and project_url must be plain https://host[:port] addresses, or http:// only to this machine (localhost, 127.0.0.0/8, ::1); userinfo (user@), backslashes, blanks and control characters, a query, a fragment and a malformed port are refused, and the scheme is read in any case. Where Nodecode's HTTP client allows it no redirect is followed, so a key reaches only the host named.
  • Nothing a call carries becomes syntax. Project refs must be twenty lowercase letters. Tables, schemas, columns and order keys must be plain names (letters, digits, _, $; not opening with a digit or a dash), and a filter cannot name a PostgREST parameter (select, order, limit, offset, and, or, not, on_conflict, columns). Filter values are percent-encoded, in lists are double-quoted with their quotes and backslashes escaped, is takes only null, true, false or unknown. supabase_tables schemas become SQL string literals; the log search becomes an escaped ClickHouse literal.
  • Answers are bounded: at most max_rows rows and max_chars characters, with the full count said.

Add nc://supabase

  1. 1

    Install Nodecode

    Once per machine.

    $ curl -fsSL https://nodecode.ai/install.sh | sh
  2. 2

    Add the cell

    Pinned at c504079.

    $ nodecode add nc://supabase

    Or tell the agent add the supabase cell.