be / app /mongo.py
Lucii1's picture
init prj
8ac4183
import logging
from motor.motor_asyncio import AsyncIOMotorClient
from beanie import init_beanie
from app.config import settings
from app.auth.models import User, UserProfile
async def init_db():
try:
client = AsyncIOMotorClient(
settings.MONGO_DATABASE_CONNECTION_STRING,
serverSelectionTimeoutMS=5000
)
# test ping MongoDB
await client.admin.command("ping")
logging.info("✅ Connected to MongoDB")
db = client.get_default_database()
await init_beanie(
database=db,
document_models=[User, UserProfile]
)
logging.info("✅ Beanie initialized with models")
except Exception as e:
logging.error(f"❌ Failed to connect to MongoDB: {e}")
raise e