feat(01-03): add full v1 ORM schema, async session factory, and DB dependency

- backend/db/models.py: 11 SQLAlchemy 2.0 ORM models (User, Quota, RefreshToken,
  Folder, Document, Topic, DocumentTopic, Share, AuditLog, CloudConnection, Group)
- Document.user_id declared nullable=True per D-03 (Phase 2 adds NOT NULL)
- AuditLog.metadata_ uses mapped_column("metadata", JSONB) to avoid DeclarativeBase
  reserved-attribute conflict
- Group table stub for D-02 (v2 feature, seeded per PROJECT.md)
- Uses Optional[X] instead of X | None for Python < 3.10 compatibility
- backend/db/session.py: async engine (pool_pre_ping=True, expire_on_commit=False)
- backend/deps/db.py: async get_db() FastAPI dependency yielding AsyncSession
This commit is contained in:
curo1305
2026-05-22 09:16:21 +02:00
parent 213afec6b3
commit 3e1fcd69b5
5 changed files with 359 additions and 0 deletions
View File
+26
View File
@@ -0,0 +1,26 @@
"""
FastAPI dependency that yields an async SQLAlchemy session per request.
Usage in route handlers:
from deps.db import get_db
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import Depends
@router.get("/items")
async def list_items(session: AsyncSession = Depends(get_db)):
...
"""
from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncSession
from db.session import AsyncSessionLocal
async def get_db() -> AsyncGenerator[AsyncSession, None]:
"""Yield a per-request AsyncSession; close it when the request is done."""
async with AsyncSessionLocal() as session:
try:
yield session
finally:
await session.close()