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

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

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