mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
28 lines
702 B
Python
28 lines
702 B
Python
from typing import AsyncGenerator
|
|
|
|
from fastapi.encoders import jsonable_encoder
|
|
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,
|
|
json_serializer=jsonable_encoder,
|
|
)
|
|
|
|
# expire_on_commit=False will prevent attributes from being expired
|
|
# after commit.
|
|
async_session_factory = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
|
|
|
|
|
|
# Dependency
|
|
async def get_db() -> AsyncGenerator:
|
|
async with async_session_factory() as session:
|
|
yield session
|