Created
May 10, 2022 15:47
-
-
Save Joaopcamposs/da53a932bb5daaf1d8e73e702acb84b4 to your computer and use it in GitHub Desktop.
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
# database.py | |
from sqlalchemy import create_engine | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.orm import sessionmaker | |
DATABASE_URL = "mysql+pymysql://user:@host:port/db_name" | |
db_engine = create_engine(DATABASE_URL) | |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=db_engine) | |
Base = declarative_base() | |
def get_db(): | |
""" | |
Function to generate db session | |
:return: Session | |
""" | |
db = None | |
try: | |
db = SessionLocal() | |
yield db | |
finally: | |
db.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment