Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.
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
5 changes: 4 additions & 1 deletion src/PostgrestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,24 @@ export default class PostgrestClient {
*
* @param fn The function name to call.
* @param params The parameters to pass to the function call.
* @param head When set to true, no data will be returned.
* @param count Count algorithm to use to count rows in a table.
*/
rpc<T = any>(
fn: string,
params?: object,
{
head = false,
count = null,
}: {
head?: boolean
count?: null | 'exact' | 'planned' | 'estimated'
} = {}
): PostgrestFilterBuilder<T> {
const url = `${this.url}/rpc/${fn}`
return new PostgrestRpcBuilder<T>(url, {
headers: this.headers,
schema: this.schema,
}).rpc(params, { count })
}).rpc(params, { head, count })
}
}
16 changes: 14 additions & 2 deletions src/lib/PostgrestRpcBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ export default class PostgrestRpcBuilder<T> extends PostgrestBuilder<T> {
rpc(
params?: object,
{
head = false,
count = null,
}: {
head?: boolean
count?: null | 'exact' | 'planned' | 'estimated'
} = {}
): PostgrestFilterBuilder<T> {
this.method = 'POST'
this.body = params
if (head) {
this.method = 'HEAD'

if (params) {
Object.entries(params).forEach(([name, value]) => {
this.url.searchParams.append(name, value)
})
}
} else {
this.method = 'POST'
this.body = params
}

if (count) {
if (this.headers['Prefer'] !== undefined) this.headers['Prefer'] += `,count=${count}`
Expand Down
11 changes: 11 additions & 0 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,17 @@ Object {
}
`;

exports[`rpc with head:true, count:exact 1`] = `
Object {
"body": null,
"count": 1,
"data": null,
"error": null,
"status": 200,
"statusText": "OK",
}
`;

exports[`select on insert 1`] = `
Object {
"body": Array [
Expand Down
9 changes: 9 additions & 0 deletions test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ test("rpc with count: 'exact'", async () => {
expect(res).toMatchSnapshot()
})

test('rpc with head:true, count:exact', async () => {
const res = await postgrest.rpc(
'get_status',
{ name_param: 'supabot' },
{ head: true, count: 'exact' }
)
expect(res).toMatchSnapshot()
})

describe("insert, update, delete with count: 'exact'", () => {
test("insert with count: 'exact'", async () => {
let res = await postgrest
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"rootDir": "src",
"sourceMap": true,
"target": "ES2015",
"lib": ["dom", "ES2017"],

"strict": true,

Expand Down