26 lines
625 B
Python
26 lines
625 B
Python
from pymongo import AsyncMongoClient
|
|
from beanie import init_beanie
|
|
|
|
from app.models import User
|
|
from app.settings import settings
|
|
|
|
|
|
async def init_db():
|
|
"""Initialize database connection and Beanie ODM"""
|
|
try:
|
|
client = AsyncMongoClient(settings.MONGODB_URL)
|
|
await client.admin.command('ping')
|
|
|
|
await init_beanie(
|
|
database=client[settings.MONGODB_DATABASE],
|
|
document_models=[User]
|
|
)
|
|
except Exception as e:
|
|
raise
|
|
|
|
|
|
async def close_db():
|
|
"""Close database connection"""
|
|
client = AsyncMongoClient(settings.MONGODB_URL)
|
|
await client.close()
|