Skip to content

Commit 9921621

Browse files
committed
Add method to query data by type and session ID
Introduces get_data_by_type_and_session_id to DatabaseBase and implements it in CosmosDBClient. This method allows querying documents by data_type, session_id, and user_id, improving data retrieval granularity.
1 parent e06dde1 commit 9921621

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/backend/common/database/cosmosdb.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,27 @@ async def delete_team(self, team_id: str) -> bool:
361361
logging.exception(f"Failed to delete team from Cosmos DB: {e}")
362362
return False
363363

364+
async def get_data_by_type_and_session_id(
365+
self, data_type: str, session_id: str
366+
) -> List[BaseDataModel]:
367+
"""Query the Cosmos DB for documents with the matching data_type, session_id and user_id."""
368+
await self._ensure_initialized()
369+
if self.container is None:
370+
return []
371+
372+
model_class = self.MODEL_CLASS_MAPPING.get(data_type, BaseDataModel)
373+
try:
374+
query = "SELECT * FROM c WHERE c.session_id=@session_id AND c.user_id=@user_id AND c.data_type=@data_type ORDER BY c._ts ASC"
375+
parameters = [
376+
{"name": "@session_id", "value": session_id},
377+
{"name": "@data_type", "value": data_type},
378+
{"name": "@user_id", "value": self.user_id},
379+
]
380+
return await self.query_items(query, parameters, model_class)
381+
except Exception as e:
382+
logging.exception(f"Failed to query data by type from Cosmos DB: {e}")
383+
return []
384+
364385
# Data Management Operations
365386
async def get_data_by_type(self, data_type: str) -> List[BaseDataModel]:
366387
"""Retrieve all data of a specific type."""

src/backend/common/database/database_base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ async def get_all_plans(self) -> List[Plan]:
105105
"""Retrieve all plans for the user."""
106106
pass
107107

108+
@abstractmethod
109+
async def get_data_by_type_and_session_id(
110+
self, data_type: str, session_id: str
111+
) -> List[BaseDataModel]:
112+
pass
113+
108114
# Step Operations
109115
@abstractmethod
110116
async def add_step(self, step: Step) -> None:

0 commit comments

Comments
 (0)