Home / File/ tutorial002_an_py39.py — fastapi Source File

tutorial002_an_py39.py — fastapi Source File

Architecture documentation for tutorial002_an_py39.py, a python file in the fastapi codebase. 3 imports, 0 dependents.

File python FastAPI Responses 3 imports 8 functions 5 classes

Entity Profile

Dependency Diagram

graph LR
  de4b5dc2_6a42_bcea_3d1b_550a5d21248e["tutorial002_an_py39.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  de4b5dc2_6a42_bcea_3d1b_550a5d21248e --> 0dda2280_3359_8460_301c_e98c77e78185
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  de4b5dc2_6a42_bcea_3d1b_550a5d21248e --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  bf11c16a_04e7_1fd0_b317_a1196fc34aad["sqlmodel"]
  de4b5dc2_6a42_bcea_3d1b_550a5d21248e --> bf11c16a_04e7_1fd0_b317_a1196fc34aad
  style de4b5dc2_6a42_bcea_3d1b_550a5d21248e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Annotated, Union

from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Session, SQLModel, create_engine, select


class HeroBase(SQLModel):
    name: str = Field(index=True)
    age: Union[int, None] = Field(default=None, index=True)


class Hero(HeroBase, table=True):
    id: Union[int, None] = Field(default=None, primary_key=True)
    secret_name: str


class HeroPublic(HeroBase):
    id: int


class HeroCreate(HeroBase):
    secret_name: str


class HeroUpdate(HeroBase):
    name: Union[str, None] = None
    age: Union[int, None] = None
    secret_name: Union[str, None] = None


sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, connect_args=connect_args)


def create_db_and_tables():
    SQLModel.metadata.create_all(engine)


def get_session():
    with Session(engine) as session:
        yield session


SessionDep = Annotated[Session, Depends(get_session)]
app = FastAPI()


@app.on_event("startup")
def on_startup():
    create_db_and_tables()


@app.post("/heroes/", response_model=HeroPublic)
def create_hero(hero: HeroCreate, session: SessionDep):
    db_hero = Hero.model_validate(hero)
    session.add(db_hero)
    session.commit()
    session.refresh(db_hero)
    return db_hero


@app.get("/heroes/", response_model=list[HeroPublic])
def read_heroes(
    session: SessionDep,
    offset: int = 0,
    limit: Annotated[int, Query(le=100)] = 100,
):
    heroes = session.exec(select(Hero).offset(offset).limit(limit)).all()
    return heroes


@app.get("/heroes/{hero_id}", response_model=HeroPublic)
def read_hero(hero_id: int, session: SessionDep):
    hero = session.get(Hero, hero_id)
    if not hero:
        raise HTTPException(status_code=404, detail="Hero not found")
    return hero


@app.patch("/heroes/{hero_id}", response_model=HeroPublic)
def update_hero(hero_id: int, hero: HeroUpdate, session: SessionDep):
    hero_db = session.get(Hero, hero_id)
    if not hero_db:
        raise HTTPException(status_code=404, detail="Hero not found")
    hero_data = hero.model_dump(exclude_unset=True)
    hero_db.sqlmodel_update(hero_data)
    session.add(hero_db)
    session.commit()
    session.refresh(hero_db)
    return hero_db


@app.delete("/heroes/{hero_id}")
def delete_hero(hero_id: int, session: SessionDep):
    hero = session.get(Hero, hero_id)
    if not hero:
        raise HTTPException(status_code=404, detail="Hero not found")
    session.delete(hero)
    session.commit()
    return {"ok": True}

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does tutorial002_an_py39.py do?
tutorial002_an_py39.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Responses subdomain.
What functions are defined in tutorial002_an_py39.py?
tutorial002_an_py39.py defines 8 function(s): create_db_and_tables, create_hero, delete_hero, get_session, on_startup, read_hero, read_heroes, update_hero.
What does tutorial002_an_py39.py depend on?
tutorial002_an_py39.py imports 3 module(s): __init__.py, sqlmodel, typing.
Where is tutorial002_an_py39.py in the architecture?
tutorial002_an_py39.py is located at docs_src/sql_databases/tutorial002_an_py39.py (domain: FastAPI, subdomain: Responses, directory: docs_src/sql_databases).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free