refactor(database): move the initialization of database into tenant middleware
This commit is contained in:
parent
e726d7e7d5
commit
9dc8811886
@ -1,44 +1,10 @@
|
||||
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):
|
||||
"""Register database-related configurations"""
|
||||
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
|
||||
)
|
||||
# Database initialization is now handled by TenantDBConnectionMiddleware
|
||||
# to support per-request tenant database switching
|
||||
Loading…
Reference in New Issue
Block a user