Initial commit
This commit is contained in:
25
app/database.py
Normal file
25
app/database.py
Normal file
@ -0,0 +1,25 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user