add
This commit is contained in:
21
app_core/db/database.py
Normal file
21
app_core/db/database.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
from app_core.config.settings import AppSettings
|
||||
|
||||
settings = AppSettings()
|
||||
|
||||
if not settings.database_url:
|
||||
raise RuntimeError(
|
||||
"Database configuration missing. Set DATABASE_URL or DB_HOST/DB_PORT/DB_NAME/DB_USER/DB_PASSWORD in a .env file at the project root."
|
||||
)
|
||||
|
||||
engine = create_engine(settings.database_url, pool_pre_ping=True, future=True, echo=settings.db_echo)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine, future=True)
|
||||
Base = declarative_base()
|
||||
|
||||
def get_db_session():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user