Skip to content

Commit

Permalink
Added function decorator to handle db issues
Browse files Browse the repository at this point in the history
  • Loading branch information
moonraker595 committed Nov 29, 2024
1 parent b5210ba commit 6a190a3
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions operationsgateway_api/src/mongo/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]],
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -273,6 +283,7 @@ async def count_documents(
) from exc

@staticmethod
@mongodb_error_handling("aggregate")
async def aggregate(
collection_name: str,
pipeline,
Expand Down

0 comments on commit 6a190a3

Please sign in to comment.