# Supabase: the rules in the database

The framework's recipe connects the starter's requests module to Postgres on Supabase with two files, a migration and an adapter. Row level security repeats the app's permissions policy by policy, so a change the screen hides is refused by the database too, four-eyes included.

Checked 1 December 2026. By Bohuslav Poláček. https://meadowstack.com/integrations/supabase/

*Who may change a request, as a row level security policy in the recipe's migration.*

From `docs/recipes/examples/supabase/0001_requests.sql` in the Meadowstack repository:

```sql
-- Which rows a person may touch at all. The guard trigger below decides which
-- change is legal, because a policy cannot compare the old row with the new.
create policy requests_update on public.requests for update
  using (
    -- request.edit, request.submit: a draft or a rejected request, by its author or an admin
    (status in ('draft', 'rejected') and (requester_id = auth.uid() or public.app_role() = 'admin'))
    -- request.withdraw: the author takes back a submitted request
    or (status = 'submitted' and requester_id = auth.uid())
    -- request.approve, request.reject: approvers and admins, never on their own (four-eyes)
    or (status = 'submitted' and requester_id <> auth.uid() and public.app_role() in ('approver', 'admin'))
    -- request.reopen: whoever decided undoes the decision
    or (status in ('approved', 'rejected') and decided_by_id = auth.uid())
  )
  with check (auth.uid() is not null);
```

## The two files

- `0001_requests.sql`, the migration: people and their roles, the requests with the schema's limits as check constraints, a history table nobody can edit or delete, row level security that mirrors the app's permissions policy by policy, a guard trigger for the workflow and four-eyes, a history trigger that writes in the same transaction, and `move_requests()` for all-or-nothing bulk moves.
- `api.supabase.ts`, the adapter: it maps rows (`needed_by`) to the app's types (`neededBy`) and the database's refusals to sentences. It checks no rule itself.

## Steps

1. Make a Supabase project you can throw away, and run the migration with the Supabase CLI or in its SQL editor.
2. In your app, `npm install @supabase/supabase-js`, copy the adapter to `src/modules/requests/data/api.supabase.ts` and add `VITE_SUPABASE_URL` and `VITE_SUPABASE_ANON_KEY`.
3. Sign people in with Supabase Auth behind `src/auth/auth.tsx`, so `auth.uid()` in the policies is the person on screen, and give each person a `profiles` row with a role.
4. Run the module's contract test against the test project before anyone signs in.

## What to watch

- A connection that bypasses row level security bypasses every policy, and several policies for one command combine with OR.
- Row level security hides the rows a person may not change, so a bulk update can succeed on fewer rows than asked. Count them and fail the whole call, as `move_requests()` does.
- Write the history in the same transaction as the change; the recipe does it in a trigger.
- The anon key is public by design. A service key belongs on the server only, never in a `VITE_` variable, which Vite builds into the bundle anyone can download.

## Questions

### Does the starter depend on Supabase?

No. The starter has no Supabase dependency. The recipe is two files you copy into your app, and the same contract runs against an HTTP api of your own instead.

### Is the anon key a secret?

No. Supabase's anon key is public by design: row level security is what protects the data. A service key that bypasses it belongs on the server only.

### What does auth.uid() return?

The ID of the person making the request, or null when nobody is signed in. That is why the policies compare it with the request's author and with whoever decided it.

## Sources

- [Supabase docs: Row Level Security](https://supabase.com/docs/guides/database/postgres/row-level-security), checked 1 December 2026

## Related

- [Postgres: the person in a transaction setting](https://meadowstack.com/integrations/postgres/) (Integrations)
- [Permissions](https://meadowstack.com/patterns/permissions/) (Patterns)
- [Team and roles, with the rules in code](https://meadowstack.com/screens/roles-and-permissions/) (Screens)
- [The approval flow](https://meadowstack.com/patterns/approval-flow/) (Patterns)
- [The audit trail](https://meadowstack.com/patterns/audit-trail/) (Patterns)
- [Google Workspace: only your company's domain](https://meadowstack.com/integrations/google-workspace/) (Integrations)
