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: add dynamic schema to postgrest client #455

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/PostgrestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class PostgrestClient<
> {
url: string
headers: Record<string, string>
schema?: SchemaName
schemaName?: SchemaName
fetch?: Fetch

// TODO: Add back shouldThrowOnError once we figure out the typings
Expand All @@ -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
}

Expand All @@ -73,7 +73,32 @@ export default class PostgrestClient<
const url = new URL(`${this.url}/${relation}`)
return new PostgrestQueryBuilder<Schema, any>(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<DynamicSchema extends string & keyof Database>(
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,
})
}
Expand Down Expand Up @@ -143,7 +168,7 @@ export default class PostgrestClient<
method,
url,
headers,
schema: this.schema,
schema: this.schemaName,
body,
fetch: this.fetch,
allowEmpty: false,
Expand Down
58 changes: 58 additions & 0 deletions test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,51 @@ test('switch schema', async () => {
`)
})

test('dynamic schema', async () => {
const postgrest = new PostgrestClient<Database>(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')
Expand Down Expand Up @@ -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
Expand Down
Loading