Skip to content

Instantly share code, notes, and snippets.

@Joaopcamposs
Created May 10, 2022 15:47
Show Gist options
  • Save Joaopcamposs/da53a932bb5daaf1d8e73e702acb84b4 to your computer and use it in GitHub Desktop.
Save Joaopcamposs/da53a932bb5daaf1d8e73e702acb84b4 to your computer and use it in GitHub Desktop.
# 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