first commit
This commit is contained in:
20
backend/app/db.py
Normal file
20
backend/app/db.py
Normal file
@@ -0,0 +1,20 @@
|
||||
"""async SQLAlchemy 引擎与会话。"""
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
from .config import settings
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
"""所有 ORM 模型的基类。"""
|
||||
|
||||
|
||||
# echo=False;生产环境可用连接池参数调优
|
||||
engine = create_async_engine(settings.database_url, echo=False, future=True)
|
||||
async_session = async_sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
|
||||
|
||||
|
||||
async def get_session() -> AsyncSession:
|
||||
"""FastAPI 依赖:提供一个事务会话。"""
|
||||
async with async_session() as session:
|
||||
yield session
|
||||
Reference in New Issue
Block a user