fooder-api/fooder/db.py
2023-04-01 16:19:12 +02:00

29 lines
668 B
Python

import logging
from typing import AsyncIterator
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from .settings import Settings
log = logging.getLogger(__name__)
settings = Settings.parse_obj({})
async_engine = create_async_engine(
settings.DB_URI,
pool_pre_ping=True,
echo=settings.ECHO_SQL,
)
AsyncSessionLocal = async_sessionmaker(
bind=async_engine,
autocommit=False,
autoflush=False,
future=True,
)
async def get_session() -> AsyncIterator[async_sessionmaker]:
try:
yield AsyncSessionLocal
except SQLAlchemyError as e:
log.exception(e)