2022-11-22 18:06:19 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from typing import Any
|
|
|
|
from typing import AsyncGenerator
|
|
|
|
|
|
|
|
from sqlalchemy import MetaData
|
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
2024-09-29 19:46:11 +02:00
|
|
|
from sqlalchemy.orm import DeclarativeBase
|
2022-11-22 18:06:19 +01:00
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
|
|
|
|
from app.config import DB_PATH
|
|
|
|
from app.config import DEBUG
|
|
|
|
from app.config import SQLALCHEMY_DATABASE_URL
|
|
|
|
|
2023-08-02 14:53:09 +02:00
|
|
|
|
2022-11-22 18:06:19 +01:00
|
|
|
engine = create_engine(
|
|
|
|
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False, "timeout": 15}
|
|
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
|
|
DATABASE_URL = f"sqlite+aiosqlite:///{DB_PATH}"
|
|
|
|
async_engine = create_async_engine(
|
|
|
|
DATABASE_URL, future=True, echo=DEBUG, connect_args={"timeout": 15}
|
|
|
|
)
|
|
|
|
async_session = sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
2024-09-29 19:46:11 +02:00
|
|
|
class Base(DeclarativeBase):
|
|
|
|
pass
|
|
|
|
|
2022-11-22 18:06:19 +01:00
|
|
|
metadata_obj = MetaData()
|
|
|
|
|
|
|
|
async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
|
|
|
|
async with async_session() as session: # type: ignore
|
|
|
|
try:
|
|
|
|
yield session
|
|
|
|
finally:
|
|
|
|
await session.close()
|