DB Retrieval¶
Helper methods to retrieve/count data from DB
ServerSideField¶
Simple class used for server side data retrieval.
Attributes are:
col_name: the name as it will appear on a frontend interface showing the fieldfield_name: the SQLModel attribute name associated with this field (TODO: explain the use in the context of ecodev-front. Should be removed in the future, fetch it viafield)field: the SQLModel attribute associated with this fieldfilter: the filtering mechanism to use for this field (more on filtering in the filtering page)
To see more on its use, you can go inspect the test suite
get_rows¶
Quite often when you store data in DB, you want to retrieve them 😆.
get_rows helps you to do so
def get_rows(fields: List[ServerSideField],
model: Any,
limit: Union[int, None] = None,
offset: Union[int, None] = None,
filter_str: str = '',
search_str: str = '',
search_cols: Optional[List] = None,
fields_order: Optional[Callable] = None
) -> pd.DataFrame:
Its attributes are:
fields: a list ofServerSideFieldused to filter each column of the passedmodelmodel: the SQLModel on which to do the DB requestlimit: if filled, limit the number of elements fetchedoffset: if filled, put an offset before fetching elements. More in the official SQLModel documentationfilter_str: if filled, filter themodelbased on it. The syntax is taken from Dash datatable filtering, to ease plug with it. Go see the test suite to larn more.search_str: if filled, corresponds to a textual (regex like) seach onsearch_colssearch_cols: the DB columns ofmodelon which to search forsearch_strfields_order: The order on which to return the fetched data (sort on this column, then this one...)
The data are returned as a dataframe (simple to directly plug on Dash table components, being it data table or Dash AG Grid)
count_rows¶
In the same spirit as get_rows, allows to easily count element from SQLModel associated table.
Its attributes are:
fields: a list ofServerSideFieldused to filter each column of the passedmodelmodel: the SQLModel on which to do the DB requestlimit: if filled, limit the number of elements fetchedfilter_str: if filled, filter themodelbased on it. The syntax is taken from Dash datatable filtering, to ease plug with it. Go see the test suite to larn more.search_str: if filled, corresponds to a textual (regex like) seach onsearch_colssearch_cols: the DB columns ofmodelon which to search forsearch_str
Example (go read the filtering page as well):
# test that the count_rows method works as intended, assuming we inseted 3 users admin, client and monitoring)
APP_FILTER = ServerSideField(col_name='user', field_name='user', field=AppUser.user,
filter=ServerSideFilter.ILIKESTR)
self.assertTrue(count_rows([APP_FILTER], AppUser) == 3)
self.assertTrue(count_rows([APP_FILTER], AppUser, filter_str='{user} scontains o') == 1)