-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
27 lines (23 loc) · 869 Bytes
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
SQL_ALCHEMY_DATABASE_URL = "sqlite:///./blog.db"
engine = create_engine(
SQL_ALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
) # To esstablish connection w database
Base = (
declarative_base()
) # This is for defining/giving a schema for a class which can be used as a table
SessionLocal = sessionmaker(
bind=engine, autoflush=False, autocommit=False
) # Create a session
def get_db():
"""function is used to create a new SQLAlchemy database session for each request."""
db = SessionLocal()
try:
yield db
except Exception as e:
print(f"An error occurred: {e}")
raise e # Re-raise the exception so FastAPI can handle it properly
finally:
db.close()