24 lines
696 B
Python
24 lines
696 B
Python
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase
|
|
|
|
from core.config import settings
|
|
|
|
|
|
class _MongoDB:
|
|
client: AsyncIOMotorClient | None = None
|
|
|
|
def get_database(self) -> AsyncIOMotorDatabase:
|
|
if self.client is None:
|
|
raise RuntimeError("MongoDB client has not been initialized")
|
|
return self.client[settings.MONGODB_DB_NAME]
|
|
|
|
mongodb = _MongoDB()
|
|
|
|
async def get_database() -> AsyncIOMotorDatabase:
|
|
return mongodb.get_database()
|
|
|
|
|
|
async def ensure_mongodb_available() -> None:
|
|
client = mongodb.client
|
|
if client is None:
|
|
raise RuntimeError("MongoDB client has not been initialized")
|
|
await client.admin.command("ping") |