| | from typing import Optional |
| | from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase, AsyncIOMotorCollection |
| | from . import __init__ |
| | from ..config.settings import MONGODB_URL, DATABASE_NAME, COLLECTION_NAME |
| |
|
| | _client: Optional[AsyncIOMotorClient] = None |
| | _db: Optional[AsyncIOMotorDatabase] = None |
| | _collection: Optional[AsyncIOMotorCollection] = None |
| |
|
| | async def connect_to_mongo() -> None: |
| | global _client, _db, _collection |
| | _client = AsyncIOMotorClient(MONGODB_URL) |
| | _db = _client[DATABASE_NAME] |
| | _collection = _db[COLLECTION_NAME] |
| |
|
| | async def close_mongo_connection() -> None: |
| | global _client |
| | if _client: |
| | _client.close() |
| |
|
| | def get_db() -> AsyncIOMotorDatabase: |
| | assert _db is not None, "DB not initialized. Call connect_to_mongo() first." |
| | return _db |
| |
|
| | def get_collection() -> AsyncIOMotorCollection: |
| | assert _collection is not None, "Collection not initialized. Call connect_to_mongo() first." |
| | return _collection |
| |
|