from typing import Optional, TYPE_CHECKING from sqlmodel import SQLModel, select from learn_sql_model.config import get_config if TYPE_CHECKING: from learn_sql_model.config import Config class FastModel(SQLModel): def pre_post(self) -> None: """run before post""" def pre_delete(self) -> None: """run before delete""" @classmethod def pre_get(self) -> None: """run before get""" def post(self, config: "Config" = None) -> None: if config is None: config = get_config() self.pre_post() instance = self.__class__(**self.dict()) with config.database.session() as session: session.add(self) session.commit() return instance def get(self, id: int = None, config: "Config" = None) -> Optional["FastModel"]: if config is None: config = get_config() self.pre_get() with config.database.session() as session: if id is None: print("get all") statement = select(self.__class__) results = session.exec(statement).all() else: print("get by id") statement = select(self.__class__).where(self.__class__.id == id) results = session.exec(statement).one() return results # TODO # update # delete