from typing import Optional from motor.motor_asyncio import AsyncIOMotorClient from .config import MONGO_DB, MONGO_URI mongo_client: Optional[AsyncIOMotorClient] = None db = None async def connect_to_mongo(): """Create a single shared Mongo client + DB handle.""" global mongo_client, db if mongo_client: return db mongo_client = AsyncIOMotorClient(MONGO_URI) db = mongo_client[MONGO_DB] return db async def close_mongo(): global mongo_client if mongo_client: mongo_client.close() mongo_client = None