From c3f1aabc98b612b225c00b9f3f14bf7bb3f3bf33 Mon Sep 17 00:00:00 2001 From: Leonardo Gregianin Date: Sun, 11 Feb 2024 11:22:49 -0400 Subject: [PATCH] feat: add sqlmodel scaffold (#749) * Added sqlmodel scaffold * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added sqlmodel scaffold * Added sqlmodel in cli --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- robyn/cli.py | 1 + robyn/scaffold/sqlmodel/Dockerfile | 11 ++++++ robyn/scaffold/sqlmodel/app.py | 47 ++++++++++++++++++++++++ robyn/scaffold/sqlmodel/models.py | 10 +++++ robyn/scaffold/sqlmodel/requirements.txt | 2 + 5 files changed, 71 insertions(+) create mode 100644 robyn/scaffold/sqlmodel/Dockerfile create mode 100644 robyn/scaffold/sqlmodel/app.py create mode 100644 robyn/scaffold/sqlmodel/models.py create mode 100644 robyn/scaffold/sqlmodel/requirements.txt diff --git a/robyn/cli.py b/robyn/cli.py index dd3aadc36..385348890 100644 --- a/robyn/cli.py +++ b/robyn/cli.py @@ -43,6 +43,7 @@ def create_robyn_app(): Choice("mongo", name="MongoDB"), Choice("sqlalchemy", name="SqlAlchemy"), Choice("prisma", name="Prisma"), + Choice("sqlmodel", name="SQLModel"), ], "default": Choice("no-db", name="No DB"), "name": "project_type", diff --git a/robyn/scaffold/sqlmodel/Dockerfile b/robyn/scaffold/sqlmodel/Dockerfile new file mode 100644 index 000000000..7ae653b73 --- /dev/null +++ b/robyn/scaffold/sqlmodel/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.11-bookworm + +WORKDIR /workspace + +COPY . . + +RUN pip install --no-cache-dir --upgrade -r requirements.txt + +EXPOSE 8080 + +CMD ["python3", "app.py", "--log-level=DEBUG"] \ No newline at end of file diff --git a/robyn/scaffold/sqlmodel/app.py b/robyn/scaffold/sqlmodel/app.py new file mode 100644 index 000000000..a974ecd35 --- /dev/null +++ b/robyn/scaffold/sqlmodel/app.py @@ -0,0 +1,47 @@ +from robyn import Robyn + +from sqlmodel import SQLModel, Session, create_engine, select +from models import Hero + + +app = Robyn(__file__) + +engine = create_engine("sqlite:///database.db", echo=True) + + +@app.get("/") +def index(): + return "Hello World" + + +@app.get("/create") +def create(): + SQLModel.metadata.create_all(bind=engine) + return "created tables" + + +@app.get("/insert") +def insert(): + hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") + hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") + hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) + + with Session(engine) as session: + session.add(hero_1) + session.add(hero_2) + session.add(hero_3) + session.commit() + return "inserted" + + +@app.get("/select") +def get_data(): + with Session(engine) as session: + statement = select(Hero).where(Hero.name == "Spider-Boy") + hero = session.exec(statement).first() + return hero + + +if __name__ == "__main__": + # create a configured "Session" class + app.start(host="0.0.0.0", port=8080) diff --git a/robyn/scaffold/sqlmodel/models.py b/robyn/scaffold/sqlmodel/models.py new file mode 100644 index 000000000..12132bb7b --- /dev/null +++ b/robyn/scaffold/sqlmodel/models.py @@ -0,0 +1,10 @@ +from typing import Optional + +from sqlmodel import Field, SQLModel + + +class Hero(SQLModel, table=True): + id: Optional[int] = Field(default=None, primary_key=True) + name: str + secret_name: str + age: Optional[int] = None diff --git a/robyn/scaffold/sqlmodel/requirements.txt b/robyn/scaffold/sqlmodel/requirements.txt new file mode 100644 index 000000000..86740d021 --- /dev/null +++ b/robyn/scaffold/sqlmodel/requirements.txt @@ -0,0 +1,2 @@ +robyn +sqlmodel \ No newline at end of file