From 6a190a35bb51eac6f16cf097b72cbd2faf4f7292 Mon Sep 17 00:00:00 2001 From: kdu14933 Date: Fri, 29 Nov 2024 14:57:10 +0000 Subject: [PATCH] Added function decorator to handle db issues --- operationsgateway_api/src/mongo/interface.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/operationsgateway_api/src/mongo/interface.py b/operationsgateway_api/src/mongo/interface.py index a21d54b1..f3a81f3f 100644 --- a/operationsgateway_api/src/mongo/interface.py +++ b/operationsgateway_api/src/mongo/interface.py @@ -14,6 +14,7 @@ from operationsgateway_api.src.config import Config from operationsgateway_api.src.exceptions import DatabaseError from operationsgateway_api.src.mongo.connection import ConnectionInstance +from operationsgateway_api.src.mongo.mongo_error_handling import mongodb_error_handling log = logging.getLogger() ProjectionAlias = Optional[Union[Mapping[str, Any], Iterable[str]]] @@ -22,16 +23,17 @@ class MongoDBInterface: """ An implementation of various PyMongo and Motor functions that suit our specific - database and colllection names + database and collection names Motor doesn't support type annotations (see https://jira.mongodb.org/browse/MOTOR-331 for any updates) so type annotations are - used from PyMongo which from a user perspective, acts almost identically (exlcuding + used from PyMongo which from a user perspective, acts almost identically (excluding async support of course). This means the type hinting can actually be useful for developers of this repo """ @staticmethod + @mongodb_error_handling("get_collection_object") def get_collection_object(collection_name: str) -> Collection: """ Simple getter function which gets a particular collection so it can be @@ -44,6 +46,7 @@ def get_collection_object(collection_name: str) -> Collection: raise DatabaseError("Invalid collection name given") from exc @staticmethod + @mongodb_error_handling("find") def find( collection_name: str = "images", filter_: dict = None, @@ -99,6 +102,7 @@ async def query_to_list(query: Cursor) -> List[Dict[str, Any]]: return await query.to_list(length=Config.config.mongodb.max_documents) @staticmethod + @mongodb_error_handling("find_one") async def find_one( collection_name: str, filter_: Dict[str, Any] = None, @@ -120,6 +124,7 @@ async def find_one( return await collection.find_one(filter_, sort=sort, projection=projection) @staticmethod + @mongodb_error_handling("update_one") async def update_one( collection_name: str, filter_: Dict[str, Any] = None, @@ -153,6 +158,7 @@ async def update_one( ) from exc @staticmethod + @mongodb_error_handling("update_many") async def update_many( collection_name: str, filter_: Dict[str, Any] = {}, # noqa: B006 @@ -177,6 +183,7 @@ async def update_many( ) from exc @staticmethod + @mongodb_error_handling("insert_one") async def insert_one(collection_name: str, data: Dict[str, Any]) -> InsertOneResult: """ Using the input data, insert a single document into a given collection @@ -194,6 +201,7 @@ async def insert_one(collection_name: str, data: Dict[str, Any]) -> InsertOneRes ) from exc @staticmethod + @mongodb_error_handling("insert_many") async def insert_many( collection_name: str, data: List[Dict[str, Any]], @@ -215,6 +223,7 @@ async def insert_many( ) from exc @staticmethod + @mongodb_error_handling("delete_one") async def delete_one( collection_name: str, filter_: Dict[str, Any] = None, @@ -245,6 +254,7 @@ async def delete_one( ) from exc @staticmethod + @mongodb_error_handling("count_documents") async def count_documents( collection_name: str, filter_: Dict[str, Any] = None, @@ -273,6 +283,7 @@ async def count_documents( ) from exc @staticmethod + @mongodb_error_handling("aggregate") async def aggregate( collection_name: str, pipeline,