how to client side query with drizzle ORM?

I'm using supabase as a backend but want to have a fully reproducible database, meaning everything that runs must be on a file in my codebase - no manual steps on the UI for that reason I'm using drizzle as an ORM, which can push and migrate my schema to supabase with drizzle kit the thing is it seems the only way to make use of RLS and postgrest to query the database from the client side is to use the supabase client library the problem is that the supabase client can't see the drizzle ORM types so to have type safe code I would have to 1. write my schema with drizzle ORM 2. push schema to supabase 3. generate typescript types with supabase 4. pass the generated types to supabase client you can see how this is quite cumbersome - ideally, it would just be 1. write schema with drizzle ORM 2. supabase client relies on that schema or maybe something else - I just need a way to query the database in a type safe way from the client side, making use of RLS for authorization has anyone set up something like this and would be able to share how they achieved it? thanks!
9 Replies
garyaustin
garyaustin•3w ago
Don't know anything about Drizzle, but https://orm.drizzle.team/docs/rls Talks about RLS and Supabase
Drizzle ORM - Row-Level Security (RLS)
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Marcos Pereira
Marcos PereiraOP•3w ago
this is fully server side though, drizzle does not support client side querying, which is why one still has to rely on the supabase-js client
silentworks
silentworks•3w ago
Create API endpoints in your app and do all of your drizzle stuff there and then use a fetch in your client components to query those API endpoints. You basically have to manage your own API endpoints instead of the Supabase SDK doing so with PostgREST.
Marcos Pereira
Marcos PereiraOP•3w ago
I considered that, but then: - I am either not using RLS at all or duplicating it in my server side logic - I'm using up my function quota when I could be querying directly
silentworks
silentworks•3w ago
- I'm not sure how you wouldn't be using RLS based on what Gary states that Drizzle having RLS support (never used this with Drizzle so only going from their docs). - Which function quota? this is being run on your own server. This section of their doc further explains how to get it working with Supabase RLS https://orm.drizzle.team/docs/rls#using-with-supabase They have an example repo in that section too.
Marcos Pereira
Marcos PereiraOP•3w ago
thanks for the suggestions! indeed drizzle supports creating RLS policies, but to actually make use of them I'd need some manual code to process supabase's JWT and send the user's role alongside the query somehow also I'd rather avoid having a custom backend and make use of supabase's functions, otherwise I'd need to integrate an additional provider into my architecture aha which defeats the purpose of supabase 🙂
silentworks
silentworks•3w ago
No manual code necessary, it's shown in the example in the link I sent you that they just use getSession() as you normally would inside of the createDrizzle function. I mean you are asking for alternative options and I'm just stating one. It defeating the purpose or not would be something you the dev would decide. I mean using Drizzle ORM already defeats the purpose of using Supabase in a way since you are now limited to the server side.
Marcos Pereira
Marcos PereiraOP•3w ago
oh coool thanks for noticing that I'm using drizzle as the source of truth for my db schema and using drizzle kit to push it to supabase, then I generate the types and use it on the frontend supabase client so still all frontend 😄
silentworks
silentworks•3w ago
So you are just using Drizzle for it's schema migration feature and not the actual ORM features? so you will still be using the Supabase SDK for the client side querying capabilities? You won't be able to use the Drizzle types directly with the @supabase/supabase-js library but you can use it to typehint the returned data.

Did you find this page helpful?