from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from . import queries, models

SessionLocal = sessionmaker(autocommit=False, autoflush=False)

def init_db(url: str, create_tables: bool = True, **engine_kwargs):
    engine = create_engine(url, **engine_kwargs)
    if create_tables:
        models.Base.metadata.create_all(bind=engine)
    SessionLocal.configure(bind=engine)


def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
