mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
28 lines
792 B
Python
28 lines
792 B
Python
from collections.abc import AsyncGenerator
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app import config
|
|
from app.utils.logging import AppLogger
|
|
|
|
global_settings = config.get_settings()
|
|
logger = AppLogger.__call__().get_logger()
|
|
|
|
engine = create_async_engine(
|
|
global_settings.asyncpg_url.unicode_string(),
|
|
future=True,
|
|
echo=True,
|
|
)
|
|
|
|
# expire_on_commit=False will prevent attributes from being expired
|
|
# after commit.
|
|
AsyncSessionFactory = sessionmaker(engine, autoflush=False, expire_on_commit=False, class_=AsyncSession)
|
|
|
|
|
|
# Dependency
|
|
async def get_db() -> AsyncGenerator:
|
|
async with AsyncSessionFactory() as session:
|
|
# logger.debug(f"ASYNC Pool: {engine.pool.status()}")
|
|
yield session
|