Repository navigation
Expand file tree
/
Copy pathasync_db_app.py
More file actions
63 lines (43 loc) · 1.79 KB
/
Copy pathasync_db_app.py
File metadata and controls
63 lines (43 loc) · 1.79 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import Annotated
from fastapi import Depends, FastAPI
from pydantic import BaseModel, ConfigDict, Field
from sqlalchemy import String, select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
engine = create_async_engine("sqlite+aiosqlite:///./fastapi-async.db")
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
class Base(DeclarativeBase):
pass
class NoteDB(Base):
__tablename__ = "notes"
id: Mapped[int] = mapped_column(primary_key=True)
text: Mapped[str] = mapped_column(String(200))
class NoteCreate(BaseModel):
text: str = Field(min_length=1, max_length=200)
class NoteOut(NoteCreate):
model_config = ConfigDict(from_attributes=True)
id: int
@asynccontextmanager
async def lifespan(app: FastAPI):
try:
async with engine.begin() as connection:
await connection.run_sync(Base.metadata.create_all)
yield
finally:
await engine.dispose()
app = FastAPI(lifespan=lifespan)
async def get_session() -> AsyncIterator[AsyncSession]:
async with SessionLocal() as session:
yield session
@app.post("/notes", response_model=NoteOut, status_code=201)
async def create_note(note: NoteCreate, session: Annotated[AsyncSession, Depends(get_session)]):
async with session.begin():
row = NoteDB(**note.model_dump())
session.add(row)
await session.flush()
return NoteOut.model_validate(row)
@app.get("/notes", response_model=list[NoteOut])
async def list_notes(session: Annotated[AsyncSession, Depends(get_session)]):
return (await session.scalars(select(NoteDB).order_by(NoteDB.id))).all()