Skip to content

Commit

Permalink
setup the db connection and user model
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGsa committed Dec 6, 2023
1 parent 7a91ae6 commit 9f52239
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_USERNAME=root
DB_PASSWORD=password
DB_HOST=localhost
DB_NAME=dzmouhami
DB_PORT=3306
Binary file modified __pycache__/main.cpython-310.pyc
Binary file not shown.
Binary file added config/__pycache__/const_db.cpython-310.pyc
Binary file not shown.
Binary file added config/__pycache__/db.cpython-310.pyc
Binary file not shown.
10 changes: 10 additions & 0 deletions config/const_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from dotenv import load_dotenv
import os

load_dotenv()
DB_USERNAME = os.getenv("DB_USERNAME")
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_HOST = os.getenv("DB_HOST")
DB_NAME = os.getenv("DB_NAME")
DB_PORT = os.getenv("DB_PORT")
URL_DB = f"mysql+pymysql://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
8 changes: 8 additions & 0 deletions config/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from config.const_db import URL_DB

engine = create_engine(URL_DB)
Base = declarative_base()
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from fastapi import FastAPI
from models import models
from config.db import engine

app = FastAPI()

models.Base.metadata.create_all(engine)

@app.get("/")
def read_root():
Expand Down
Binary file added models/__pycache__/models.cpython-310.pyc
Binary file not shown.
13 changes: 13 additions & 0 deletions models/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from config.db import Base
from sqlalchemy import String, Column, Integer, Boolean, DateTime

class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True, index=True)
nom = Column(String(255))
prenom = Column(String(255))
email = Column(String(255), unique=True)
password = Column(String(255))
isGoogleUser = Column(Boolean, default=False)
createdAt = Column(DateTime)
role = Column(String(255), default="user")
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
fastapi
fastapi
PyMySql
python-dotenv

0 comments on commit 9f52239

Please sign in to comment.