-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (full-stack): SQLModel setup and example
- Loading branch information
Showing
2 changed files
with
48 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
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 | ||
~~~ |
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,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() |