Spaces:
Sleeping
Sleeping
File size: 827 Bytes
da530c3 475aa1b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | """
MongoDB Atlas connection using Motor (async driver).
"""
from motor.motor_asyncio import AsyncIOMotorClient
from config import settings
client: AsyncIOMotorClient | None = None
def get_client() -> AsyncIOMotorClient:
global client
if client is None:
if not settings.MONGODB_URI:
raise RuntimeError(
"MONGODB_URI is not set. Add your MongoDB Atlas connection "
"string as an environment variable / HF Space secret."
)
client = AsyncIOMotorClient(settings.MONGODB_URI)
return client
def get_db():
return get_client()[settings.MONGODB_DB_NAME]
def users_collection():
return get_db()["users"]
def conversations_collection():
return get_db()["conversations"]
def documents_collection():
return get_db()["documents"]
|