py4ai.data.layer.mongo.repository module

Module for Repository pattern for MongoDB persistence layers.

class py4ai.data.layer.mongo.repository.MongoRepository(collection: MotorCollection, serializer: DataSerializer[KE, KD, E, Dict[Any, Any]], session: Optional[MotorClientSession] = None)

Bases: Repository[KE, KD, E, Dict[Any, Any], Dict[str, Any]], Generic[KE, KD, E]

Class implementing MongoDB repository.

Return a MongoDB Repository Implementation.

The current implementation uses the Motor async framework.

Parameters
  • collection – MongoDB collection

  • serializer – Serializer to be used to serialize/deserialize MongoDB documents into domain objects

  • session – MongoDB session to be used for unit-of-work operations

async create(entity: E) E

Create the entity in the underlying persistence layer.

Parameters

entity – Entity to be created

Returns

same entity provided as input, after creation. If creation fails, an error should be returned.

async delete(key: KE) bool

Delete the entry in the persisence layer associated to the provided entity key.

Parameters

key – key identifying the entity.

Returns

boolean value indicating whether the deletion has completed successfully.

async delete_by_criteria(criteria: SearchCriteria[Dict[str, Any]]) bool

Delete all entries matching a given query.

Parameters

criteria – query to be used for deleting entries.

Returns

boolean value indicating whether the deletion has completed successfully.

async list(options: ~py4ai.data.layer.common.repository.QueryOptions = <py4ai.data.layer.common.repository.QueryOptions object>) Paged[E]

Return a full list of entities stored in the persistence layer.

Parameters

options – query options to be used when retrieving data

Returns

Paged object for retrieved list of entities

async retrieve(key: KE) Optional[E]

Return an entry corresponding to a determined Entity key. If no match is found, returns None.

Parameters

key – Entity Key to be used for retrieving the entity.

Returns

Entity associated with the provided key. If no match is found, None is returned.

async retrieve_by_criteria(criteria: ~py4ai.data.layer.common.criteria.SearchCriteria[~typing.Dict[str, ~typing.Any]], options: ~py4ai.data.layer.common.repository.QueryOptions = <py4ai.data.layer.common.repository.QueryOptions object>) Paged[E]

Return a list of entities, matching the query provided.

Parameters
  • criteria – query to be used for selecting items

  • options – query options to be used when retrieving data

Returns

Paged object for retrieved list of entities

async save(entities: Sequence[E]) Sequence[E]

Create the entries in the persistence layer associated to a list of entities.

Parameters

entities – list of entities to be created.

Returns

list of entities that have been successfully created in the persistence layer

property serializer: DataSerializer[KE, KD, E, Dict[Any, Any]]

Return the serializer.

Returns

DataSerializer for serializing/deserializing MongoDB documents into domain objects