-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
577b485
commit c3f1aab
Showing
5 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
robyn | ||
sqlmodel |