Skip to content

Commit

Permalink
feat (full-stack): SQLModel setup and example
Browse files Browse the repository at this point in the history
  • Loading branch information
santanche committed Aug 11, 2024
1 parent 0e19012 commit 5eab83b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions full-stack/sqlmodel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SQLModel
https://sqlmodel.tiangolo.com/

~~~
pip install sqlmodel
~~~

## PostgreSQL adapter

Installing dependencies:
~~~
sudo apt-get install libpq-dev python3-dev
~~~

### psycopg2
https://pypi.org/project/psycopg2/

~~~
pip install psycopg2
~~~
28 changes: 28 additions & 0 deletions full-stack/sqlmodel/users_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from datetime import date
from typing import Optional

from sqlmodel import Field, Session, SQLModel, create_engine

def parse_date(birthday_str: Optional[str]) -> Optional[date]:
if birthday_str:
return date.fromisoformat(birthday_str)
return None

class Users(SQLModel, table=True):
email_id: str = Field(default=None, primary_key=True)
name: str
birthday: Optional[date] = None

user_1 = Users(email_id="asdrubal@email.com", name="Asdrubal", birthday=parse_date("2024-01-20"))
user_2 = Users(email_id="doriana@email.com", name="Doriana", birthday=parse_date("2024-03-05"))
user_3 = Users(email_id="bonerges@email.com", name="Bonerges", birthday=parse_date("2024-05-01"))

engine = create_engine("postgresql://postgres:postgres@localhost:5431/test")

SQLModel.metadata.create_all(engine)

with Session(engine) as session:
session.add(user_1)
session.add(user_2)
session.add(user_3)
session.commit()

0 comments on commit 5eab83b

Please sign in to comment.