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(postgrest, supabase): add useSchema() method for making rest API calls on custom schema. #525

Merged
merged 8 commits into from
Aug 8, 2023

Conversation

dshukertjr
Copy link
Member

@dshukertjr dshukertjr commented Jun 28, 2023

What kind of change does this PR introduce?

Allows developers to easily query custom schema.
Fixes #314

What is the current behavior?

There isn't a way to query other schemas once Supabase client is initialized. Some workarounds that I can think about right now would be

  • Don't use custom schemas
  • Create postgres views/ functions in the public schema for every custom schema table
  • dispose and re-initialize the client every time when querying custom schema
    All of them are kind of painful to implement.

What is the new behavior?

Developers can query custom schema like this:

final data = await supabase.useSchema('custom_schema').from('table').select();

Additional Context

Since schema name is already taken, we couldn't name the method schema() like how the js SDK does it.

@Vinzent03
Copy link
Collaborator

Is there a reason you are adding that as a parameter to from instead of a new method as it seems proposed in supabase/postgrest-js#280 ?

Additionally, I think rpc needs that param as well.

@dshukertjr
Copy link
Member Author

@Vinzent03 No, I had forgotten about that one. Thanks for bringing it up.

@dshukertjr dshukertjr marked this pull request as draft June 29, 2023 07:31
@dshukertjr dshukertjr changed the title feat(postgrest, supabase): add schema parameter to from() to query custom schema feat(postgrest, supabase): add setSchema method for making rest API calls. Aug 7, 2023
@dshukertjr dshukertjr marked this pull request as ready for review August 7, 2023 09:18
@dshukertjr dshukertjr requested review from DanMossa, bdlukaa and Vinzent03 and removed request for DanMossa, bdlukaa and Vinzent03 August 7, 2023 09:19
@Vinzent03
Copy link
Collaborator

.setSchema() sounds like you would change the schema of the existing postgrest client. What about just .schema(). That's what they use in js now as well.

@dshukertjr
Copy link
Member Author

dshukertjr commented Aug 7, 2023

@Vinzent03
I should have mentioned it in the PR somewhere, but the schema name is already taken by the schema property of SupabaseClient and the PostgrestClient, so had to go with something else. In the next major release though, we should be able to rename it to schema() after making the schema property private.

Also would love to hear your thoughts on the method name. Some that went through my head were:

  • setSchema()
  • useSchema()
  • fromSchema()

@Vinzent03
Copy link
Collaborator

Ah okay. I would choose useSchema() or fromSchema(). I tend towards fromSchema(), because it's similar to from() and that's okay, because they both do pretty much the same.

@bdlukaa
Copy link
Collaborator

bdlukaa commented Aug 7, 2023

I like useSchema better. setSchema can be misleading

@dshukertjr
Copy link
Member Author

Thanks for the comments. It was very close for me between setSchema() and useSchema() for me, so I'd say useSchema() is the winner. Also want to hear your opinion on another thing. What do you think about a syntax like this:

.from('table', schema: 'my_schema')

which is what I had originally on this PR. I know it's not at all similar to the js implementation, but I think it's cleaner, because we don't have extra words like use or set with the schema keyword. I also slightly don't like the fact that you can chain useSchema like this:

supabase.useSchema('my_schema').useSchema('another_schema')...

It means the IDE will suggest useSchema() after calling useSchema like this.
Screenshot 2023-08-07 at 23 34 48

@dshukertjr dshukertjr changed the title feat(postgrest, supabase): add setSchema method for making rest API calls. feat(postgrest, supabase): add useSchema method for making rest API calls. Aug 7, 2023
@dshukertjr dshukertjr changed the title feat(postgrest, supabase): add useSchema method for making rest API calls. feat(postgrest, supabase): add useSchema method for making rest API calls on custom schema. Aug 7, 2023
@Vinzent03
Copy link
Collaborator

Using the current workflow allows using one postgrest client with a specific schema for multiple calls. Adding it to from means you have to repeat it always. So I would stay with the current approach.

@dshukertjr
Copy link
Member Author

Cool, in that case this PR should be ready for review.

@dshukertjr dshukertjr changed the title feat(postgrest, supabase): add useSchema method for making rest API calls on custom schema. feat(postgrest, supabase): add useSchema() method for making rest API calls on custom schema. Aug 8, 2023
@dshukertjr dshukertjr merged commit 40a0f09 into main Aug 8, 2023
10 checks passed
@dshukertjr dshukertjr deleted the feat/schema branch August 8, 2023 09:17
@garysm
Copy link

garysm commented Aug 21, 2023

@dshukertjr Can you give an example of what the recommended permissions should be for a new schema for the Flutter client? For example, a table that would only be available to postgres, service_role, and authenticated users. I have tried adding these myself but I still get a PGRST106 error.

@dshukertjr
Copy link
Member Author

@garysm
Have you added your new schema to search path in your Supabase dashboard under settings>API>Extra Search Path?

@garysm
Copy link

garysm commented Aug 21, 2023

@garysm Have you added your new schema to search path in your Supabase dashboard under settings>API>Extra Search Path?

No, I've been developing locally, but I assume that can be configured in config.toml. I'll give that a shot. Thanks!

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.

initialize client with multiple schemas
4 participants