mafw.tools.db_tools
Database Tools
This module provides utility functions for working with database models using the Peewee ORM. It offers helper functions for creating key-value mappings, retrieving primary key information, and combining fields for composite keys or display purposes.
Functions
|
Combine multiple database fields into a single concatenated string expression. |
|
Combine primary key fields of a database model into a single aliased field expression. |
|
Retrieve the primary key fields of a database model. |
|
Create a key-value mapping from a database model. |
- mafw.tools.db_tools.combine_fields(fields: list[Field], join_str: str = ' x ') Function[source]
Combine multiple database fields into a single concatenated string expression.
This function creates an SQL CONCAT expression that combines multiple field values into a single string using the specified separator. It’s particularly useful for creating composite keys or display strings from multiple fields.
- Parameters:
fields (list[peewee.Field]) – List of field objects to be combined.
join_str (str) – String to use as separator between fields. Defaults to ‘ x ‘.
- Returns:
A SQL CONCAT function expression combining the fields.
- Return type:
peewee.Function
- mafw.tools.db_tools.combine_pk(model: MAFwBaseModel | type[MAFwBaseModel], alias_name: str = 'combo_pk', join_str: str = ' x ') Alias[source]
Combine primary key fields of a database model into a single aliased field expression.
This function retrieves the primary key fields from the given model using
get_pk()and combines them into a single field expression. For models with a single primary key field, it simply aliases that field. For composite primary keys, it uses :func:combine_fields` to concatenate the fields with the specified separator.- Parameters:
model (MAFwBaseModel | type[MAFwBaseModel]) – The database model or model class to examine for primary key fields.
alias_name (str) – The alias name to apply to the resulting field expression. Defaults to ‘combo_pk’.
join_str (str) – String to use as separator between primary key fields when combining. Defaults to ‘ x ‘.
- Returns:
An aliased field expression representing the combined primary key.
- Return type:
peewee.Alias
- mafw.tools.db_tools.get_pk(model: MAFwBaseModel | type[MAFwBaseModel]) list[Field][source]
Retrieve the primary key fields of a database model.
This function examines the primary key of the provided model and returns a list of field objects that constitute the primary key. For composite primary keys, it returns all constituent fields; for simple primary keys, it returns a list containing just the primary key field.
- Parameters:
model (MAFwBaseModel | type[MAFwBaseModel]) – The database model or model class to examine.
- Returns:
A list of field objects representing the primary key fields.
- Return type:
list[peewee.Field]
- mafw.tools.db_tools.make_kv(model: MAFwBaseModel | type[MAFwBaseModel], key: Field, value: Field) dict[Any, Any][source]
Create a key-value mapping from a database model.
This function selects data from a given model using specified key and value fields, and returns a dictionary where keys are values from the key field and values are values from the value field.
- Parameters:
model (MAFwBaseModel | type[MAFwBaseModel]) – The database model or model class to query.
key (peewee.Field) – The field to use as dictionary keys.
value (peewee.Field) – The field to use as dictionary values.
- Returns:
A dictionary mapping key field values to value field values.
- Return type:
dict[Any, Any]
- Raises:
AttributeError – If the model parameter doesn’t have the required methods.