29 lines
729 B
Python
29 lines
729 B
Python
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.orm import declarative_base, sessionmaker
|
|
|
|
from models.db_config import (
|
|
build_database_url,
|
|
create_database_engine,
|
|
read_database_config,
|
|
)
|
|
|
|
_config = read_database_config()
|
|
DATABASE_URL = build_database_url(_config)
|
|
engine = create_database_engine(_config)
|
|
AsyncSessionLocal = sessionmaker(
|
|
engine, class_=AsyncSession, expire_on_commit=False
|
|
)
|
|
Base = declarative_base()
|
|
|
|
|
|
async def get_db():
|
|
async with AsyncSessionLocal() as session:
|
|
try:
|
|
yield session
|
|
await session.commit()
|
|
except Exception:
|
|
await session.rollback()
|
|
raise
|
|
finally:
|
|
await session.close()
|