angeetoile's picture
feat(database): connect API to Neon PostgreSQL
2a09328
Raw
History Blame Contribute Delete
743 Bytes
from collections.abc import AsyncIterator
from sqlalchemy.ext.asyncio import (
AsyncSession,
async_sessionmaker,
create_async_engine,
)
from app.core.config import settings
engine = create_async_engine(
settings.sqlalchemy_database_url,
pool_pre_ping=True,
pool_recycle=300,
echo=False,
)
AsyncSessionFactory = async_sessionmaker(
bind=engine,
class_=AsyncSession,
autoflush=False,
expire_on_commit=False,
)
async def get_database_session() -> AsyncIterator[AsyncSession]:
async with AsyncSessionFactory() as session:
try:
yield session
except Exception:
await session.rollback()
raise
finally:
await session.close()