22 lines
593 B
Python
22 lines
593 B
Python
from collections.abc import AsyncIterator
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
from sqlalchemy.orm import DeclarativeBase
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
def make_session_factory(database_url: str) -> async_sessionmaker[AsyncSession]:
|
|
return async_sessionmaker(
|
|
create_async_engine(database_url, pool_pre_ping=True), expire_on_commit=False
|
|
)
|
|
|
|
|
|
async def session_dependency(
|
|
factory: async_sessionmaker[AsyncSession],
|
|
) -> AsyncIterator[AsyncSession]:
|
|
async with factory() as session:
|
|
yield session
|