Skip to content
This repository was archived by the owner on Sep 8, 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
6 changes: 6 additions & 0 deletions infra/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ as $function$
select * from countries;
$function$;

create or replace function public.search_countries_by_name(search_name text)
returns setof countries
language sql
as $function$
select * from countries where nicename ilike '%' || search_name || '%';
$function$;

create table
orchestral_sections (id int8 primary key, name text);
Expand Down
9 changes: 9 additions & 0 deletions postgrest/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ def rpc(

headers = Headers({"Prefer": f"count={count}"}) if count else Headers()

if method in ("HEAD", "GET"):
return AsyncRPCFilterRequestBuilder[Any](
self.session,
f"/rpc/{func}",
method,
headers,
QueryParams(params),
json={},
)
# the params here are params to be sent to the RPC and not the queryparams!
return AsyncRPCFilterRequestBuilder[Any](
self.session, f"/rpc/{func}", method, headers, QueryParams(), json=params
Expand Down
9 changes: 9 additions & 0 deletions postgrest/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ def rpc(

headers = Headers({"Prefer": f"count={count}"}) if count else Headers()

if method in ("HEAD", "GET"):
return SyncRPCFilterRequestBuilder[Any](
self.session,
f"/rpc/{func}",
method,
headers,
QueryParams(params),
json={},
)
# the params here are params to be sent to the RPC and not the queryparams!
return SyncRPCFilterRequestBuilder[Any](
self.session, f"/rpc/{func}", method, headers, QueryParams(), json=params
Expand Down
6 changes: 5 additions & 1 deletion postgrest/base_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,11 @@ def select(
"""
method, params, headers, json = pre_select(*columns, count=None)
self.params = self.params.add("select", params.get("select"))
self.headers["Prefer"] = "return=representation"
if self.headers.get("Prefer"):
self.headers["Prefer"] += ",return=representation"
else:
self.headers["Prefer"] = "return=representation"

return self

def single(self) -> Self:
Expand Down
42 changes: 42 additions & 0 deletions tests/_sync/test_filter_request_builder_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,48 @@ def test_rpc_with_range():
]


def test_rpc_post_with_args():
res = (
rest_client()
.rpc("search_countries_by_name", {"search_name": "Alban"})
.select("nicename, iso")
.execute()
)
assert res.data == [{"nicename": "Albania", "iso": "AL"}]


def test_rpc_get_with_args():
res = (
rest_client()
.rpc("search_countries_by_name", {"search_name": "Alger"}, get=True)
.select("nicename, iso")
.execute()
)
assert res.data == [{"nicename": "Algeria", "iso": "DZ"}]


def test_rpc_get_with_count():
res = (
rest_client()
.rpc("search_countries_by_name", {"search_name": "Al"}, get=True, count="exact")
.select("nicename")
.execute()
)
assert res.count == 2
assert res.data == [{"nicename": "Albania"}, {"nicename": "Algeria"}]


def test_rpc_head_count():
res = (
rest_client()
.rpc("search_countries_by_name", {"search_name": "Al"}, head=True, count="exact")
.execute()
)

assert res.count == 2
assert res.data == []


def test_order():
res = (
rest_client()
Expand Down