Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic schemas on a per-query basis #828

Merged
merged 1 commit into from
Aug 7, 2023
Merged

Conversation

sjones6
Copy link
Contributor

@sjones6 sjones6 commented Aug 5, 2023

Resolves supabase/postgrest-js#280

What kind of change does this PR introduce?

Feature

What is the current behavior?

Currently, the supabase client is created and defaults to the public schema when no option is supplied:

createClient(supabaseUrl, supabaseKey)

Or, you can supply an alternative schema:

createClient(supabaseUrl, supabaseKey, {
  db: {
    schema: 'alternative'
  }
})

However, this means that all methods/queries for the client are associated to the single schema. Using multiple schemas requires multiple clients, which presents a challenge for auth where all clients need to be authenticated.

See pre-existing issue:

supabase/postgrest-js#280

What is the new behavior?

Extends on behavior added to underlying PostgrestClient: supabase/postgrest-js#455

The method added to the supabase client allows for dynamic schema selection on a per-query basis.

const supabase = createClient(supabaseUrl, supabaseKey);

supabase.from('some_table_in_public').select();

supabase.schema('alt').from('table_in_alt').select()

If a Database generic type is supplied, type inference works as expected without additional generics:
Screenshot 2023-08-06 at 1 12 31 PM

Additional context

Add any other context or screenshots.

@dshukertjr
Copy link
Member

Thanks for the feedback! We do have an issue discussing a solution for this here supabase/postgrest-js#280 . We will pick this one post launch week. Thanks for your patience!

@soedirgo
Copy link
Member

soedirgo commented Aug 5, 2023

Thanks @sjones6! We'll need the change both here and in postgrest-js, so keeping this open.

@sjones6
Copy link
Contributor Author

sjones6 commented Aug 6, 2023

@soedirgo , updated this PR now that supabase/postgrest-js#455 is published.

@@ -0,0 +1,201 @@
export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]

export interface Database {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from postgrest-js. Didn't bring over the the whole database setup that postgrest-js has, since that seems like a lot for this change.

@soedirgo soedirgo merged commit 90bd357 into supabase:master Aug 7, 2023
1 check passed
@sjones6 sjones6 deleted the 280 branch August 7, 2023 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Method to set schema on a call by call basis.
3 participants