diff --git a/src/PostgrestClient.ts b/src/PostgrestClient.ts index 9e331104..e7d9678a 100644 --- a/src/PostgrestClient.ts +++ b/src/PostgrestClient.ts @@ -25,7 +25,7 @@ export default class PostgrestClient< > { url: string headers: Record - schema?: SchemaName + schemaName?: SchemaName fetch?: Fetch // TODO: Add back shouldThrowOnError once we figure out the typings @@ -52,7 +52,7 @@ export default class PostgrestClient< ) { this.url = url this.headers = { ...DEFAULT_HEADERS, ...headers } - this.schema = schema + this.schemaName = schema this.fetch = fetch } @@ -73,7 +73,32 @@ export default class PostgrestClient< const url = new URL(`${this.url}/${relation}`) return new PostgrestQueryBuilder(url, { headers: { ...this.headers }, - schema: this.schema, + schema: this.schemaName, + fetch: this.fetch, + }) + } + + /** + * Select a schema to query or perform an function (rpc) call. + * + * The schema needs to be on the list of exposed schemas inside Supabase. + * + * @param schema - The schema to query + */ + schema( + schema: DynamicSchema + ): PostgrestClient< + Database, + DynamicSchema, + Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any + > { + return new PostgrestClient< + Database, + DynamicSchema, + Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any + >(this.url, { + headers: this.headers, + schema, fetch: this.fetch, }) } @@ -143,7 +168,7 @@ export default class PostgrestClient< method, url, headers, - schema: this.schema, + schema: this.schemaName, body, fetch: this.fetch, allowEmpty: false, diff --git a/test/basic.ts b/test/basic.ts index 9e7951fd..0c948828 100644 --- a/test/basic.ts +++ b/test/basic.ts @@ -196,6 +196,51 @@ test('switch schema', async () => { `) }) +test('dynamic schema', async () => { + const postgrest = new PostgrestClient(REST_URL) + const res = await postgrest.schema('personal').from('users').select() + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": Array [ + Object { + "age_range": "[1,2)", + "data": null, + "status": "ONLINE", + "username": "supabot", + }, + Object { + "age_range": "[25,35)", + "data": null, + "status": "OFFLINE", + "username": "kiwicopple", + }, + Object { + "age_range": "[25,35)", + "data": null, + "status": "ONLINE", + "username": "awailas", + }, + Object { + "age_range": "[20,30)", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, + Object { + "age_range": "[20,40)", + "data": null, + "status": "ONLINE", + "username": "leroyjenkins", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", + } + `) +}) + test('on_conflict insert', async () => { const res = await postgrest .from('users') @@ -865,6 +910,19 @@ test('rpc with head:true, count:exact', async () => { `) }) +test('rpc with dynamic schema', async () => { + const res = await postgrest.schema('personal').rpc('get_status', { name_param: 'kiwicopple' }) + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": "OFFLINE", + "error": null, + "status": 200, + "statusText": "OK", + } + `) +}) + describe("insert, update, delete with count: 'exact'", () => { test("insert with count: 'exact'", async () => { let res = await postgrest