roninforge/roninforge-drizzle/drizzle-rls
Claude Code and agent skill by roninforge/roninforge-drizzle
- community
Checking that the SKILL.md file is still in the repository.
Scaffold Postgres Row-Level Security with Drizzle: pgPolicy declarations, enableRLS() (0.45.x) or pgTable.withRLS() (1.0-rc), permissive vs restrictive policies, owner / role / public patterns, setting auth context per request, Supabase + Neon Authorize integration.
Install
npx vskill@latest install roninforge/roninforge-drizzle/drizzle-rlsSKILL.md
The same SKILL.md was published earlier as roninforge/roninforge-drizzle/drizzle-rls.
Scaffold Postgres RLS with Drizzle
When to Use
When designing a multi-tenant or per-user-isolated table on Postgres. Postgres RLS pushes authorization to the database, so a missing WHERE user_id = $1 in application code cannot leak data.
Drizzle has first-class RLS support from 0.32 (primitives) and 0.33 (GA).
Instructions
Step 1: Decide the policy shape
Three common patterns:
| Pattern | Meaning | Example | |
|---|---|---|---|
| Owner | Each row belongs to one user; only that user can read/write | per-user notes, drafts | |
| Tenant | Each row belongs to one tenant; any user in that tenant can read | shared workspace data | |
| Public-read, owner-write | Anyone can read; only the owner can mutate | published posts |
Pick one before writing the policy.
Step 2: Add the schema with pgPolicy + enableRLS() (0.45.x)
Critical: `.enableRLS()` is required. Adding pgPolicy calls without it does nothing - Postgres only enforces policies on tables where RLS is explicitly enabled.
Step 2 (alternative): v1.0-rc syntax
If your project pins drizzle-orm@1.x, use pgTable.withRLS. Otherwise stay on .enableRLS().
Step 3: Generate the migration
The generated SQL should include both ALTER TABLE notes ENABLE ROW LEVEL SECURITY; and CREATE POLICY ... statements. Review the file.
Step 4: Set the auth context per request
Policies are evaluated against auth.uid() (Supabase / Neon Authorize convention). You must set the JWT claims on the connection before queries run:
SET LOCAL scopes the change to the current transaction. Wrap your request handler in a transaction so the setting is automatically discarded:
For Supabase, the platform sets these for you when you use the supabase-js client server-side. For Neon Authorize, the same. For self-hosted Postgres, write the middleware yourself.
Step 5: Test the policies
Use Testcontainers Postgres + the per-test-transaction-rollback pattern:
Step 6: Disable bypass for the application role
Postgres roles with BYPASSRLS see every row regardless of policy. The application's connection role should NOT have it.
Application connects as app_user. Migrations and admin tasks connect as a different role with BYPASSRLS (or as the table owner).
Common policy shapes
The snippets below assume authenticatedRole, anonRole, and pgPolicy / sql are already imported at the top of the file (see Step 2 for the full import block).
Tenant isolation
JWT contains the tenant claim. Policy filters every query.
Public-read, owner-write
Restrictive (additional constraints, AND'ed with permissive)
Permissive policies are OR'd; restrictive policies are AND'd on top. Use restrictive sparingly - they affect every operation.
Anti-patterns to avoid
- Forgetting `.enableRLS()` - policies exist but Postgres ignores them silently. Test that bob cannot read alice's rows; if he can, RLS is off.
- Letting the application role have `BYPASSRLS` - policies become decorative.
- `SET` instead of `SET LOCAL` - the auth context leaks across requests on the same pooled connection.
- Per-row checks in app code in addition to RLS - duplication, two sources of truth, eventually one drifts. Trust RLS or trust app code, pick one.
- Policy `using` clause with mutable functions -
using: sqlnow() < expires_at`is fine;using: sqlsome_volatile_func()` is not. Postgres caches policy evaluation aggressively. - Skipping `withCheck` on INSERT/UPDATE -
usingonly filters reads; withoutwithChecka user can write rows tagged with someone else'suser_id.
What to ship
- Schema file with
pgPolicydeclarations and.enableRLS()(orpgTable.withRLS()on v1). - Generated SQL migration that includes
ALTER TABLE ... ENABLE ROW LEVEL SECURITY;andCREATE POLICY .... - Middleware that sets
SET LOCAL "request.jwt.claims"per request inside a transaction. - Tests that verify cross-user isolation (the spoofed-userId case).
- A
app_userPostgres role withNOBYPASSRLSfor the application connection.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/drizzle-rls/SKILL.md
Popularity
- 7-day trend score
- 5
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/roninforge/roninforge-drizzle/drizzle-rls)