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
3 changes: 2 additions & 1 deletion documentation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,8 @@ and `include_total`, and passes any other keyword arguments to CKAN:
records = result["records"]

**get_datastore_records** takes the same query parameters but pages through
all matching rows, yielding each record (`page_size` defaults to 10000):
all matching rows, yielding each record (`page_size` defaults to 10000).
`records_format` csv and tsv are not supported; use `search_datastore` for those:

for record in resource.get_datastore_records(filters={"code": "A"}):
...
Expand Down
9 changes: 8 additions & 1 deletion src/hdx/data/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,11 +930,18 @@ def get_datastore_records(
sort: Comma separated field names with optional asc/desc eg. "code desc". Defaults to None.
distinct: Whether to return only distinct rows. Defaults to False.
page_size: Number of rows to request per call. Defaults to 10000.
**kwargs: Other parameters to pass to datastore_search eg. plain, language.
**kwargs: Other parameters to pass to datastore_search eg. plain, language. records_format csv and tsv are not supported.

Returns:
Iterator of record dictionaries
"""
if page_size < 1:
raise HDXError("page_size must be at least 1!")
records_format = kwargs.get("records_format")
if records_format in ("csv", "tsv"):
raise HDXError(
f"records_format {records_format} is not supported! Use search_datastore instead."
)
offset = 0
while True:
result = self.search_datastore(
Expand Down
4 changes: 4 additions & 0 deletions tests/hdx/data/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,10 @@ def test_datastore_queries(self, configuration, post_datastore_query):
records = list(resource.get_datastore_records(page_size=3))
assert len(records) == 3
assert TestResource.datastore_request["offset"] == 3
with pytest.raises(HDXError):
list(resource.get_datastore_records(page_size=0))
with pytest.raises(HDXError):
list(resource.get_datastore_records(records_format="csv"))

assert resource.get_datastore_fields() == [
{"id": "code", "type": "text"},
Expand Down
Loading