You've already forked DataMate
* feat: Front-end data annotation page adaptation to the backend API. * feat: Implement labeling configuration editor and enhance annotation task creation form * feat: add python backend build and deployment; add backend configuration for Label Studio integration and improve logging setup * refactor: remove duplicate log configuration
32 lines
851 B
Python
32 lines
851 B
Python
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
|
from sqlalchemy.orm import declarative_base
|
|
from app.core.config import settings
|
|
from app.core.logging import get_logger
|
|
from typing import AsyncGenerator
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
# 创建数据库引擎
|
|
engine = create_async_engine(
|
|
settings.database_url,
|
|
echo=False, # 关闭SQL调试日志以减少输出
|
|
future=True
|
|
)
|
|
|
|
# 创建会话工厂
|
|
AsyncSessionLocal = async_sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False
|
|
)
|
|
|
|
# 创建基础模型类
|
|
Base = declarative_base()
|
|
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
|
"""获取数据库会话"""
|
|
async with AsyncSessionLocal() as session:
|
|
try:
|
|
yield session
|
|
finally:
|
|
await session.close()
|
|
|