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

initialize client with multiple schemas #314

Closed
chirastefan opened this issue Dec 23, 2022 · 6 comments · Fixed by #525
Closed

initialize client with multiple schemas #314

chirastefan opened this issue Dec 23, 2022 · 6 comments · Fixed by #525
Labels
enhancement New feature or request

Comments

@chirastefan
Copy link

chirastefan commented Dec 23, 2022

Flutter client doesn't seem to support initializing Supabase with multiple schemas and then querying based on those schemas. One client = one schema.

the code below throws this error This instance is already initialized

var schemaOne = await Supabase.initialize(url: endpoint, anonKey: anonKey, schema: 'schema_one', debug: kDebugMode);
var schemaTwo = await Supabase.initialize(url: endpoint, anonKey: anonKey, schema: 'schema_two', debug: kDebugMode);
  1. A workaround for this is to dispose of the client and re-init it every time a new query is made to a different schema. IMO this doesn't sound that good. I guess it raises some performance issues and other side effects, but maybe not and that's the intended behavior.

  2. Another workaround would be to not use the Supabase.initialize() method and init the SupabaseClient directly:

await SupabaseAuth.initialize();
var schemaOne = SupabaseClient(url, anonKey, schema: 'schema_one');
var schemaTwo = SupabaseClient(url, anonKey, schema: 'schema_two');

This is a bit of a hack also, this way the log() method from Supabase class doesn't know about the _debugEnable flag.
Is there another way of doing this?

Edit: I tried solution 2 and it doesn't work. Supabase.initialize() must be called, so the way I did it is to call that method which initializes Supabase with the default scheme - public and then separately initialize other schemas with SupabaseClient()

enum SupabaseSchemas { public, schema1, schema2 }

late final Map<SupabaseSchemas, SupabaseClient> schemas = {};

Future<void> _initSchemas() async {
    schemas[SupabaseSchemas.public] = (await Supabase.initialize(url: endpoint, anonKey: anonKey, debug: kDebugMode)).client;
    final List<SupabaseSchemas> customSchemas = SupabaseSchemas.values.filterNot((sch) => sch == SupabaseSchemas.public).toList();

    for (final schema in customSchemas) {
      schemas[schema] = schemas[schema] == null || schemas[schema] is! SupabaseClient
          ? (SupabaseClient(endpoint, anonKey, schema: schema.name))
          : schemas[schema]!;
    }
  }

Currently, the above solution seems to work, but I'm not sure if this is the proper way to do it or if there's another way of doing this. This works for insert/upsert/update queries, but for select I don't have a solution to query the from other schemas.
An ideal solution would be to init the client with the custom schemas and then for every query to be able to set the schema as an option.

@chirastefan chirastefan added the enhancement New feature or request label Dec 23, 2022
@chirastefan chirastefan changed the title init client with multiple schemas initialize client with multiple schemas Dec 23, 2022
@dshukertjr
Copy link
Member

Yeah, this is an huge issue with the Flutter SDK, isn't it. We will try to figure out a nice way to handle this.

@nietsmmar
Copy link

@dshukertjr
This is really a big problem for us. Is there a timetable for when the problem will be fixed? Thanks a lot!

@dshukertjr
Copy link
Member

@nietsmmar
Thanks for letting us know. Let me think about what would be the ideal API to implement this feature.

@rddewan
Copy link

rddewan commented Jun 28, 2023

@dshukertjr
I think this is a major road blocker to use Supabase in flutter app. In production app we will have multiple schemas but currently we can only use public. Can we directly make a http call to API so instead of use using flutter package we can call api using http or dio package

@dshukertjr
Copy link
Member

@rddewan
Sorry this issue has been long due. You can certainly make your own HTTP call to our API endpoints.

What do you think about a solution like this, where you can specify the schema within the from?

final data = await supabase.from('users', schema: 'custom_schema').select();

@dshukertjr
Copy link
Member

Actually, let me close this one as there is an issue that is basically trying to tackle the same problem, querying across different schemas.
#206

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
4 participants