effect-sql-rls
Skill9 starsChanged 42 days agoMITView on GitHub
Use when writing any query, migration, or test that touches tenant data in this repository. Covers withOrgScope and withWorkerScope, the two Postgres roles and why the boundary is in the database rather than in a lint rule, the shape a row-level security policy has to take, when Effect.orDie is right, and why a fixture seeds through the product's own write path.
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 8
Every tenant-owned table is protected twice: a row-level security policy in Postgres, and withOrgScope around the query.
- Very specific: line 75
Not because it is tidy, but because a fixture needing more privilege than the application has is a fixture describing a state the …
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Tenant data, and the two things that keep it apart
Every tenant-owned table is protected twice: a row-level security policy in Postgres,
and withOrgScope around the query. Neither is trusted alone. Unit tests cover each; only
e2e/tests/tenancy.spec.ts shows two real users, session to SQL, failing to see each
other's rows.
The connection is the boundary
packages/database/src/roles/init.sql makes two roles:
vantion— what every application process connects as, andNOBYPASSRLS. A handler that forgetswithOrgScopetherefore reads nothing rather than reading everybody.admin— holdsBYPASSRLS, for the cross-tenant surface only. Which process may cross tenants is decided by Postgres, not by a reviewer.
This was not true until recently. docker compose set POSTGRES_USER: vantion, making it
the bootstrap superuser — so every policy in this schema was inert while
pg_class.relrowsecurity read true. packages/database/test/Role.test.ts asserts the
connection cannot bypass, because a suite pointed at a superuser passes every tenancy test
while proving nothing.
Switching the role found two production bugs, both the same shape: a worker escape on
using but not on with check, and an UPDATE is checked by both.
Which scope
| Function | Who uses it | What it sets |
withOrgScope | A handler with a caller | app.current_org from CurrentUser |
withOrgScopeFor(orgId, …) | A seed, a test, anything with an id and no session | the same, from a plain id |
withWorkerScope | The relay, delivery, the Stripe webhook | app.worker = 'on' |
withOrgScopeFor lives in packages/database and withOrgScope in
@vantion/module-iam, because reading the caller is an identity concern and database
would otherwise depend on the module that depends on it.
Filter by organization and scope. Both, never either. currentSubscription relied on
the policy alone and every organization read the first subscription row in the table — a
superuser, or any role with BYPASSRLS, ignores row-level security even on a FORCEd table,
which is what a managed Postgres often hands you.
Writing a policy
Split by command rather than widening one. 0012_worker_writes.sql is the worked example:
INSERT stays exactly as strict as it was, and the worker gains only the ability to update
what it is already allowed to read.
create policy "x_select" on "x" for select
using ("organizationId" = current_setting('app.current_org', true)
or current_setting('app.worker', true) = 'on');
create policy "x_insert" on "x" for insert
with check ("organizationId" = current_setting('app.current_org', true));A table better-auth owns gets no policy at all rather than a permissive one — see the
better-auth skill and 0011_sso.sql.
Effect.orDie on a query is usually right
RULES.md says when. What reaches an orDie on a sql call is the database being
unreachable, the query being wrong, or a policy refusing it — all bugs. The durable rule:
adding a constraint to a table is the moment to revisit every orDie writing to it,
because that is when a defect becomes a message somebody needed.
Tests seed through the product's write path
Not because it is tidy, but because a fixture needing more privilege than the application
has is a fixture describing a state the application cannot produce. Use withOrgScopeFor,
not a bare insert — a bare insert into a policied table is refused outright.
Database-backed tests skip without a database. describe.skipIf(testDbUrl() === undefined).
Where to look
packages/database/src/OrgScope.ts,packages/modules/iam/src/identity/OrgScope.tspackages/database/src/migrations/0002_rls.sql,0012_worker_writes.sqlpackages/database/test/Role.test.ts— the test that keeps the role honestpackages/modules/admin/— the only thing using the second role, throughcrossTenant
In this repository33 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .cursor/rules/better-auth.mdcCursor rule
- .cursor/rules/brand-review.mdcCursor rule
- .cursor/rules/campaign-plan.mdcCursor rule
- .cursor/rules/competitive-brief.mdcCursor rule
- .cursor/rules/content-creation.mdcCursor rule
- .cursor/rules/draft-content.mdcCursor rule
- .cursor/rules/effect-form-e2e.mdcCursor rule
- .cursor/rules/effect-sql-rls.mdcCursor rule
- .cursor/rules/email-sequence.mdcCursor rule
- .cursor/rules/grilling.mdcCursor rule
- .cursor/rules/grill-me.mdcCursor rule
- .cursor/rules/impeccable.mdcCursor rule
- .cursor/rules/performance-report.mdcCursor rule
- .cursor/rules/product-development.mdcCursor rule
- .cursor/rules/seo-audit.mdcCursor rule
- .cursor/rules/tanstack-start-ssr.mdcCursor rule
- better-authSkill
- brand-reviewSkill
- campaign-planSkill
- competitive-briefSkill
- content-creationSkill
- draft-contentSkill
- effect-form-e2eSkill
- email-sequenceSkill
- grillingSkill
- grill-meSkill
- impeccableSkill
- performance-reportSkill
- product-developmentSkill
- seo-auditSkill
- tanstack-start-ssrSkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

