Skip to content

Commit 8d3f1e9

Browse files
sjones6soedirgo
authored andcommitted
feat: add dynamic schema to postgrest client
Resolves #280
1 parent 32e220c commit 8d3f1e9

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

src/PostgrestClient.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class PostgrestClient<
2525
> {
2626
url: string
2727
headers: Record<string, string>
28-
schema?: SchemaName
28+
schemaName?: SchemaName
2929
fetch?: Fetch
3030

3131
// TODO: Add back shouldThrowOnError once we figure out the typings
@@ -52,7 +52,7 @@ export default class PostgrestClient<
5252
) {
5353
this.url = url
5454
this.headers = { ...DEFAULT_HEADERS, ...headers }
55-
this.schema = schema
55+
this.schemaName = schema
5656
this.fetch = fetch
5757
}
5858

@@ -73,7 +73,32 @@ export default class PostgrestClient<
7373
const url = new URL(`${this.url}/${relation}`)
7474
return new PostgrestQueryBuilder<Schema, any>(url, {
7575
headers: { ...this.headers },
76-
schema: this.schema,
76+
schema: this.schemaName,
77+
fetch: this.fetch,
78+
})
79+
}
80+
81+
/**
82+
* Select a schema to query or perform an function (rpc) call.
83+
*
84+
* The schema needs to be on the list of exposed schemas inside Supabase.
85+
*
86+
* @param schema - The schema to query
87+
*/
88+
schema<DynamicSchema extends string & keyof Database>(
89+
schema: DynamicSchema
90+
): PostgrestClient<
91+
Database,
92+
DynamicSchema,
93+
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any
94+
> {
95+
return new PostgrestClient<
96+
Database,
97+
DynamicSchema,
98+
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any
99+
>(this.url, {
100+
headers: this.headers,
101+
schema,
77102
fetch: this.fetch,
78103
})
79104
}
@@ -143,7 +168,7 @@ export default class PostgrestClient<
143168
method,
144169
url,
145170
headers,
146-
schema: this.schema,
171+
schema: this.schemaName,
147172
body,
148173
fetch: this.fetch,
149174
allowEmpty: false,

test/basic.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,51 @@ test('switch schema', async () => {
196196
`)
197197
})
198198

199+
test('dynamic schema', async () => {
200+
const postgrest = new PostgrestClient<Database>(REST_URL)
201+
const res = await postgrest.schema('personal').from('users').select()
202+
expect(res).toMatchInlineSnapshot(`
203+
Object {
204+
"count": null,
205+
"data": Array [
206+
Object {
207+
"age_range": "[1,2)",
208+
"data": null,
209+
"status": "ONLINE",
210+
"username": "supabot",
211+
},
212+
Object {
213+
"age_range": "[25,35)",
214+
"data": null,
215+
"status": "OFFLINE",
216+
"username": "kiwicopple",
217+
},
218+
Object {
219+
"age_range": "[25,35)",
220+
"data": null,
221+
"status": "ONLINE",
222+
"username": "awailas",
223+
},
224+
Object {
225+
"age_range": "[20,30)",
226+
"data": null,
227+
"status": "ONLINE",
228+
"username": "dragarcia",
229+
},
230+
Object {
231+
"age_range": "[20,40)",
232+
"data": null,
233+
"status": "ONLINE",
234+
"username": "leroyjenkins",
235+
},
236+
],
237+
"error": null,
238+
"status": 200,
239+
"statusText": "OK",
240+
}
241+
`)
242+
})
243+
199244
test('on_conflict insert', async () => {
200245
const res = await postgrest
201246
.from('users')
@@ -865,6 +910,19 @@ test('rpc with head:true, count:exact', async () => {
865910
`)
866911
})
867912

913+
test('rpc with dynamic schema', async () => {
914+
const res = await postgrest.schema('personal').rpc('get_status', { name_param: 'kiwicopple' })
915+
expect(res).toMatchInlineSnapshot(`
916+
Object {
917+
"count": null,
918+
"data": "OFFLINE",
919+
"error": null,
920+
"status": 200,
921+
"statusText": "OK",
922+
}
923+
`)
924+
})
925+
868926
describe("insert, update, delete with count: 'exact'", () => {
869927
test("insert with count: 'exact'", async () => {
870928
let res = await postgrest

0 commit comments

Comments
 (0)