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:
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);
}
}
Resolves https://github.com/supabase/postgrest-js/issues/280
What kind of change does this PR introduce?
New feature
What is the current behavior?
See discussion on https://github.com/supabase/postgrest-js/issues/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.What is the new behavior?
A single class instance exposes a facade method to reference other schemas:
Type inference for responses also work, assuming a
Database
type has been supplied:Table schema:
Compare without
.schema('personal')
:Additional context
Assuming this PR is accepted, this PR https://github.com/supabase/supabase-js/pull/828 could be modified like so:
This would allow the following:
Add any other context or screenshots.