File size: 711 Bytes
e8e33af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from pathlib import Path
from collections.abc import AsyncGenerator
# Create data directory if it doesn't exist
DATA_DIR = Path("./data/database")
DATA_DIR.mkdir(parents=True, exist_ok=True)

DATABASE_URL = "sqlite+aiosqlite:///./data/database/ocr_results.db"

engine = create_async_engine(
    DATABASE_URL,
    echo=False
)

async_session_maker = async_sessionmaker(
    bind=engine,
    class_=AsyncSession,
    expire_on_commit=False
)


async def get_session() -> AsyncGenerator[AsyncSession]:
    """Dependency to get async database session."""
    async with async_session_maker() as session:
        yield session