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.
By Bohuslav PoláčekChecked
-- 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, andmove_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
- Make a Supabase project you can throw away, and run the migration with the Supabase CLI or in its SQL editor.
- In your app,
npm install @supabase/supabase-js, copy the adapter tosrc/modules/requests/data/api.supabase.tsand addVITE_SUPABASE_URLandVITE_SUPABASE_ANON_KEY. - Sign people in with Supabase Auth behind
src/auth/auth.tsx, soauth.uid()in the policies is the person on screen, and give each person aprofilesrow with a role. - 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.
Sources
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.
Two ways
Build it yourself, or have it built
Build it yourself
Build your app on the framework
Get the codebase these apps stand on and build yours with Claude Code, Cursor or Codex.
One payment, lifetime updates
Get the frameworkHave it built
Have us build your app
Tell us how your team works. We plan the app with you and build it on the same framework at a fixed price.
After launch, Care keeps it improving, from €99 a month.
from €1,990
Describe your app