Merge pull request #1335 from kom-senapati/feature/mongodb-connection-refactor

refactor: use MongoDB singleton for connection management
This commit is contained in:
Alex authored and GitHub committed 2024-10-31 11:36:01 +00:00
commit ed12c2d527
8 files changed
+36 -13

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
from application.core.settings import settings
from pymongo import MongoClient
class MongoDB:
_client = None
@classmethod
def get_client(cls):
"""
Get the MongoDB client instance, creating it if necessary.
"""
if cls._client is None:
cls._client = MongoClient(settings.MONGO_URI)
return cls._client
@classmethod
def close_client(cls):
"""
Close the MongoDB client connection.
"""
if cls._client is not None:
cls._client.close()
cls._client = None