abc¶
- class litestar.repository.abc.AbstractAsyncRepository[source]¶
Bases:
Generic[T]Interface for persistent data interaction.
- abstractmethod async add(data: T) T[source]¶
Add
datato the collection.- Parameters:
data¶ – Instance to be added to the collection.
- Returns:
The added instance.
- abstractmethod async add_many(data: list[T]) list[T][source]¶
Add multiple
datato the collection.- Parameters:
data¶ – Instances to be added to the collection.
- Returns:
The added instances.
- static check_not_found(item_or_none: T | None) T[source]¶
Raise
NotFoundErrorifitem_or_noneisNone.- Parameters:
item_or_none¶ – Item (
T) to be tested for existence.- Returns:
The item, if it exists.
- abstractmethod async count(*filters: FilterTypes, **kwargs: Any) int[source]¶
Get the count of records returned by a query.
- abstractmethod async delete(item_id: Any) T[source]¶
Delete instance identified by
item_id.- Parameters:
item_id¶ – Identifier of instance to be deleted.
- Returns:
The deleted instance.
- Raises:
NotFoundError – If no instance found identified by
item_id.
- abstractmethod async delete_many(item_ids: list[Any]) list[T][source]¶
Delete multiple instances identified by list of IDs
item_ids.- Parameters:
item_ids¶ – list of Identifiers to be deleted.
- Returns:
The deleted instances.
- abstractmethod async exists(*filters: FilterTypes, **kwargs: Any) bool[source]¶
Return true if the object specified by
kwargsexists.
- abstractmethod filter_collection_by_kwargs(collection: CollectionT, /, **kwargs: Any) CollectionT[source]¶
Filter the collection by kwargs.
Has
ANDsemantics where multiple kwargs name/value pairs are provided.- Parameters:
- Returns:
The filtered objects
- Raises:
RepositoryError – if a named attribute doesn’t exist on
model_type.
- abstractmethod async get(item_id: Any, **kwargs: Any) T[source]¶
Get instance identified by
item_id.
- classmethod get_id_attribute_value(item: T | type[T], id_attribute: str | None = None) Any[source]¶
Get value of attribute named as
id_attributeonitem.- Parameters:
item¶ – Anything that should have an attribute named as
id_attributevalue.id_attribute¶ – Allows customization of the unique identifier to use for model fetching. Defaults to None, but can reference any surrogate or candidate key for the table.
- Returns:
The value of attribute on
itemnamed asid_attribute.
- abstractmethod async get_one(**kwargs: Any) T[source]¶
Get an instance specified by the
kwargsfilters if it exists.- Parameters:
**kwargs¶ – Instance attribute value filters.
- Returns:
The retrieved instance.
- Raises:
NotFoundError – If no instance found identified by
kwargs.
- abstractmethod async get_one_or_none(**kwargs: Any) T | None[source]¶
Get an instance if it exists or None.
- Parameters:
**kwargs¶ – Instance attribute value filters.
- Returns:
The retrieved instance or None.
- abstractmethod async get_or_create(**kwargs: Any) tuple[T, bool][source]¶
Get an instance specified by the
kwargsfilters if it exists or create it.- Parameters:
**kwargs¶ – Instance attribute value filters.
- Returns:
A tuple that includes the retrieved or created instance, and a boolean on whether the record was created or not
- id_attribute: Any = 'id'¶
Name of the primary identifying attribute on
model_type.
- abstractmethod async list(*filters: FilterTypes, **kwargs: Any) list[T][source]¶
Get a list of instances, optionally filtered.
- abstractmethod async list_and_count(*filters: FilterTypes, **kwargs: Any) tuple[list[T], int][source]¶
List records with total count.
- classmethod set_id_attribute_value(item_id: Any, item: T, id_attribute: str | None = None) T[source]¶
Return the
itemafter the ID is set to the appropriate attribute.- Parameters:
item_id¶ – Value of ID to be set on instance
item¶ – Anything that should have an attribute named as
id_attributevalue.id_attribute¶ – Allows customization of the unique identifier to use for model fetching. Defaults to None, but can reference any surrogate or candidate key for the table.
- Returns:
Item with
item_idset toid_attribute
- abstractmethod async update(data: T) T[source]¶
Update instance with the attribute values present on
data.- Parameters:
data¶ – An instance that should have a value for
id_attributethat exists in the collection.- Returns:
The updated instance.
- Raises:
NotFoundError – If no instance found with same identifier as
data.
- abstractmethod async update_many(data: list[T]) list[T][source]¶
Update multiple instances with the attribute values present on instances in
data.- Parameters:
data¶ – A list of instance that should have a value for
id_attributethat exists in the collection.- Returns:
a list of the updated instances.
- Raises:
NotFoundError – If no instance found with same identifier as
data.
- abstractmethod async upsert(data: T) T[source]¶
Update or create instance.
Updates instance with the attribute values present on
data, or creates a new instance if one doesn’t exist.- Parameters:
data¶ – Instance to update existing, or be created. Identifier used to determine if an existing instance exists is the value of an attribute on
datanamed as value ofid_attribute.- Returns:
The updated or created instance.
- Raises:
NotFoundError – If no instance found with same identifier as
data.
- abstractmethod async upsert_many(data: list[T]) list[T][source]¶
Update or create multiple instances.
Update instances with the attribute values present on
data, or create a new instance if one doesn’t exist.- Parameters:
data¶ – Instances to update or created. Identifier used to determine if an existing instance exists is the value of an attribute on
datanamed as value ofid_attribute.- Returns:
The updated or created instances.
- Raises:
NotFoundError – If no instance found with same identifier as
data.
- class litestar.repository.abc.AbstractSyncRepository[source]¶
Bases:
Generic[T]Interface for persistent data interaction.
- abstractmethod add(data: T) T[source]¶
Add
datato the collection.- Parameters:
data¶ – Instance to be added to the collection.
- Returns:
The added instance.
- abstractmethod add_many(data: list[T]) list[T][source]¶
Add multiple
datato the collection.- Parameters:
data¶ – Instances to be added to the collection.
- Returns:
The added instances.
- static check_not_found(item_or_none: T | None) T[source]¶
Raise
NotFoundErrorifitem_or_noneisNone.- Parameters:
item_or_none¶ – Item (
T) to be tested for existence.- Returns:
The item, if it exists.
- abstractmethod count(*filters: FilterTypes, **kwargs: Any) int[source]¶
Get the count of records returned by a query.
- abstractmethod delete(item_id: Any) T[source]¶
Delete instance identified by
item_id.- Parameters:
item_id¶ – Identifier of instance to be deleted.
- Returns:
The deleted instance.
- Raises:
NotFoundError – If no instance found identified by
item_id.
- abstractmethod delete_many(item_ids: list[Any]) list[T][source]¶
Delete multiple instances identified by list of IDs
item_ids.- Parameters:
item_ids¶ – list of Identifiers to be deleted.
- Returns:
The deleted instances.
- abstractmethod exists(*filters: FilterTypes, **kwargs: Any) bool[source]¶
Return true if the object specified by
kwargsexists.
- abstractmethod filter_collection_by_kwargs(collection: CollectionT, /, **kwargs: Any) CollectionT[source]¶
Filter the collection by kwargs.
Has
ANDsemantics where multiple kwargs name/value pairs are provided.- Parameters:
- Returns:
The filtered objects
- Raises:
RepositoryError – if a named attribute doesn’t exist on
model_type.
- classmethod get_id_attribute_value(item: T | type[T], id_attribute: str | None = None) Any[source]¶
Get value of attribute named as
id_attributeonitem.- Parameters:
item¶ – Anything that should have an attribute named as
id_attributevalue.id_attribute¶ – Allows customization of the unique identifier to use for model fetching. Defaults to None, but can reference any surrogate or candidate key for the table.
- Returns:
The value of attribute on
itemnamed asid_attribute.
- abstractmethod get_one(**kwargs: Any) T[source]¶
Get an instance specified by the
kwargsfilters if it exists.- Parameters:
**kwargs¶ – Instance attribute value filters.
- Returns:
The retrieved instance.
- Raises:
NotFoundError – If no instance found identified by
kwargs.
- abstractmethod get_one_or_none(**kwargs: Any) T | None[source]¶
Get an instance if it exists or None.
- Parameters:
**kwargs¶ – Instance attribute value filters.
- Returns:
The retrieved instance or None.
- abstractmethod get_or_create(**kwargs: Any) tuple[T, bool][source]¶
Get an instance specified by the
kwargsfilters if it exists or create it.- Parameters:
**kwargs¶ – Instance attribute value filters.
- Returns:
A tuple that includes the retrieved or created instance, and a boolean on whether the record was created or not
- id_attribute: Any = 'id'¶
Name of the primary identifying attribute on
model_type.
- abstractmethod list(*filters: FilterTypes, **kwargs: Any) list[T][source]¶
Get a list of instances, optionally filtered.
- abstractmethod list_and_count(*filters: FilterTypes, **kwargs: Any) tuple[list[T], int][source]¶
List records with total count.
- classmethod set_id_attribute_value(item_id: Any, item: T, id_attribute: str | None = None) T[source]¶
Return the
itemafter the ID is set to the appropriate attribute.- Parameters:
item_id¶ – Value of ID to be set on instance
item¶ – Anything that should have an attribute named as
id_attributevalue.id_attribute¶ – Allows customization of the unique identifier to use for model fetching. Defaults to None, but can reference any surrogate or candidate key for the table.
- Returns:
Item with
item_idset toid_attribute
- abstractmethod update(data: T) T[source]¶
Update instance with the attribute values present on
data.- Parameters:
data¶ – An instance that should have a value for
id_attributethat exists in the collection.- Returns:
The updated instance.
- Raises:
NotFoundError – If no instance found with same identifier as
data.
- abstractmethod update_many(data: list[T]) list[T][source]¶
Update multiple instances with the attribute values present on instances in
data.- Parameters:
data¶ – A list of instance that should have a value for
id_attributethat exists in the collection.- Returns:
a list of the updated instances.
- Raises:
NotFoundError – If no instance found with same identifier as
data.
- abstractmethod upsert(data: T) T[source]¶
Update or create instance.
Updates instance with the attribute values present on
data, or creates a new instance if one doesn’t exist.- Parameters:
data¶ – Instance to update existing, or be created. Identifier used to determine if an existing instance exists is the value of an attribute on
datanamed as value ofid_attribute.- Returns:
The updated or created instance.
- Raises:
NotFoundError – If no instance found with same identifier as
data.
- abstractmethod upsert_many(data: list[T]) list[T][source]¶
Update or create multiple instances.
Update instances with the attribute values present on
data, or create a new instance if one doesn’t exist.- Parameters:
data¶ – Instances to update or created. Identifier used to determine if an existing instance exists is the value of an attribute on
datanamed as value ofid_attribute.- Returns:
The updated or created instances.
- Raises:
NotFoundError – If no instance found with same identifier as
data.