Files
app_03_pregnancy/backend/app/database.py
Karlcarlo 409dabafb5 feat: 初始化孕育无忧全栈项目基础代码结构
完成项目初始搭建,包含后端FastAPI+MongoDB服务、前端React+Vite项目的完整基础结构,添加各类配置文件与依赖清单,涵盖核心业务初始框架。
2026-05-21 00:46:48 +08:00

39 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
import certifi
from beanie import init_beanie
from app.config import settings
from app.models.user import User
from app.models.report import Report
from app.models.analysis import Analysis
from app.models.chat import ChatSession
from app.models.checkup import CheckupPlan
async def init_db():
use_mock = os.environ.get("USE_MONGOMOCK", "").lower() in ("1", "true", "yes")
if use_mock:
# 使用 mongomock 内存数据库(无需安装 MongoDB)
from mongomock_motor import AsyncMongoMockClient
client = AsyncMongoMockClient()
else:
from motor.motor_asyncio import AsyncIOMotorClient
# Only use TLS for Atlas (mongodb+srv://) connections
if settings.mongodb_url.startswith("mongodb+srv://") or "mongodb.net" in settings.mongodb_url:
client = AsyncIOMotorClient(settings.mongodb_url, tlsCAFile=certifi.where())
else:
client = AsyncIOMotorClient(settings.mongodb_url)
await init_beanie(
database=client[settings.mongodb_db_name],
document_models=[
User,
Report,
Analysis,
ChatSession,
CheckupPlan,
],
)