CoolFace
Apppublic

mydatascraper/competitor_compare

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
database.py34 linesDownload Raw Back to app
1from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
2from sqlalchemy.orm import DeclarativeBase
3from app.config import settings
4
5engine = create_async_engine(
6    settings.DATABASE_URL,
7    echo=False,
8    pool_size=20,
9    max_overflow=10,
10    pool_pre_ping=True,
11)
12
13async_session = async_sessionmaker(
14    engine,
15    class_=AsyncSession,
16    expire_on_commit=False,
17)
18
19
20class Base(DeclarativeBase):
21    pass
22
23
24async def get_db() -> AsyncSession:
25    async with async_session() as session:
26        try:
27            yield session
28        finally:
29            await session.close()
30
31
32async def init_db():
33    async with engine.begin() as conn:
34        await conn.run_sync(Base.metadata.create_all)