protocols.dao.Dao

A duck type with methods common to all DAOs.

Usage

Source

protocols.dao.Dao()

Methods

Name Description
delete() Delete a resource by providing its ID.
find_all() Find all resources that match the specified mapping.
find_one() Find the resource that matches the specified mapping.
get_by_id() Get a resource by providing its ID.
insert() Create a new resource.
update() Update an existing resource.
upsert() Update the provided resource if it already exists, create it otherwise.
with_transaction() Creates a transaction manager that uses an async context manager interface:

delete()

Delete a resource by providing its ID.

Usage

Source

delete(id_)
Parameters
id_: ID
The ID of the resource.
Raises
ResourceNotFoundError
when resource with the specified id_ was not found


find_all()

Find all resources that match the specified mapping.

Usage

Source

find_all(*, mapping, skip=None, limit=None, sort=None)

The values in the mapping are used to filter the resources. Provide them using the same Python types as the corresponding DTO model fields, e.g. a UUID object for a UUID field and a datetime object for a datetime field. The behavior for non-scalar values depends on the specific provider.

Parameters
mapping: Mapping[str, Any]

A mapping where the keys correspond to the names of resource fields and the values correspond to the actual values of the resource fields.

skip: int | None = None

Number of matching resources to skip before yielding results. Defaults to None (no skipping).

limit: int | None = None

Maximum number of resources to yield. Defaults to None (no limit). Specifying 0 is interpreted literally and returns no results.

sort: list[str] | None = None
A list of field names defining the sort order, where field names prefixed with “-” indicate descending order. For example, if sort is specified as ["name", "-age"], the results would be sorted first by “name” in ascending order, then by “age” in descending order. When paginating with skip/limit, providing a sort order is strongly recommended to ensure consistent, deterministic results. Defaults to None (no sort).
Returns
FindResult[Dto]
A FindResult that is async-iterable and also provides total_count().
Raises
InvalidMappingError

If mapping doesn’t pass validation.

ValueError
if skip or limit are less than 0.


find_one()

Find the resource that matches the specified mapping.

Usage

Source

find_one(*, mapping)

It is expected that at most one resource matches the constraints. An exception is raised if no or multiple hits are found.

The values in the mapping are used to filter the resources. Provide them using the same Python types as the corresponding DTO model fields, e.g. a UUID object for a UUID field and a datetime object for a datetime field. The behavior for non-scalar values depends on the specific provider.

Parameters
mapping: Mapping[str, Any]
A mapping where the keys correspond to the names of resource fields and the values correspond to the actual values of the resource fields
Returns
Dto

Returns a hit in the form of the respective DTO model if exactly one hit

was found that matches the given mapping.
Raises
NoHitsFoundError

If no hit was found.

MultipleHitsFoundError
Raised when obtaining more than one hit.


get_by_id()

Get a resource by providing its ID.

Usage

Source

get_by_id(id_)
Parameters
id_: ID
The ID of the resource.
Returns
Dto
The resource represented using the respective DTO model.
Raises
ResourceNotFoundError
when resource with the specified id_ was not found


insert()

Create a new resource.

Usage

Source

insert(dto)
Parameters
dto: Dto
Resource content as a pydantic-based data transfer object including the resource ID.
Raises
ResourceAlreadyExistsError

when a resource with the ID specified in the dto does already exist.

UniqueConstraintViolationError
when inserting the dto would violate a unique index constraint over some field other than the ID field.


update()

Update an existing resource.

Usage

Source

update(dto)
Parameters
dto: Dto
The updated resource content as a pydantic-based data transfer object including the resource ID.
Raises
ResourceNotFoundError

when resource with the id specified in the dto was not found

UniqueConstraintViolationError
when updating the dto would violate a unique index constraint over some field other than the ID field.


upsert()

Update the provided resource if it already exists, create it otherwise.

Usage

Source

upsert(dto)
Parameters
dto: Dto
Resource content as a pydantic-based data transfer object including the resource ID.
Raises
UniqueConstraintViolationError
when upserting the dto would violate a unique index constraint over some field other than the ID field.


with_transaction()

Creates a transaction manager that uses an async context manager interface:

Usage

Source

with_transaction()

Upon aenter, pens a new transactional scope. Returns a transaction-scoped DAO.

Upon aexit, closes the transactional scope. A full rollback of the transaction is performed in case of an exception. Otherwise, the changes to the database are committed and flushed.