Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /nijaru_zenith /examples /task-api /app /database.py
| """ | |
| Database configuration and session management. | |
| """ | |
| from collections.abc import AsyncGenerator | |
| from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine | |
| from sqlmodel import SQLModel | |
| from app.config import settings | |
| # Create async engine | |
| engine = create_async_engine( | |
| settings.DATABASE_URL, | |
| echo=settings.DEBUG, # Log SQL in debug mode | |
| future=True, | |
| ) | |
| # Create session factory | |
| async_session_maker = async_sessionmaker( | |
| engine, class_=AsyncSession, expire_on_commit=False | |
| ) | |
| async def init_db(): | |
| """Initialize database tables.""" | |
| async with engine.begin() as conn: | |
| await conn.run_sync(SQLModel.metadata.create_all) | |
| async def get_session() -> AsyncGenerator[AsyncSession]: | |
| """ | |
| Get database session. | |
| Yields: | |
| Database session for dependency injection | |
| """ | |
| async with async_session_maker() as session: | |
| try: | |
| yield session | |
| await session.commit() | |
| except Exception: | |
| await session.rollback() | |
| raise | |
| finally: | |
| await session.close() | |
Xet Storage Details
- Size:
- 1.1 kB
- Xet hash:
- a10e8cbd6e4f48d36ea7d814514e9928104b093af9d98cab8e71832875cfd3a4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.