Skip to content

abc

Data persistence interface.

AbstractRepository

AbstractRepository(**kwargs)

Bases: Generic[T]

Interface for persistent data interaction.

id_attribute class-attribute

id_attribute = 'id'

Name of the primary identifying attribute on model_type.

model_type class-attribute

model_type: type[T]

Type of object represented by the repository.

add abstractmethod async

add(data)

Add data to the collection.

Parameters:

Name Type Description Default
data T

Instance to be added to the collection.

required

Returns:

Type Description
T

The added instance.

check_not_found staticmethod

check_not_found(item_or_none)

Raise RepositoryNotFoundException if item_or_none is None.

Parameters:

Name Type Description Default
item_or_none T | None

Item to be tested for existence.

required

Returns:

Type Description
T

The item, if it exists.

delete abstractmethod async

delete(id_)

Delete instance identified by id_.

Parameters:

Name Type Description Default
id_ Any

Identifier of instance to be deleted.

required

Returns:

Type Description
T

The deleted instance.

Raises:

Type Description
RepositoryNotFoundException

If no instance found identified by id_.

filter_collection_by_kwargs abstractmethod

filter_collection_by_kwargs(**kwargs)

Filter the collection by kwargs.

Has AND semantics where multiple kwargs name/value pairs are provided.

Parameters:

Name Type Description Default
**kwargs Any

key/value pairs such that objects remaining in the collection after filtering have the property that their attribute named key has value equal to value.

{}

Raises:

Type Description
RepositoryException

if a named attribute doesn't exist on self.model_type.

get abstractmethod async

get(id_)

Get instance identified by id_.

Parameters:

Name Type Description Default
id_ Any

Identifier of the instance to be retrieved.

required

Returns:

Type Description
T

The retrieved instance.

Raises:

Type Description
RepositoryNotFoundException

If no instance found identified by id_.

get_id_attribute_value classmethod

get_id_attribute_value(item)

Get value of attribute named as self.id_attribute on item.

Parameters:

Name Type Description Default
item T

Anything that should have an attribute named as self.id_attribute value.

required

Returns:

Type Description
Any

The value of attribute on item named as self.id_attribute.

list abstractmethod async

list(*filters, **kwargs)

Get a list of instances, optionally filtered.

Parameters:

Name Type Description Default
*filters FilterTypes

Types for specific filtering operations.

()
**kwargs Any

Instance attribute value filters.

{}

Returns:

Type Description
list[T]

The list of instances, after filtering applied.

set_id_attribute_value classmethod

set_id_attribute_value(id_, item)

Return the item after the ID is set to the appropriate attribute.

Parameters:

Name Type Description Default
id_ Any

Value of ID to be set on instance

required
item T

Anything that should have an attribute named as self.id_attribute value.

required

Returns:

Type Description
Any

Item with id_ set to cls.id_attribute

update abstractmethod async

update(data)

Update instance with the attribute values present on data.

Parameters:

Name Type Description Default
data T

An instance that should have a value for self.id_attribute that exists in the collection.

required

Returns:

Type Description
T

The updated instance.

Raises:

Type Description
RepositoryNotFoundException

If no instance found with same identifier as data.

upsert abstractmethod async

upsert(data)

Update or create instance.

Updates instance with the attribute values present on data, or creates a new instance if one doesn't exist.

Parameters:

Name Type Description Default
data T

Instance to update existing, or be created. Identifier used to determine if an existing instance exists is the value of an attribute on data named as value of self.id_attribute.

required

Returns:

Type Description
T

The updated or created instance.

Raises:

Type Description
RepositoryNotFoundException

If no instance found with same identifier as data.