Skip to content

store_base

This module defines the abstract base class for all data store implementations in Merlin.

This module provides the StoreBase class, which outlines the required interface for saving, retrieving, listing, and deleting entities from a backing data store. All concrete store classes (e.g., Redis-based stores) must inherit from this class and implement its abstract methods.

StoreBase

Bases: ABC, Generic[T]

Base class for all stores supported in Merlin.

This class defines the methods that are needed for each store in Merlin.

Methods:

Name Description
save

Save or update an object in the database.

retrieve

Retrieve an entity from the database by ID.

retrieve_all

Query the database for all entities of this type.

delete

Delete an entity from the database by ID.

Source code in merlin/backends/store_base.py
class StoreBase(ABC, Generic[T]):
    """
    Base class for all stores supported in Merlin.

    This class defines the methods that are needed for each store in Merlin.

    Methods:
        save: Save or update an object in the database.
        retrieve: Retrieve an entity from the database by ID.
        retrieve_all: Query the database for all entities of this type.
        delete: Delete an entity from the database by ID.
    """

    @abstractmethod
    def save(self, entity: BaseDataModel):
        """
        Save or update an object in the database.

        Args:
            entity: The object to save.
        """
        raise NotImplementedError("Subclasses of `StoreBase` must implement a `save` method.")

    @abstractmethod
    def retrieve(self, identifier: str) -> BaseDataModel:
        """
        Retrieve an entity from the database by an identifier.

        Args:
            identifier: The identifier (typically ID or name) of the entity to retrieve.

        Returns:
            The entity if found, None otherwise.
        """
        raise NotImplementedError("Subclasses of `StoreBase` must implement a `retrieve` method.")

    @abstractmethod
    def retrieve_all(self) -> List[BaseDataModel]:
        """
        Query the database for all entities of this type.

        Returns:
            A list of entities.
        """
        raise NotImplementedError("Subclasses of `StoreBase` must implement a `retrieve_all` method.")

    @abstractmethod
    def delete(self, identifier: str):
        """
        Delete an entity from the database by an identifier.

        Args:
            identifier: The identifier (typically ID or name) of the entity to delete.
        """
        raise NotImplementedError("Subclasses of `StoreBase` must implement a `delete` method.")

delete(identifier) abstractmethod

Delete an entity from the database by an identifier.

Parameters:

Name Type Description Default
identifier str

The identifier (typically ID or name) of the entity to delete.

required
Source code in merlin/backends/store_base.py
@abstractmethod
def delete(self, identifier: str):
    """
    Delete an entity from the database by an identifier.

    Args:
        identifier: The identifier (typically ID or name) of the entity to delete.
    """
    raise NotImplementedError("Subclasses of `StoreBase` must implement a `delete` method.")

retrieve(identifier) abstractmethod

Retrieve an entity from the database by an identifier.

Parameters:

Name Type Description Default
identifier str

The identifier (typically ID or name) of the entity to retrieve.

required

Returns:

Type Description
BaseDataModel

The entity if found, None otherwise.

Source code in merlin/backends/store_base.py
@abstractmethod
def retrieve(self, identifier: str) -> BaseDataModel:
    """
    Retrieve an entity from the database by an identifier.

    Args:
        identifier: The identifier (typically ID or name) of the entity to retrieve.

    Returns:
        The entity if found, None otherwise.
    """
    raise NotImplementedError("Subclasses of `StoreBase` must implement a `retrieve` method.")

retrieve_all() abstractmethod

Query the database for all entities of this type.

Returns:

Type Description
List[BaseDataModel]

A list of entities.

Source code in merlin/backends/store_base.py
@abstractmethod
def retrieve_all(self) -> List[BaseDataModel]:
    """
    Query the database for all entities of this type.

    Returns:
        A list of entities.
    """
    raise NotImplementedError("Subclasses of `StoreBase` must implement a `retrieve_all` method.")

save(entity) abstractmethod

Save or update an object in the database.

Parameters:

Name Type Description Default
entity BaseDataModel

The object to save.

required
Source code in merlin/backends/store_base.py
@abstractmethod
def save(self, entity: BaseDataModel):
    """
    Save or update an object in the database.

    Args:
        entity: The object to save.
    """
    raise NotImplementedError("Subclasses of `StoreBase` must implement a `save` method.")