
- Set up project structure with FastAPI and SQLAlchemy - Configure SQLite database with async support - Implement CRUD endpoints for items resource - Add health endpoint for monitoring - Set up Alembic migrations - Create comprehensive documentation generated with BackendIM... (backend.im)
32 lines
803 B
Python
32 lines
803 B
Python
from pathlib import Path
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
from sqlalchemy.orm import sessionmaker
|
|
from typing import Generator
|
|
|
|
DB_DIR = Path("/app/storage/db")
|
|
DB_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
SQLALCHEMY_DATABASE_URL = f"sqlite+aiosqlite:///{DB_DIR}/db.sqlite"
|
|
|
|
engine = create_async_engine(
|
|
SQLALCHEMY_DATABASE_URL,
|
|
connect_args={"check_same_thread": False},
|
|
echo=True
|
|
)
|
|
|
|
SessionLocal = sessionmaker(
|
|
autocommit=False,
|
|
autoflush=False,
|
|
bind=engine,
|
|
class_=AsyncSession
|
|
)
|
|
|
|
async def get_db() -> Generator[AsyncSession, None, None]:
|
|
"""
|
|
Dependency for getting async db session.
|
|
"""
|
|
async with SessionLocal() as session:
|
|
try:
|
|
yield session
|
|
finally:
|
|
await session.close() |