mirror of
https://github.com/arc53/DocsGPT.git
synced 2026-02-19 02:41:26 +00:00
refactor: Use MongoDB singleton for connection management
This commit is contained in:
25
application/core/mongo_db.py
Normal file
25
application/core/mongo_db.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from application.core import settings
|
||||
from pymongo import MongoClient
|
||||
from flask import current_app, g
|
||||
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user