freeleaps-service-hub/apps/notification/webapi/providers/database.py

44 lines
1.1 KiB
Python

from webapi.config.site_settings import site_settings
from beanie import init_beanie
from motor.motor_asyncio import AsyncIOMotorClient
from backend.models.models import MessageTemplateDoc, EmailSenderDoc, EmailSendStatusDoc, EmailTrackingDoc, EmailBounceDoc, UsageLogDoc
import os
# MongoDB config
MONGODB_URI = os.getenv('MONGODB_URI')
MONGODB_NAME = os.getenv('MONGODB_NAME')
# create MongoDB client
client = AsyncIOMotorClient(
MONGODB_URI,
serverSelectionTimeoutMS=60000,
minPoolSize=5,
maxPoolSize=20,
heartbeatFrequencyMS=20000,
)
# define all document models
document_models = [
MessageTemplateDoc,
EmailSenderDoc,
EmailSendStatusDoc,
EmailTrackingDoc,
EmailBounceDoc,
UsageLogDoc
]
def register(app):
app.debug = site_settings.DEBUG
app.title = site_settings.NAME
@app.on_event("startup")
async def start_database():
await initiate_database()
async def initiate_database():
"""initiate Beanie database connection"""
await init_beanie(
database=client[MONGODB_NAME],
document_models=document_models
)