In-Memory Repository#

In-memory repository is a data store that is simply a Python list in temporary memory.

from redbird.repos import MemoryRepo
repo = MemoryRepo()

Usage#

Now you may use the repository the same way as any other repository. Please see:

Class#

class redbird.repos.MemoryRepo(*, model=<class 'dict'>, id_field=None, query_model=<class 'redbird.base.BasicQuery'>, errors_query='raise', field_access='infer', ordered=False, collection=[])#

Memory repository

This is a repository which operates in memory. Useful for unit tests and prototyping.

Parameters
  • collection (list) – The collection

  • 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

Examples

repo = MemoryRepo()
repo = MemoryRepo(collection=[
    {"car_type": "van", "color": "red"},
    {"car_type": "truck", "color": "red"}
])
insert(item)#

Insert item to the repository

Parameters

item (instance of model) – Item to insert to the repository

Examples

repo.insert(Item(id="a", color="red"))
filter_by(**kwargs)#

Filter the repository

Parameters

**kwargs (dict) – Query which is used to conduct furher operation.

Examples

repo.filter_by(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"))
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"))