Skip to content
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
4 changes: 3 additions & 1 deletion packages/core/postgrest-js/src/PostgrestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ export default abstract class PostgrestBuilder<
res = await _fetch(this.url.toString(), {
method: this.method,
headers: requestHeaders,
body: JSON.stringify(this.body),
body: JSON.stringify(this.body, (_, value) =>
typeof value === 'bigint' ? value.toString() : value
),
signal: this.signal,
})
} catch (fetchError: any) {
Expand Down
66 changes: 66 additions & 0 deletions packages/core/postgrest-js/test/rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@ const REST_URL = 'http://localhost:54321/rest/v1'
export const postgrest = new PostgrestClient<Database>(REST_URL)

export const RPC_NAME = 'get_username_and_status'
const BIGINT_PARAM = 9223372036854775807n

test('RPC call serializes bigint values in the request body', async () => {
const mockFetch = jest.fn().mockResolvedValue({
ok: true,
status: 200,
statusText: 'OK',
headers: new Headers(),
json: async () => null,
text: async () => '',
})

const client = new PostgrestClient<Database>('https://example.com', {
fetch: mockFetch as any,
})

await client.rpc(
'my_func' as any,
{
bigint_param: BIGINT_PARAM,
payload: { id: BIGINT_PARAM },
} as any
)

const [, options] = mockFetch.mock.calls[0] as [string, RequestInit]
expect(options.method).toBe('POST')
expect(options.body).toBe(
JSON.stringify({
bigint_param: BIGINT_PARAM.toString(),
payload: { id: BIGINT_PARAM.toString() },
})
)
})

test('RPC call with no params', async () => {
const res = await postgrest.rpc(RPC_NAME, { name_param: 'supabot' }).select()
Expand Down Expand Up @@ -213,3 +246,36 @@ test('RPC call with field aggregate', async () => {
expectType<TypeEqual<typeof result, typeof expected>>(true)
ExpectedSchema.parse(res.data)
})

test('RPC call with bigint param is accepted by a bigint Postgres argument', async () => {
const res = await postgrest.rpc('echo_bigint_as_text' as any, { bigint_param: BIGINT_PARAM } as any)

expect(res).toMatchInlineSnapshot(`
{
"count": null,
"data": "9223372036854775807",
"error": null,
"status": 200,
"statusText": "OK",
"success": true,
}
`)
})

test('RPC call with bigint nested in jsonb param is serialized as a JSON string', async () => {
const res = await postgrest.rpc(
'extract_jsonb_bigint_as_text' as any,
{ payload: { id: BIGINT_PARAM } } as any
)

expect(res).toMatchInlineSnapshot(`
{
"count": null,
"data": "9223372036854775807",
"error": null,
"status": 200,
"statusText": "OK",
"success": true,
}
`)
})
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ CREATE FUNCTION public.void_func()
RETURNS void AS $$
$$ LANGUAGE SQL;

create function public.echo_bigint_as_text(bigint_param bigint)
returns text as $$
select bigint_param::text;
$$ language sql immutable;

create function public.extract_jsonb_bigint_as_text(payload jsonb)
returns text as $$
select payload->>'id';
$$ language sql immutable;

-- Extensions schema already exists in Supabase CLI
create extension if not exists postgis schema extensions;

Expand Down
Loading