Files
marriage/services/auth/src/app/db/session.py

27 lines
585 B
Python

import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, DeclarativeBase
from typing import Generator
DEFAULT_DB_URL = "postgresql+psycopg2://postgres:postgres@postgres:5432/auth_db"
class Base(DeclarativeBase):
pass
DATABASE_URL = os.getenv("DATABASE_URL", DEFAULT_DB_URL)
engine = create_engine(
DATABASE_URL,
pool_pre_ping=True,
future=True,
)
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
def get_db() -> Generator:
db = SessionLocal()
try:
yield db
finally:
db.close()