from motor.motor_asyncio import AsyncIOMotorClient import os from dotenv import load_dotenv # Load environment from .env if present load_dotenv() MONGODB_URI = os.getenv("MONGODB_URI", "mongodb://localhost:27017/") # Optional database name (useful for MongoDB SRV URIs that don't include a DB) MONGODB_DB = os.getenv("MONGODB_DB", "ucprojects") client: AsyncIOMotorClient | None = None def get_client() -> AsyncIOMotorClient: global client if client is None: client = AsyncIOMotorClient(MONGODB_URI) return client def get_db(): # Return a named database. If the URI already contains a default DB, this # will still work because get_database accepts the explicit name. return get_client().get_database(MONGODB_DB)