new init
This commit is contained in:
32
apps/backend/app/core/database.py
Normal file
32
apps/backend/app/core/database.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
from contextlib import contextmanager
|
||||
|
||||
# Datenbank-URL (später besser aus Umgebungsvariable laden)
|
||||
DATABASE_URL = "postgresql://postgres:bacchus@localhost:5432/bacchus"
|
||||
|
||||
engine = create_engine(DATABASE_URL)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
# Dependency für FastAPI-Routen
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
# Optional: Kontextmanager für Skripte außerhalb von FastAPI
|
||||
@contextmanager
|
||||
def db_session():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
db.commit()
|
||||
except:
|
||||
db.rollback()
|
||||
raise
|
||||
finally:
|
||||
db.close()
|
Reference in New Issue
Block a user