Skip to content

Commit

Permalink
feat: add sqlmodel scaffold (#749)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
leogregianin and pre-commit-ci[bot] authored Feb 11, 2024
1 parent 577b485 commit c3f1aab
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions robyn/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions robyn/scaffold/sqlmodel/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
47 changes: 47 additions & 0 deletions robyn/scaffold/sqlmodel/app.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions robyn/scaffold/sqlmodel/models.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions robyn/scaffold/sqlmodel/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
robyn
sqlmodel

0 comments on commit c3f1aab

Please sign in to comment.