29 lines
733 B
Python
29 lines
733 B
Python
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase
|
|
|
|
from app.core.config import settings
|
|
|
|
client: AsyncIOMotorClient | None = None
|
|
|
|
|
|
def get_database() -> AsyncIOMotorDatabase:
|
|
if client is None:
|
|
raise RuntimeError("MongoDB client has not been initialized")
|
|
return client[settings.mongo_db_name]
|
|
|
|
|
|
def get_client() -> AsyncIOMotorClient:
|
|
if client is None:
|
|
raise RuntimeError("MongoDB client has not been initialized")
|
|
return client
|
|
|
|
|
|
async def connect_to_mongo() -> None:
|
|
global client
|
|
client = AsyncIOMotorClient(settings.mongo_uri)
|
|
|
|
|
|
async def close_mongo_connection() -> None:
|
|
global client
|
|
if client is not None:
|
|
client.close()
|
|
client = None
|