Files
eamco_auto_api/database.py

39 lines
881 B
Python

from sqlalchemy import create_engine
from sqlalchemy.engine import URL
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import declarative_base
from config import load_config
ApplicationConfig = load_config()
url = URL.create(
drivername="postgresql",
username=ApplicationConfig.POSTGRES_USERNAME,
password=ApplicationConfig.POSTGRES_PW,
host=ApplicationConfig.POSTGRES_SERVER,
database=ApplicationConfig.POSTGRES_DBNAME00,
port=ApplicationConfig.POSTGRES_PORT
)
engine = create_engine(url)
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Keep the global session for backwards compatibility
session = Session()
Base = declarative_base()
Base.metadata.create_all(engine)
# Add per-request session dependency for FastAPI
def get_db():
db = Session()
try:
yield db
finally:
db.close()