From 5f328d82d3af62f3528d36d8ee6c426edae7a218 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Sun, 19 Feb 2023 17:49:44 +0400 Subject: [PATCH] MySQL database --- .env | 10 +++++----- .env_db | 5 +++++ .env_debug | 10 +++++----- app/sql/crud.py | 38 ++++++++++++++++++++++++++++++++++++++ app/sql/db.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ app/sql/models.py | 12 ++++++++++++ app/sql/schemas.py | 11 +++++++++++ 7 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 .env_db create mode 100644 app/sql/crud.py create mode 100644 app/sql/db.py create mode 100644 app/sql/models.py create mode 100644 app/sql/schemas.py diff --git a/.env b/.env index 40ffad0..dfb73ea 100644 --- a/.env +++ b/.env @@ -1,5 +1,5 @@ -MYSQL_HOST=${REPO_NAME_SNAKE}_db -MYSQL_PORT=3306 -MYSQL_USER=${REPO_NAME_SNAKE} -MYSQL_PASSWORD= -MYSQL_DATABASE=${REPO_NAME_SNAKE} +DB_HOST=${REPO_NAME_SNAKE}_db +DB_PORT=3306 +DB_USER=${REPO_NAME_SNAKE} +DB_PASSWORD= +DB_DATABASE=${REPO_NAME_SNAKE} diff --git a/.env_db b/.env_db new file mode 100644 index 0000000..40ffad0 --- /dev/null +++ b/.env_db @@ -0,0 +1,5 @@ +MYSQL_HOST=${REPO_NAME_SNAKE}_db +MYSQL_PORT=3306 +MYSQL_USER=${REPO_NAME_SNAKE} +MYSQL_PASSWORD= +MYSQL_DATABASE=${REPO_NAME_SNAKE} diff --git a/.env_debug b/.env_debug index 554ae8e..1342d7c 100644 --- a/.env_debug +++ b/.env_debug @@ -1,5 +1,5 @@ -MYSQL_HOST=localhost -MYSQL_PORT=3306 -MYSQL_USER=darkcat09 -MYSQL_PASSWORD= -MYSQL_DATABASE=apptest +DB_HOST=localhost +DB_PORT=3306 +DB_USER=darkcat09 +DB_PASSWORD= +DB_DATABASE=apptest diff --git a/app/sql/crud.py b/app/sql/crud.py new file mode 100644 index 0000000..207be6d --- /dev/null +++ b/app/sql/crud.py @@ -0,0 +1,38 @@ +from typing import Optional, List +from sqlalchemy.orm import Session + +from . import models +from . import schemas + + +def get_user( + db: Session, + user_id: int) -> Optional[models.User]: + + return db \ + .query(models.User) \ + .filter(models.User.id == user_id) \ + .first() + + +def get_users( + db: Session, + skip: int = 0, + limit: int = 100) -> List[Optional[models.User]]: + + return db \ + .query(models.User) \ + .offset(skip) \ + .limit(limit) \ + .all() + + +def create_user( + db: Session, + user: schemas.User) -> models.User: + + user_model = models.User(**user.dict()) + db.add(user_model) + db.commit() + db.refresh(user_model) + return user_model diff --git a/app/sql/db.py b/app/sql/db.py new file mode 100644 index 0000000..17d970d --- /dev/null +++ b/app/sql/db.py @@ -0,0 +1,46 @@ +from typing import Generator +from pydantic import BaseSettings + +from sqlalchemy import create_engine +from sqlalchemy.orm import Session, sessionmaker +from sqlalchemy.ext.declarative import declarative_base + + +class SqlSettings(BaseSettings): + db_host: str = '${REPO_NAME_SNAKE}_db' + db_port: int = 3306 + db_user: str = '${REPO_NAME_SNAKE}' + db_password: str = '' + db_database: str = '${REPO_NAME_SNAKE}' + +sql_settings = SqlSettings() + + +db_url = ( + 'mysql://{db_user}:{db_password}@' + '{db_host}:{db_port}/${db_database}' +).format(**sql_settings.dict()) + +engine = create_engine(db_url) +SessionLocal = sessionmaker( + autocommit=False, + autoflush=False, + bind=engine, +) +Base = declarative_base() + + +def get_db() -> Generator[Session, None, None]: + """FastAPI dependency + returning database Session object. + Code is copied from the official docs + + Yields: + SQLAlchemy Session object + """ + + db = SessionLocal() + try: + yield db + finally: + db.close() diff --git a/app/sql/models.py b/app/sql/models.py new file mode 100644 index 0000000..abdbe7f --- /dev/null +++ b/app/sql/models.py @@ -0,0 +1,12 @@ +from sqlalchemy import Column, String, Integer + +from .db import Base + + +class User(Base): + __tablename__ = 'users' + + id = Column(Integer, primary_key=True) + email = Column(String) + name = Column(String) + age = Column(Integer) diff --git a/app/sql/schemas.py b/app/sql/schemas.py new file mode 100644 index 0000000..294775e --- /dev/null +++ b/app/sql/schemas.py @@ -0,0 +1,11 @@ +from pydantic import BaseModel + + +class User(BaseModel): + id: int + email: str + name: str + age: int + + class Config: + orm_mode = True