24 lines
501 B
Python
24 lines
501 B
Python
|
|
from sqlalchemy import create_engine
|
||
|
|
from sqlalchemy.orm import DeclarativeBase, sessionmaker
|
||
|
|
|
||
|
|
from backend.config import settings
|
||
|
|
|
||
|
|
|
||
|
|
class Base(DeclarativeBase):
|
||
|
|
pass
|
||
|
|
|
||
|
|
|
||
|
|
engine = create_engine(
|
||
|
|
settings.database_url,
|
||
|
|
connect_args={"check_same_thread": False} if settings.database_url.startswith("sqlite") else {},
|
||
|
|
)
|
||
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||
|
|
|
||
|
|
|
||
|
|
def get_db():
|
||
|
|
db = SessionLocal()
|
||
|
|
try:
|
||
|
|
yield db
|
||
|
|
finally:
|
||
|
|
db.close()
|