Skip to content

initialize client with multiple schemas #314

Closed
@chirastefan

Description

@chirastefan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions