[v8] Bind a role and session settings per query in core, not only in the Supabase extension, for RLS multi-tenancy
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- node.js, postgresql, typescript
- Domain
- authentication, backend, databases
Research direction
Read the existing Supabase binding and RoleBoundDb implementation, then trace how connections are acquired, middleware runs, and cache annotations handle bound scopes. The issue also points to #31002 and #12735 for context. Done means core can bind a custom role and settings per query or transaction, clean up pooled connections, and expose the bound scope to middleware and caching.
Written by the indexing model from the issue text.
Description
What problem are you trying to solve?
Multi-tenant SaaS on PostgreSQL Row Level Security. Policies read an application-defined setting, current_setting('app.tenant_id'), and are written for a non-owner role, tenant_app. Some work also has to run as another role, for example an operator role that sees the tenants table and nothing inside the tenants. Every statement Prisma sends must therefore run on a connection that has the right role and setting at that moment. Nothing may leak to the next user of the pooled connection.
The Supabase extension already does exactly this: asUser(jwt) / asAnon() / asServiceRole() bind role and request.jwt.claims with session set_config beneath the middleware chain, and RESET ALL on release. That machinery is Supabase-specific: JWT-shaped, with fixed roles and claims.
Outside Supabase, the only working hook today is to subclass pg.Pool and override connect() so that every checkout runs set_config(...) before Prisma gets the client. The alternatives don't work:
SqlMiddlewaredoesn't see the connection.pg.Pool'sconnectevent fires only for new physical connections.acquireis synchronous.
Subclassing works, but it:
- ties the application to
pginternals and to Prisma's exactpgversion (see #31002); - is invisible to Prisma: the docs on
cacheAnnotationalready warn that RLS-filtered reads on the plain façade aren't protected. A binding Prisma knows about could keep those reads out of the cache, as it does forRoleBoundDb.
Proposed solution
Generalize the Supabase binding into core. For example:
const db = postgres({ contract, url });
// Bound to a role and settings; same surface as RoleBoundDb, including .transaction().
const tenantDb = db.bind({ role: 'tenant_app', settings: { 'app.tenant_id': tenantId } });
await tenantDb.orm.public.Project.all();
It would be applied by the same mechanism as the Supabase extension:
- session
set_configbeneath middleware andRESET ALLon release; - or
SET LOCALinside a transaction, as an option for transaction-mode poolers.
It would also be visible to middleware and the cache as a bound scope.
A lower-level alternative would also do: an async per-acquire hook, onAcquire(client, ctx), which the runtime awaits before handing a connection to a statement or transaction. Applications could then build the binding on it without subclassing pg.Pool.
Alternatives considered
- Subclassing
pg.Pool: what we do now. It works and is measured with concurrent mixed checkouts, but see the drawbacks above. - One transaction per request with
SET LOCAL: it holds a connection for the whole request, including non-database work such as mail, files and LLM calls. A caught error aborts the whole transaction. Background jobs need their own path. - The Supabase extension: Supabase-only.
- Filtering by tenant in application code instead of RLS: the database stops being the last line of defence.
Scope and impact
This matters to any multi-tenant application on PostgreSQL RLS with a custom setting or several database roles; #12735 shows long-standing demand. It needs a session-capable connection, the same requirement and documentation as the Supabase extension. It is additive: no change for applications that don't bind.
- Dominant language
- TypeScript
- Stars
- 47.8k
- Forks
- 2.6k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 227
Getting set up
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from prisma/orm
-
bug/1-unconfirmed kind/bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
get-platform: distros with ID_LIKE=alpine are not detected as musl, so glibc engines are used on Alpine-based imagesPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
prisma/orm#31281 · 1 comment ·
Maintainers usually reply within 1 day
-
adapter-pg: connectToShadowDb connects to the main database when built from a connection stringMay be free again @cestercian claimed this 10 days ago, and no pull request is open. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
prisma/orm#31196 · 1 comment ·
Maintainers usually reply within 1 day
-
[Prisma 8] Type-check time grows exponentially with chained .include() callsPossibly taken @SiKreuz claimed this 13 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
prisma/orm#31161 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
prisma/orm#31125 · 4 comments · 6 reactions ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
tenstorrent/ttnn-visualizer#2526 ·
Maintainers usually reply within 1 day
-
trySessionCache calls /get-session twice when minting session_data (cache miss or expired cookie)Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
neondatabase/neon-js#301 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
tashfeenahmed/freellmapi#2305 ·
Maintainers usually reply within 1 day