This commit is contained in:
Waylon Walker 2023-05-19 20:37:42 -05:00
parent c238b9d757
commit fcc0698aeb
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
12 changed files with 95 additions and 97 deletions

View file

@ -2,6 +2,8 @@ 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
@ -19,28 +21,36 @@ class FastModel(SQLModel):
def post(self, config: "Config" = None) -> None:
if config is None:
from learn_sql_model.config import get_config
config = get_config()
self.pre_post()
with config.session as session:
instance = self.__class__(**self.dict())
with config.database.session() as session:
session.add(self)
session.commit()
return instance
@classmethod
def get(
self, item_id: int = None, config: "Config" = None
) -> Optional["FastModel"]:
def get(self, id: int = None, config: "Config" = None) -> Optional["FastModel"]:
if config is None:
from learn_sql_model.config import get_config
config = get_config()
self.pre_get()
with config.session as session:
if item_id is None:
return session.exec(select(self)).all()
return session.exec(select(self).where(self.id == item_id)).one()
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