Demo, all content is generated
Question

Next.js + Supabase: query with supabase-js or use Drizzle directly?

Open · 526 views · asked by theo_side · edited

Starting a new project. Claude Code set up Drizzle for queries and supabase-js only for auth. Tutorials mostly use supabase-js for everything. Which one is less likely to bite me later? I care about RLS because users have private notes.

What I’ve tried

Read the Drizzle docs on Supabase and the Supabase docs on Drizzle. Both say it works. Neither says what you give up.

Comment

3 answers

rafa_dev · edited

The thing nobody mentions up front: Drizzle connects as the postgres role, which bypasses RLS. Your policies do nothing for queries that go through Drizzle. Every query has to filter by user in code, and one missed where leaks notes.

If RLS is your safety net, use supabase-js (with @supabase/ssr on the server) for anything user-scoped. Use Drizzle for the schema, migrations and admin jobs, where bypassing RLS is what you want.

Comment
I had no idea. Checked, and yes, my notes query has no user filter at all because I assumed RLS covers it. theo_side · edited
mei_lin · edited

Counterpoint, there's a middle way: Drizzle can run queries inside a transaction that sets role authenticated and the JWT claims, so RLS applies. There are helpers for this in the Drizzle docs. It's more setup though, and easy for an AI agent to skip. For a first project I'd go with Rafa's split.

Comment
True, and it's a good pattern. The risk is exactly what you say: one query outside the helper and you're back to no RLS. rafa_dev · edited
dev_ana · edited

Whichever you pick, add a test that logs in as user B and tries to read user A's note. That test catches the leak regardless of the library.

Comment
Wrote that test first thing. It failed. Now it passes. theo_side · edited