Supabase
supabaseBackend51 lines
Postgres database, authentication, storage and realtime behind one client.
Current — as published in v1.3.2. Written by hand for Supabase, not generated.
The rule
Access
- One client instance, in
src/services/supabase/client.ts. Never callcreateClientanywhere else. - Components never import
@supabase/supabase-js. They call a service insrc/services/, which owns the query. - Every query is typed from the generated
database.types.ts. Regenerate it after a schema change; do not hand-edit it.
Every response has an error
ts
const { data, error } = await supabase.from('profiles').select('id, name');
if (error) throw new Error(`Failed to load profiles: ${error.message}`);The client does not throw. An unchecked error silently reads as an empty result, which is the single most common Supabase bug.
Queries
- Select the columns you need.
select('*')fetches everything and breaks the day someone adds a large column. - Paginate with
.range()for anything unbounded. - Filter in the query, not in JavaScript after fetching.
- Any column used in a filter or join needs an index.
Security
- RLS enabled on every table. No exceptions. The anon key is public — it ships inside the app — so RLS is the only thing protecting the data.
- The
service_rolekey never appears in client code, in anEXPO_PUBLIC_*variable, or in this repository. It bypasses every policy. - If a query needs privileges the user does not have, the answer is an edge function or an RLS policy — never a more powerful key in the client.
Schema
- Schema changes are migration files in
supabase/migrations/, committed to git. Never change the schema through the dashboard: environments drift and the divergence only shows up during a release. - Migrations must be compatible with the currently deployed app version, so a rollback does not corrupt data.
Auth
- Initialise auth state once at startup and subscribe to
onAuthStateChange. - Never read a user id from client state for an authorisation decision — the policy uses
auth.uid(), which the client cannot forge.
As each tool receives it
The same guidance, in the shape each editor reads. Cursor and Claude Code are written separately by hand; the rest are derived from the Cursor rule.
.cursor/rules/supabase.mdc
---
description: Supabase conventions
globs: ["src/services/**/*.ts", "supabase/**/*.sql"]
alwaysApply: false
---
# Supabase
## Access
- One client instance, in `src/services/supabase/client.ts`. Never call
`createClient` anywhere else.
- Components never import `@supabase/supabase-js`. They call a service in
`src/services/`, which owns the query.
- Every query is typed from the generated `database.types.ts`. Regenerate it
after a schema change; do not hand-edit it.
## Every response has an error
```ts
const { data, error } = await supabase.from('profiles').select('id, name');
if (error) throw new Error(`Failed to load profiles: ${error.message}`);
```
The client does not throw. An unchecked `error` silently reads as an empty
result, which is the single most common Supabase bug.
## Queries
- Select the columns you need. `select('*')` fetches everything and breaks the
day someone adds a large column.
- Paginate with `.range()` for anything unbounded.
- Filter in the query, not in JavaScript after fetching.
- Any column used in a filter or join needs an index.
## Security
- **RLS enabled on every table.** No exceptions. The anon key is public — it
ships inside the app — so RLS is the only thing protecting the data.
- The `service_role` key never appears in client code, in an `EXPO_PUBLIC_*`
variable, or in this repository. It bypasses every policy.
- If a query needs privileges the user does not have, the answer is an edge
function or an RLS policy — never a more powerful key in the client.
## Schema
- Schema changes are migration files in `supabase/migrations/`, committed to
git. Never change the schema through the dashboard: environments drift and
the divergence only shows up during a release.
- Migrations must be compatible with the currently deployed app version, so a
rollback does not corrupt data.
## Auth
- Initialise auth state once at startup and subscribe to `onAuthStateChange`.
- Never read a user id from client state for an authorisation decision — the
policy uses `auth.uid()`, which the client cannot forge.
What else this module writes
Selecting Supabase contributes more than a rule file — all of it merged with every other module you pick, with conflicts resolved rather than duplicated.
Environment
EXPO_PUBLIC_SUPABASE_URLrequiredProject URL from Project Settings → API.EXPO_PUBLIC_SUPABASE_ANON_KEYrequiredPublic anon key. Safe in the client only because RLS is enabled.SUPABASE_SERVICE_ROLE_KEYoptionalServer-side only. Bypasses all RLS. Never expose to the client.SUPABASE_PROJECT_REFoptionalProject reference used by the CLI for migrations.Dependencies
@supabase/supabase-js^2.112.0react-native-url-polyfill^4.0.0supabase^2.111.0dev
Folders
src/services/supabase/supabase/migrations/supabase/functions/
Checklists
checklists/supabase-security.md
Related
Put this in your repo
Adds Supabase to a project this tool generated, without starting over. Files you have hand-edited are left alone.