2022-06-22 13:11:22 -05:00
|
|
|
from typing import Any
|
2022-06-29 13:43:17 -05:00
|
|
|
from typing import AsyncGenerator
|
2022-06-22 13:11:22 -05:00
|
|
|
|
2022-07-17 11:43:08 -05:00
|
|
|
from sqlalchemy import MetaData
|
2022-06-22 13:11:22 -05:00
|
|
|
from sqlalchemy import create_engine
|
2022-06-29 13:43:17 -05:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
2022-06-22 13:11:22 -05:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
|
2022-06-29 13:43:17 -05:00
|
|
|
from app.config import DB_PATH
|
2022-06-22 13:11:22 -05:00
|
|
|
from app.config import SQLALCHEMY_DATABASE_URL
|
|
|
|
|
|
|
|
engine = create_engine(
|
|
|
|
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
|
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
2022-06-29 13:43:17 -05:00
|
|
|
DATABASE_URL = f"sqlite+aiosqlite:///{DB_PATH}"
|
|
|
|
async_engine = create_async_engine(DATABASE_URL, future=True, echo=False)
|
|
|
|
async_session = sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
2022-06-22 13:11:22 -05:00
|
|
|
Base: Any = declarative_base()
|
2022-07-17 11:43:08 -05:00
|
|
|
metadata_obj = MetaData()
|
2022-06-22 13:11:22 -05:00
|
|
|
|
|
|
|
|
2022-06-29 13:43:17 -05:00
|
|
|
async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
|
|
|
|
async with async_session() as session:
|
2022-07-14 05:13:23 -05:00
|
|
|
try:
|
|
|
|
yield session
|
|
|
|
finally:
|
|
|
|
await session.close()
|