sql_app: basic models and schemas from FastAPI docs

- Classes in models describe tables in database

	- Classes in schemas desribe pydantic models to
	read from the API.

	- database provides a connection and sessions to
	the database.

	- crud exposes the interface to the database, i.e.
	actions we can take from the application onto the
	database

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-03-03 15:49:26 +03:00
parent 0dfbda8d32
commit b0dc22070e
Signed by: Hesham
GPG Key ID: 74876157D199B09E
6 changed files with 114 additions and 0 deletions

0
sql_app/__init__.py Normal file
View File

38
sql_app/crud.py Normal file
View File

@ -0,0 +1,38 @@
# CRUD (Create, Read, Update, Delete) from db
from sqlalchemy.orm import Session
from . import models, schemas
def get_user(db: Session, user_id: int):
return db.query(models.User).filter(models.User.id == user_id).first()
def get_user_by_email(db: Session, email: str):
return db.query(models.User).filter(models.User.email == email).first()
def get_users(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.User).offset(skip).limit(limit).all()
def create_user(db: Session, user: schemas.UserCreate):
fake_hashed_password = user.password + "notreallyhashed"
db_user = models.User(email=user.email, hashed_password=fake_hashed_password)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
def get_iot_entities(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.IotEntity).offset(skip).limit(limit).all()
def create_iot_entity(db: Session, item: schemas.IotEntityCreate, user_id: int):
db_item = models.Item(**item.dict(), owner_id=user_id)
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item

14
sql_app/database.py Normal file
View File

@ -0,0 +1,14 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
# For sqlite
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

0
sql_app/main.py Normal file
View File

25
sql_app/models.py Normal file
View File

@ -0,0 +1,25 @@
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from .database import Base
class User(Base):
__tablename__ = "user_accounts"
id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, index=True)
hashed_password = Column(String)
is_active = Column(Boolean, default=True)
items = relationship("Item", back_populates="owner")
class IotEntity(Base):
__tablename__ = "iot_entities"
id = Column(Integer, primary_key=True, index=True)
description = Column(String, index=True)
owner_id = Column(Integer, ForeignKey("users.id"))
owner = relationship("User", back_populates="items")

37
sql_app/schemas.py Normal file
View File

@ -0,0 +1,37 @@
from typing import Optional
from pydantic import BaseModel
class IotEntityBase(BaseModel):
id: int
description: str
class IotEntityCreate(IotEntityBase):
pass
class IotEntity(IotEntityBase):
id: int
description: str
class Config:
orm_mode = True
class UserBase(BaseModel):
email: str
class UserCreate(UserBase):
password: str
class User(UserBase):
id: int
is_active: bool
authorized_devices: list[IotEntity] = []
class Config:
orm_mode = True