Abstract Classes
Contents
Abstract Classes#
Template#
- class redbird.templates.TemplateRepo(*, model=<class 'dict'>, id_field=None, query_model=<class 'redbird.base.BasicQuery'>, errors_query='raise', field_access='infer', ordered=False)#
Template repository for easy subclassing
- Parameters
model (Type) – Class of an item in the repository. Commonly dict or subclass of Pydantic BaseModel. By default dict
id_field (str, optional) – Attribute or key that identifies each item in the repository.
field_access ({'attr', 'key'}, optional) – How to access a field in an item. Either by attribute (‘attr’) or key (‘item’). By default guessed from the model.
query (Type, optional) – Query model of the repository.
errors_query ({'raise', 'warn', 'discard'}) – Whether to raise an exception, warn or discard the item inn case of validation error in converting data to the item model from the repository. By default raise
Examples
class MyRepo(TemplateRepo): def insert(self, item): # Insert item to the data store ... def query_read(self, query: dict): # Get data from repository for item in ...: yield item def query_update(self, query: dict, values: dict): # Update items with values matcing the query ... def query_delete(self, query: dict): # Delete items matcing the query ... def item_to_data(self, item): # Convert item to type that is understandable # by the repository's data store ... return data
- cls_result#
alias of
redbird.templates.TemplateResult
- query_data(query)#
Query (read) the data store and return raw data
Override this or
query_items()
method.- Parameters
query (dict) – Repository query, by default dict.
- Returns
Iterable of raw data that is converted to an item using
data_to_item()
- Return type
iterable (any)
Examples
Used in following cases:
repo.filter_by(color="red").all()
- query_items(query)#
Query (read) the data store and return items
Override this or
query_data()
method.- Parameters
query (dict) – Repository query, by default dict.
- Returns
Items that are instances of the class in the
model
attibute. Typically dicts or instances of subclassed Pydantic BaseModel- Return type
iterable (
model
)
Examples
Used in following cases:
repo.filter_by(color="red").all()
- abstract query_update(query, values)#
Update the result of the query with given values
Override this method.
- Parameters
query (any) – Repository query, by default dict.
Examples
Used in following cases:
repo.filter_by(color="red").update(color="blue")
- abstract query_delete(query)#
Delete the result of the query
Override this method.
- Parameters
query (any) – Repository query, by default dict.
Examples
Used in following cases:
repo.filter_by(color="red").delete()
- query_read_first(query)#
Query the first item
You may override this method. By default, the first item returned by
TemplateRepo.query_read
is returned.- Parameters
query (any) – Repository query, by default dict.
Examples
Used in the following case:
repo.filter_by(color="red").first()
- query_read_limit(query, n)#
Query the first n items
You may override this method. By default, the N first items returned by
TemplateRepo.query_read
are returned.- Parameters
query (any) – Repository query, by default dict.
n (int) – Number of items to return
Examples
Used in the following case:
repo.filter_by(color="red").limit(3)
- query_read_last(query)#
Query the last item
You may override this method. By default, the last item returned by
TemplateRepo.query_read
is returned.- Parameters
query (any) – Repository query, by default dict.
Examples
Used in the following case:
repo.filter_by(color="red").last()
- query_replace(query, values)#
Replace the items with given values using given query
You may override this method. By default, the result of the query is deleted and an item from the values is generated.
- Parameters
query (any) – Repository query, by default dict.
values (dict) – Values to replace the items’ existing values with
Examples
Used in the following case:
repo.filter_by(color="red").replace(color="blue")
- query_count(query)#
Count the items returned by the query
You may override this method. By default, the items returned by
TemplateRepo.query_read
are counted.- Parameters
query (any) – Repository query, by default dict.
Examples
Used in the following case:
repo.filter_by(color="red").count()
- format_query(query)#
Format the query to a format suitable by the repository
You may override this method. By default, the query is as dictionary.
- Parameters
query (dict) – Query to reformat
Examples
Used in the following case:
repo.filter_by(color="red")
- class redbird.templates.TemplateResult(query=None, repo=None)#
- query()#
Get actual result
- query_data()#
Get actual result
- update(**kwargs)#
Update the resulted items
- delete()#
Delete the resulted items
- replace(_TemplateResult__values=None, **kwargs)#
Replace the existing item(s) with given
- count()#
Count the resulted items
- first()#
Return first item
- limit(n)#
Return n items
- last()#
Return last item
- format_query(query)#
Turn the query to a form that’s understandable by the underlying database
Base Classes#
- class redbird.base.BaseRepo(*, model=<class 'dict'>, id_field=None, query_model=<class 'redbird.base.BasicQuery'>, errors_query='raise', field_access='infer', ordered=False)#
Abstract Repository
Base class for the repository pattern.
- Parameters
model (Type) – Class of an item in the repository. Commonly dict or subclass of Pydantic BaseModel. By default dict
id_field (str, optional) – Attribute or key that identifies each item in the repository.
field_access ({'attr', 'key'}, optional) – How to access a field in an item. Either by attribute (‘attr’) or key (‘item’). By default guessed from the model.
query (Type, optional) – Query model of the repository.
errors_query ({'raise', 'warn', 'discard'}) – Whether to raise an exception, warn or discard the item in case of validation error in converting data to the item model from the repository. By default raise
- add(item, if_exists='raise')#
Add an item to the repository
- abstract insert()#
Insert item to the repository
- Parameters
item (instance of model) – Item to insert to the repository
Examples
repo.insert(Item(id="a", color="red"))
- upsert(item)#
Upsert item to the repository
Upsert is an insert if the item does not exist in the repository and update if it does.
- Parameters
item (instance of model) – Item to upsert to the repository
Examples
repo.upsert(Item(id="a", color="red"))
- delete(item)#
Delete item from the repository
- Parameters
item (instance of model) – Item to delete from the repository
Examples
repo.delete(Item(id="a", color="red"))
- update(item)#
Update item in the repository
- Parameters
item (instance of model) – Item to update in the repository
Examples
repo.update(Item(id="a", color="red"))
- replace(item)#
Update an item in the repository
- get_by(id)#
Get item based on ID but returns result for further operations
- filter_by(**kwargs)#
Filter the repository
- Parameters
**kwargs (dict) – Query which is used to conduct furher operation.
Examples
repo.filter_by(color="red")
- data_to_item(data)#
Turn object from repo (row, doc, dict, etc.) to item
- to_item(obj)#
Turn an object to item
- get_field_value(item, key)#
Utility method to get key’s value from an item
If item’s fields are accessed via attribute, getattr is used. If fields are accessed via items, getitem is used.
- set_field_value(item, key, value)#
Utility method to set field’s value in an item
If item’s fields are accessed via attribute, setattr is used. If fields are accessed via items, setitem is used.
- class redbird.base.BaseResult(query=None, repo=None)#
Abstract filter result
Result classes add additional alchemy to Red Bird providing convenient ways to read, modify or delete data.
Subclass of BaseRepo should also have custom subclass of BaseResult as cls_result attribute.
- first()#
Return first item
- last()#
Return last item
- all()#
Return all items
- limit(n)#
Return n items
- query()#
Get actual result
- abstract query_data()#
Get actual result
- abstract update(**kwargs)#
Update the resulted items
- abstract delete()#
Delete the resulted items
- replace(_BaseResult__values=None, **kwargs)#
Replace the existing item(s) with given
- count()#
Count the resulted items
- format_query(query)#
Turn the query to a form that’s understandable by the underlying database