mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
27 lines
766 B
Python
27 lines
766 B
Python
from collections.abc import AsyncGenerator
|
|
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
from sqlalchemy.ext.asyncio import async_sessionmaker
|
|
|
|
from app.config import settings as global_settings
|
|
from app.utils.logging import AppLogger
|
|
|
|
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 = async_sessionmaker(engine, autoflush=False, expire_on_commit=False,)
|
|
|
|
|
|
# Dependency
|
|
async def get_db() -> AsyncGenerator:
|
|
async with AsyncSessionFactory() as session:
|
|
# logger.debug(f"ASYNC Pool: {engine.pool.status()}")
|
|
yield session
|