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

feat: add dynamic schema to postgrest client #455

Merged
merged 1 commit into from
Aug 6, 2023

Conversation

sjones6
Copy link
Contributor

@sjones6 sjones6 commented Aug 5, 2023

Resolves #280

What kind of change does this PR introduce?

New feature

What is the current behavior?

See discussion on #280

The current behavior is that the PostgrestClient is created with a schema passed into the constructor. The class instance cannot reference tables or rpc calls for any other schemas unless a new instance is created.

// implicitly "public" schema
const postgrestPublic = new PostgrestClient<Database>(REST_URL)

// explicit specification of non-public schema
const postgrestPersonal = new PostgrestClient<Database, 'personal'>(REST_URL, { schema: 'personal' })

postgrestPublic.from('users').select() // select users from the "public" schema

postgresPersonal.from('users').select() // select users from the "personal" schema

What is the new behavior?

A single class instance exposes a facade method to reference other schemas:

const postgrest = new PostgrestClient<Database>(REST_URL) // implicitly "public" schema

postgrestPublic.from('users').select(); // select users from the "public" schema

postgrestPublic.schema('personal').from('users').select(); // select users from the "personal" schema

Type inference for responses also work, assuming a Database type has been supplied:
Screenshot 2023-08-05 at 12 09 27 PM

Table schema:
Screenshot 2023-08-05 at 12 10 09 PM

Compare without .schema('personal'):
Screenshot 2023-08-05 at 12 13 11 PM

Additional context

Assuming this PR is accepted, this PR supabase/supabase-js#828 could be modified like so:

export default class SupabaseClient {
  /* snip */

  /**
   * Perform a query on a schema distinct from the default schema supplied via
   * the `options.db.schema` constructor parameter.
   *
   * The schema needs to be on the list of exposed schemas inside Supabase.
   *
   * @param schema - The name of the schema to query
   */
  schema(
    schema: string & keyof Database
  ): PostgrestClient<Database[typeof schema] extends GenericSchema ? Database[typeof schema] : any, any> {
    return this.rest.schema(schema);
  }
}

This would allow the following:

const supabase = createClient(supabaseURL, supabaseAnonKey);

supabase.schema('personal').from('users').select();
supabase.schema('personal').rpc('get_status', { name_param: 'kiwicopple' });

Add any other context or screenshots.

Copy link
Member

@soedirgo soedirgo left a comment

Choose a reason for hiding this comment

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

Thanks again!

@soedirgo soedirgo merged commit 8d3f1e9 into supabase:master Aug 6, 2023
1 check passed
@github-actions
Copy link

github-actions bot commented Aug 6, 2023

🎉 This PR is included in version 1.8.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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