acupedia-api/app/db/session.py

30 lines
879 B
Python

from __future__ import annotations
# from typing import AsyncGenerator, Annotated
# from fastapi import Depends
from sqlalchemy.ext.asyncio import (
AsyncSession,
async_sessionmaker,
create_async_engine,
)
from collections.abc import AsyncIterator
from app.core.config import settings
engine = create_async_engine(settings.database_url, pool_pre_ping=True, future=True)
# AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
# async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
# async with AsyncSessionLocal() as session:
# yield session
SessionFactory = async_sessionmaker(
bind=engine,
class_=AsyncSession,
expire_on_commit=False,
autoflush=False,
)
async def get_db_session() -> AsyncIterator[AsyncSession]:
async with SessionFactory() as session:
yield session