mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
33 lines
786 B
Python
33 lines
786 B
Python
from typing import AsyncGenerator
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from the_app import config
|
|
|
|
global_settings = config.get_settings()
|
|
url = global_settings.asyncpg_url
|
|
|
|
engine = create_async_engine(
|
|
url,
|
|
echo=True,
|
|
)
|
|
|
|
# expire_on_commit=False will prevent attributes from being expired
|
|
# after commit.
|
|
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
|
|
|
|
|
|
# Dependency
|
|
async def get_db() -> AsyncGenerator:
|
|
session = async_session()
|
|
try:
|
|
yield session
|
|
await session.commit()
|
|
except SQLAlchemyError as ex:
|
|
await session.rollback()
|
|
raise ex
|
|
finally:
|
|
await session.close()
|