Skip to content

Commit 2365c8d

Browse files
committed
rename list() to find(), add efficient count()
1 parent f64cab8 commit 2365c8d

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/bubble_data_api_client/client/orm.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pydantic import Field
77

88
from bubble_data_api_client.client.raw_client import RawClient
9-
from bubble_data_api_client.constraints import ConstraintTypes, constraint
9+
from bubble_data_api_client.constraints import Constraint, ConstraintTypes, constraint
1010

1111

1212
def _get_client() -> RawClient:
@@ -48,7 +48,7 @@ async def get_many(cls, uids: list[str]) -> dict[str, typing.Self]:
4848
"""Retrieve multiple things by their unique IDs, keyed by uid."""
4949
if not uids:
5050
return {}
51-
items: list[typing.Self] = await cls.list(
51+
items: list[typing.Self] = await cls.find(
5252
constraints=[constraint("_id", ConstraintTypes.IN, uids)],
5353
)
5454
return {item.uid: item for item in items}
@@ -65,10 +65,10 @@ async def delete(self) -> None:
6565
response.raise_for_status()
6666

6767
@classmethod
68-
async def list(
68+
async def find(
6969
cls,
7070
*,
71-
constraints: list | None = None,
71+
constraints: list[Constraint] | None = None,
7272
cursor: int | None = None,
7373
limit: int | None = None,
7474
sort_field: str | None = None,
@@ -77,7 +77,7 @@ async def list(
7777
additional_sort_fields: list | None = None,
7878
) -> list[typing.Self]:
7979
async with _get_client() as client:
80-
response = await client.list(
80+
response = await client.find(
8181
cls._typename,
8282
constraints=constraints,
8383
cursor=cursor,
@@ -89,3 +89,9 @@ async def list(
8989
)
9090
response.raise_for_status()
9191
return [cls(**item) for item in response.json()["response"]["results"]]
92+
93+
@classmethod
94+
async def count(cls, *, constraints: list[Constraint] | None = None) -> int:
95+
"""Return total count of objects matching constraints."""
96+
async with _get_client() as client:
97+
return await client.count(cls._typename, constraints=constraints)

src/bubble_data_api_client/client/raw_client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async def replace(self, typename: str, uid: str, data: typing.Any) -> httpx.Resp
6464
return await self._transport.put(f"/{typename}/{uid}", json=data)
6565

6666
# https://manual.bubble.io/core-resources/api/the-bubble-api/the-data-api/data-api-requests#get-a-list-of-things
67-
async def list(
67+
async def find(
6868
self,
6969
typename: str,
7070
*,
@@ -94,3 +94,14 @@ async def list(
9494
params["additional_sort_fields"] = json.dumps(additional_sort_fields)
9595

9696
return await self._transport.get(f"/{typename}", params=params)
97+
98+
async def count(
99+
self,
100+
typename: str,
101+
*,
102+
constraints: list[Constraint] | None = None,
103+
) -> int:
104+
"""Return total count of objects matching constraints."""
105+
response = await self.find(typename, constraints=constraints, limit=1)
106+
body = response.json()["response"]
107+
return body["count"] + body["remaining"]

src/tests/integration/test_orm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ async def test_orm_integration():
3232
assert isinstance(thing3, IntegrationTestModel)
3333
assert thing3.text == "test2"
3434

35-
# list
36-
things = await IntegrationTestModel.list(
35+
# find
36+
things = await IntegrationTestModel.find(
3737
constraints=[{"key": "_id", "constraint_type": "equals", "value": thing3.uid}]
3838
)
3939
assert len(things) == 1

0 commit comments

Comments
 (0)