36 lines
694 B
Python
36 lines
694 B
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import DeclarativeBase, sessionmaker
|
|
|
|
from app.config import settings
|
|
|
|
|
|
def build_database_url() -> str:
|
|
return (
|
|
f"postgresql+psycopg://{settings.postgres_user}:"
|
|
f"{settings.postgres_password}@{settings.postgres_host}:"
|
|
f"{settings.postgres_port}/{settings.postgres_db}"
|
|
)
|
|
|
|
|
|
DATABASE_URL = build_database_url()
|
|
|
|
engine = create_engine(DATABASE_URL, pool_pre_ping=True)
|
|
|
|
SessionLocal = sessionmaker(
|
|
autocommit=False,
|
|
autoflush=False,
|
|
bind=engine,
|
|
)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|