Hello. I'm trying to implement a multi-tenant appr...
# help
j
Hello. I'm trying to implement a multi-tenant approach through schemas. I know from the docs that the
Copy code
supabaseClient = createClient()
will allow access to one and only one schema. Right now a non-authenticated user will access the public schema. Once authenticated, I want to change the accessed schema. Currently, I just override the supabaseClient with
Copy code
createClient({schema: 'new-schema')
. Is there a better way to go about this approach? Also, will overriding the variable allow the connection to the public schema to remain open?
n
Hello @Jingly! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
g
No, one schema allowed at a time. But you can have two clients open (at least in my limited testing, you would want to research that a bit more around token refresh). You can also likely change the header before each call. The content-profile header contains the schema. I'll add how to do that in a second.
n
JinglySmith (2022-05-22)
g
I used this to change a header for refresh token, but should work for content-profile...
Copy code
//Option 1, shell to call if you will need valid token... there are probably "nicer" ways to implement this...
var SUPAbase = async function (sb) {
    if (await checkSession() ==='expired') {
        let session = supabase.auth.session();
        sb.headers.Authorization = `Bearer ${session.access_token}`  
    }
    return (await sb)
}
// call like SUPAbase(supabase.from....)
So I think supabase.headers.content-profile = 'my-schema' before the call might work, where supabase is your client, but have not tested it.
j
Thanks for the help! That doesn't seem to work tho, returns an error saying that I have to use the public schema.
I'm still trying to figure out the best way to implement multi-tenancy before I start moving everything over
Ideally a schema based approach would probably be best, but seems limited by supabase (for now, I've been reading through the github discussions and there aware of it). Might have to go the route of same database/schema with increased RLS
g
Did you add schema to the available ones to the API? Under settings,API, Schema.