Skip to content

Commit 2b20851

Browse files
Add count and find-one methods (#6)
* Raise exception for unsuccessful requests * Add request call * Add count * Add find one * Add examples to README.md * Fix README for count Co-authored-by: Samuel López <[email protected]> --------- Co-authored-by: Samuel López <[email protected]>
1 parent ed17169 commit 2b20851

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ table_rows = client.table_row_list(project, table_name, params={'offset': 100})
101101
table_rows = client.table_row_list(project, table_name, InFilter("name", "sam"))
102102
table_rows = client.table_row_list(project, table_name, filter_obj=EqFilter("Id", 100))
103103

104+
# Filter and count rows
105+
count = client.table_count(project, table_name, filter_obj=EqFilter("Id", 100))
106+
107+
# Find one row
108+
table_row = client.table_find_one(project, table_name, filter_obj=EqFilter("Id", 100), params={"sort": "-created_at"})
109+
104110
# Retrieve a single row
105111
row_id = 10
106112
row = client.table_row_detail(project, table_name, row_id)

nocodb/api.py

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ def get_table_uri(self, project: NocoDBProject, table: str) -> str:
2626
)
2727
)
2828

29+
def get_table_count_uri(self, project: NocoDBProject, table: str) -> str:
30+
return "/".join(
31+
(
32+
self.get_table_uri(project, table),
33+
'count'
34+
)
35+
)
36+
37+
def get_table_find_one_uri(self, project: NocoDBProject, table: str) -> str:
38+
return "/".join(
39+
(
40+
self.get_table_uri(project, table),
41+
'find-one'
42+
)
43+
)
44+
2945
def get_row_detail_uri(
3046
self, project: NocoDBProject, table: str, row_id: int
3147
):

nocodb/infra/requests_client.py

+25
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ def table_row_delete(self, project: NocoDBProject, table: str, row_id: int) -> i
7777
self.__api_info.get_row_detail_uri(project, table, row_id),
7878
).json()
7979

80+
def table_count(
81+
self,
82+
project: NocoDBProject,
83+
table: str,
84+
filter_obj: Optional[WhereFilter] = None,
85+
) -> dict:
86+
return self._request(
87+
"GET",
88+
self.__api_info.get_table_count_uri(project, table),
89+
params=get_query_params(filter_obj),
90+
).json()
91+
92+
def table_find_one(
93+
self,
94+
project: NocoDBProject,
95+
table: str,
96+
filter_obj: Optional[WhereFilter] = None,
97+
params: Optional[dict] = None,
98+
) -> dict:
99+
return self._request(
100+
"GET",
101+
self.__api_info.get_table_find_one_uri(project, table),
102+
params=get_query_params(filter_obj, params),
103+
).json()
104+
80105
def table_row_nested_relations_list(
81106
self,
82107
project: NocoDBProject,

nocodb/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def get_query_params(filter_obj, params) -> dict:
1+
def get_query_params(filter_obj, params=None) -> dict:
22
query_params = params or {}
33
if filter_obj:
44
query_params["where"] = filter_obj.get_where()

0 commit comments

Comments
 (0)