updated and cleaned

This commit is contained in:
2025-03-24 13:36:14 +03:00
parent 713730420c
commit 22876d250d
26 changed files with 161 additions and 98 deletions

View File

@@ -1,5 +1,6 @@
from typing import Any, Dict, List, Optional
from functools import wraps
# from pymongo.errors import (
# ConnectionFailure,
# OperationFailure,
@@ -22,4 +23,5 @@ def mongo_error_wrapper(func):
:return:
"""
return func(*args, **kwargs)
return wrapper

View File

@@ -10,11 +10,13 @@ from Services.MongoService.handlers import mongo_error_wrapper
class MongoBase:
"""Base class for MongoDB connection and operations."""
collection: Collection = None
class MongoErrorHandler:
"""Error handler for MongoDB operations."""
...
@@ -63,7 +65,6 @@ class MongoFindMixin(MongoBase):
class MongoUpdateMixin(MongoBase):
"""Mixin for MongoDB update operations."""
@mongo_error_wrapper
@@ -100,9 +101,10 @@ class MongoDeleteMixin(MongoBase):
"""Delete multiple documents from the collection."""
return self.collection.delete_many(filter_query)
class MongoAggregateMixin(MongoBase):
class MongoAggregateMixin(MongoBase):
"""Mixin for MongoDB aggregation operations."""
@mongo_error_wrapper
def aggregate(self, collection: Collection, pipeline: List[Dict[str, Any]]):
"""Execute an aggregation pipeline on the collection."""
@@ -110,7 +112,6 @@ class MongoAggregateMixin(MongoBase):
return result
class MongoProvider(
MongoUpdateMixin,
MongoInsertMixin,
@@ -124,9 +125,7 @@ class MongoProvider(
managing collections based on company UUID and storage reason.
"""
def __init__(
self, client: MongoClient, database: str, storage_reason: list[str]
):
def __init__(self, client: MongoClient, database: str, storage_reason: list[str]):
"""Initialize MongoDB actions with client and collection info.
Args:
@@ -172,7 +171,9 @@ class MongoProvider(
collection_name = ""
for each_storage_reason in storage_name_list:
if self.delimiter in str(each_storage_reason):
raise ValueError(f"Storage reason cannot contain delimiter : {self.delimiter}")
raise ValueError(
f"Storage reason cannot contain delimiter : {self.delimiter}"
)
collection_name += f"{self.delimiter}{each_storage_reason}"
collection_name = collection_name[1:]
self._collection = self._client[self._database][collection_name]