c/backend › command
0votes

Cursor rules for Supabase

Template Tested
submitted by CommandVault editors to c/backend · 1 use

A .cursor/rules/supabase.mdc covering row-level security, migrations, auth and safe client usage.

Template · 1 variable
---
description: Supabase conventions
globs: ["supabase/**","src/**/*.ts","app/**/*.ts"]
alwaysApply: false
---

# Supabase

- Every table: `enable row level security` plus explicit policies in the same migration. Use `(select auth.uid())` in policies.
- Schema changes only through migrations in `supabase/migrations` (`supabase migration new <name>`); never edit the remote database by hand.
- The anon key is public; the service-role key is server-only and never logged or sent to the browser.
- Security definer functions set `search_path = ''` and are revoked from anon/authenticated unless meant to be called.
- Index every foreign key and every column used in filters or sorting.
- Generate types with `supabase gen types typescript` after schema changes.

## When you change code
- Follow the patterns in the nearest existing file before inventing new ones.
- Add or update a test for behavior changes; run `{{test_command}}`.
- Keep diffs small; explain anything surprising in your reply.
0 commentsreport

Guide

What you get: New tables arrive in a supabase/migrations file with RLS enabled and explicit policies using (select auth.uid()), an index on the foreign key, user-scoped client code (no service-role key) and tests. Best for Supabase developers who want agents to stop shipping tables without RLS..

Before you start

  • A project with supabase/ initialised (supabase init) and the Supabase CLI
  • Fill test_command (we used npm test with Vitest)
  • For type generation the agent needs a running local stack (supabase start) or a linked project
  • Cursor: .cursor/rules/supabase.mdc. Claude Code/Codex: the same text in CLAUDE.md / AGENTS.md

Worked example (Nov 14, 2026, Claude Code 2.1.318 (claude-opus-5-5); Codex 0.151.0 (gpt-5.6-sol))

Input

A TypeScript project with one existing migration (profiles with RLS), a userClient(accessToken) helper and generated types. Task: bookmarks (URL, title, optional note) that each signed-in user can add, list newest first and delete; nobody sees anyone else's.

Result

Both: `supabase migration new ...`, `create policy ... for select to authenticated using ((select auth.uid()) = user_id)` plus insert and delete policies, index on (user_id, created_at desc), and src/bookmarks.ts using the user's client.

Not tested in Cursor or Copilot: the rule text was saved verbatim (front matter included) as CLAUDE.md for Claude Code and AGENTS.md for Codex, the files those agents load at startup, plus a copy at its normal path. We applied each migration to Postgres 16 with a Supabase-like stub (anon/authenticated roles, auth.uid(), Supabase default grants): user B saw 0 of A's rows, deleted 0, updated 0, inserting a row owned by A failed with an RLS error, anon saw nothing (Codex also revoked anon grants). Tests and tsc passed. Neither could run `supabase gen types` (no local stack/Docker in the session); both edited the generated types file by hand and said so.

How to check it worked

  • Apply the migration locally and query as two different users
  • Check every new table has enable row level security and a policy per operation
  • Regenerate types with supabase gen types once a local stack is running

Common problems

  • supabase gen types needs a running stack or linked project; agents fall back to hand-editing the generated file
  • Policies missing for update are fine only if updates aren't meant to happen; say so in the task

How to add this Cursor rule

  1. Fill in the blank above; Copy puts your answers into the file.
  2. Save it in .cursor/rules/ with the .mdc extension (plain .md files there are ignored), e.g. .cursor/rules/nextjs.mdc.
  3. The front matter decides when it applies: alwaysApply: true for every chat, globs for matching files, or a description so Cursor applies it when relevant.
  4. Check it in Cursor's Rules settings, then ask something about a matching file and confirm the answer follows the rule.
  5. Careful: this one is marked medium risk. Review what the AI suggests before you run it on real systems or data.
  • {{test_command}} Test command

Checked against Cursor docs: rules.

More from c/backend

See all of c/backend →
0 comments

Log in to join the discussion.