learn-sql-model/learn_sql_model/models/fast_model.py
2023-05-22 08:29:03 -05:00

57 lines
1.4 KiB
Python

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()
with config.database.session as session:
session.add(self)
session.commit()
def get(
self, id: int = None, config: "Config" = None, where=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__)
if where is not None:
statement = statement.where(where).options()
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