mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2026-01-17 11:40:39 +03:00
refactor
This commit is contained in:
37
app/database.py
Normal file
37
app/database.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app import config
|
||||
|
||||
global_settings = config.get_settings()
|
||||
url = global_settings.asyncpg_url
|
||||
|
||||
engine = create_async_engine(
|
||||
url,
|
||||
future=True,
|
||||
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:
|
||||
async with async_session() as session:
|
||||
try:
|
||||
yield session
|
||||
await session.commit()
|
||||
except SQLAlchemyError as sql_ex:
|
||||
await session.rollback()
|
||||
raise sql_ex
|
||||
except HTTPException as http_ex:
|
||||
await session.rollback()
|
||||
raise http_ex
|
||||
finally:
|
||||
await session.close()
|
||||
Reference in New Issue
Block a user