from typing import Optional, Dict, List, Any, Union
import datetime as dt
from sqlmodel import Field, Session, SQLModel, create_engine, select
import threading as th
import queue

# ~~~ Database ~~~~~~~~~~~~~~~

class Database:
    def __init__(self, uri: str):
        self.engine = create_engine(uri)
        SQLModel.metadata.create_all(self.engine)

    def create_all(self, items: List[SQLModel]):
        with Session(self.engine) as session:
            for item in items:
                session.add(item)
            session.commit()

    def get_by_id(self, id: Union[str, int], model: SQLModel):
        with Session(self.engine) as session:
            stmt = select(model).where(model.id == id)
            return session.exec(stmt).first()
    
    def get_by_field(self, key: str, value: Any, model: SQLModel):
        stmt = select(model).where(getattr(model, key) == value)
        print(stmt)
        return self.exec(stmt)
    
    def exec(self, stmt: str, params = {}):
        with 