Skip to content

Commit

Permalink
improve: Add count function to async resource service
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkLark86 committed Oct 24, 2024
1 parent 904fe88 commit 6112951
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions superdesk/core/resources/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,25 @@ async def find(

return await self._mongo_find(search_request)

async def count(self, lookup: dict[str, Any] | None = None, use_mongo: bool = False) -> int:
"""Get the number of items that match the lookup, or all items if lookup is not provided
Will use Elasticsearch if configured for this resource and ``use_mongo == False``.
This will not perform a search, but use the item count feature of the underlying data store
:param lookup: Dictionary to search
:param use_mongo: Force to use MongoDB instead of Elasticsearch
:return: The number of items that match the lookup
"""

try:
if not use_mongo:
return await self.elastic.count(lookup)
except KeyError:
pass

return await self.mongo_async.count_documents(lookup or {})

async def _mongo_find(
self, req: SearchRequest, versioned: bool = False
) -> MongoResourceCursorAsync[ResourceModelType]:
Expand Down

0 comments on commit 6112951

Please sign in to comment.