29 lines
633 B
Python
29 lines
633 B
Python
from __future__ import annotations
|
|
|
|
from collections.abc import AsyncIterator
|
|
|
|
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase
|
|
|
|
from fastcheck_api.app.core.config import settings
|
|
|
|
_client: AsyncIOMotorClient | None = None
|
|
|
|
|
|
def get_client() -> AsyncIOMotorClient:
|
|
global _client
|
|
if _client is None:
|
|
_client = AsyncIOMotorClient(settings.mongodb_uri)
|
|
return _client
|
|
|
|
|
|
def get_db() -> AsyncIOMotorDatabase:
|
|
return get_client()[settings.mongo_db_name]
|
|
|
|
|
|
async def lifespan_db() -> AsyncIterator[None]:
|
|
client = get_client()
|
|
try:
|
|
yield
|
|
finally:
|
|
client.close()
|