- uv(workspace) + pnpm monorepo;docker-compose(pg+api+web) - SQLAlchemy 16 MVP 表 + Alembic 初版迁移(无漂移,users stub) - FastAPI 骨架:统一错误信封(带 request_id) + structlog + /jobs/:id + OpenAPI - Next.js 骨架:纸感主题 token + OpenAPI→TS 客户端代码生成(gen:api) - CI(ruff/mypy/pytest + pg service + alembic 漂移校验) - 四份设计规格(PRODUCT/UX/ARCHITECTURE/DEV_PLAN) + CLAUDE.md
28 lines
762 B
Python
28 lines
762 B
Python
"""async engine / session(ARCH §2.3:同进程,async-first)。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from collections.abc import AsyncIterator
|
||
from functools import lru_cache
|
||
|
||
from sqlalchemy.ext.asyncio import (
|
||
AsyncSession,
|
||
async_sessionmaker,
|
||
create_async_engine,
|
||
)
|
||
from ww_config import get_settings
|
||
|
||
|
||
@lru_cache
|
||
def get_sessionmaker() -> async_sessionmaker[AsyncSession]:
|
||
settings = get_settings()
|
||
engine = create_async_engine(settings.database_url, pool_pre_ping=True)
|
||
return async_sessionmaker(engine, expire_on_commit=False)
|
||
|
||
|
||
async def get_session() -> AsyncIterator[AsyncSession]:
|
||
"""FastAPI 依赖:每请求一个 session。"""
|
||
sm = get_sessionmaker()
|
||
async with sm() as session:
|
||
yield session
|