Supabase RPC vs Edge Function: where should this code run?
A Supabase app offers at least six places to put a piece of logic: the app itself, a Postgres function called through RPC, a database trigger, a Supabase Edge Function, an Expo API route, and a separate Node server. The choice is not a matter of style. Each place can see different things, and that determines what it is safe and sensible to do there.
Three questions decide it
For any piece of logic, three questions narrow the options. Does it need a secret, such as a Stripe key or the Supabase secret key? Does it need to know who the user is? Does it need to change several rows as a single unit, so that a failure halfway leaves nothing behind?
No single place answers yes to all three. Postgres functions and triggers run inside the database transaction but cannot hold outside secrets. Edge Functions, API routes and servers hold secrets but sit outside the transaction. The app knows the user but can hold no secrets and can be modified by whoever has the phone.
The app
The app holds the publishable key and the user's session. Supabase describes the publishable key as readable by anyone, limited to whatever Row Level Security allows. That makes the app a reasonable place for reads and writes that RLS already polices, such as a user reading their own orders.
Anything that has to be enforced cannot rely on app code alone, because the app runs on a device the developer does not control. The secret key never belongs here. Supabase's documentation states that it bypasses Row Level Security and should never be placed in a browser or a shipped application.
Postgres functions called by RPC
A Postgres function runs inside the database. The app calls it with supabase.rpc('function_name'). Every request through Supabase's Data API runs inside a transaction, so if any statement in the function fails, the whole request rolls back.
That makes functions the natural home for operations that touch several rows together. A booking that checks a slot, inserts the booking and updates a count either completes or leaves no trace. The transaction alone does not stop two users booking the same slot at the same moment; a unique constraint or a row lock inside the function does that. Keeping the whole operation in one function makes both easy to apply.
Functions know the user. The JWT travels with the request and auth.uid() reads the user ID from it. Supabase's default is security invoker, so the function runs with the caller's permissions and RLS still applies. A security definer function runs with its owner's permissions, and Supabase recommends setting search_path = '' and naming the schema of every table in that case.
Triggers
A trigger runs a function automatically when rows are inserted, updated or deleted. PostgreSQL runs a trigger as part of the same transaction as the statement that fired it, so an error in the trigger undoes the statement too. A row-level BEFORE trigger can modify the incoming row or skip it. An AFTER trigger runs once the row has been written.
Triggers suit rules that must hold whoever writes the data: an app, a server, or someone in the SQL editor. Setting updated_at and writing audit rows are common examples. The drawback is visibility. Nothing in the application code shows that a trigger exists.
Edge Functions
Supabase Edge Functions are TypeScript functions running on a Deno-based runtime, distributed close to users. They read secrets from environment variables, which makes them the usual home for Stripe calls and webhook receivers. By default the gateway validates the user's Supabase JWT before the function runs.
They run outside the database transaction. Three separate database calls from an Edge Function are three separate transactions. A reliable pattern is for the Edge Function to handle the outside world, then call a single Postgres function for the database changes. Supabase lists a limit of 2 seconds of CPU time per request, a wall clock limit of 150 seconds on the Free plan and 400 seconds on paid plans, and notes that cold starts are possible.
Expo API routes
Expo Router API routes are files ending in +api.ts inside the app directory. Each becomes a server endpoint, and code in those files is kept out of the client bundle, so secrets in them stay on the server. They require web.output: "server" and are deployed to EAS Hosting or another host. Like Edge Functions, they sit outside the database transaction.
The choice between an API route and an Edge Function is mostly about where the code lives and where it is deployed. An API route sits in the same repository as the screens that call it. An Edge Function is deployed to Supabase, next to the database. An Edge Function gets JWT validation at the gateway by default; an API route receives whatever token the app sends and verifies it in its own code.
A Node server
A server you run yourself has no platform limits on duration and can hold connections or run scheduled work. It also has to be deployed, monitored and paid for.
My apps share one Supabase database and a Node server. For the rider side, permission rules live in server code rather than in RLS. The rider app asks the server for orders, the server checks the rider is allowed to see them, and then reads with the secret key. The trade is that the database no longer enforces anything for those reads, so every route has to perform its own check.
A decision guide
- Must happen on every write, whoever writes: a trigger.
- Several rows that change together: a Postgres function called by RPC.
- Needs a secret or calls a third-party API: an Edge Function or API route, which hands its database work to a function.
- Long-running, scheduled or holding a connection: a server.
- None of these: the app, with RLS doing the checking.
Combining them
Most real features use two of these together. A payment is a good example: an Edge Function or server receives Stripe's webhook, verifies it with a secret only the server holds, and then calls one Postgres function that changes the order and payment rows as a single unit. The outside work happens where the secrets are; the database work happens where the transaction is.
Sources
- Supabase docs: API keys
- Supabase docs: Database functions
- PostgREST docs: Transactions
- Supabase docs: Row Level Security
- PostgreSQL docs: Transaction isolation
- Supabase docs: Triggers
- PostgreSQL docs: Overview of trigger behavior
- Supabase docs: Edge Functions
- Supabase docs: Edge Function limits
- Expo docs: API routes