Skip to content

Command Repository

The CommandRepository in comby is a high-level abstraction that integrates with a CommandStore to manage commands and their domain-specific data. It provides methods to add, retrieve, and list commands, integrating seamlessly with the underlying CommandStore for persistence and the DomainCommandProvider for domain-specific data enrichment.

INFO

The DomainCommandProvider is an internal component used by the facade to enrich raw command data with domain-specific context, ensuring that commands are ready for consumption before being returned to the user.

Integration with Facade

The repository interacts with the CommandStore for command persistence.

Users can utilize any compatible CommandStore implementation (e.g., in-memory or PostgreSQL) by passing it to the repository during the initialization of the facade. The repository abstracts the underlying storage mechanism, enabling users to seamlessly switch between different storage backends without altering the application logic. All that’s required is to instantiate the desired CommandStore and provide it to the facade, which handles the rest.

go
import "github.com/gradientzero/comby/v2"
import "github.com/gradientzero/comby/v2/store"

// create in-memory command store
commandStoreMemory := comby.NewCommandStoreMemory()

// or SQLite command store
commandStoreSQLite := store.NewCommandStoreSQLite("/path/to/command-store.db")

// create facade with command store
fc, _ := comby.NewFacade(
    comby.FacadeWithCommandStore(commandStoreMemory),
    // or
    comby.FacadeWithCommandStore(commandStoreSQLite),
)

// get command repository
commandRepo := fc.GetCommandRepository()

Core Responsibilities

  • Add Commands: Adds new commands to the repository and persists them using the underlying CommandStore.
  • Retrieve Commands: Fetches individual commands by their unique identifier, enriching them with domain-specific data if available.
  • List Commands: Supports paginated and filtered retrieval of commands based on tenant, aggregate, domain, or creation timestamps.
  • Integration with DomainCommandProvider: The repository uses the DomainCommandProvider to map raw domain command data to runtime domain command representations, ensuring that the retrieved commands are fully contextualized for application use.
  • Abstraction of CommandStore: The repository abstracts interactions with the CommandStore, allowing developers to seamlessly switch between storage backends (e.g., in-memory, PostgreSQL).

Key Methods

The CommandRepository provides the following key methods for managing commands:

Add Command

Adds a new command to the repository using options to configure the command details. This method delegates the creation process to the CommandStore.

go
AddCommand(ctx context.Context, opts ...CommandRepositoryAddOption) error

Returns an error if the operation fails.

OptionsDescription
CommandRepositoryAddOptionWithCommand(c Command)Specifies the command to be added to the repository.

Get Command

Retrieves a specific command by its unique UUID. The method ensures the DomainCommandProvider enriches the raw command data with domain-specific context.

go
GetCommand(ctx context.Context, opts ...CommandRepositoryGetOption) (Command, error)

Returns the command instance and an error if the operation fails.

OptionsDescription
CommandRepositoryGetOptionWithCommandUuid(commandUuid string)Specifies the UUID of the command to retrieve.

List Commands

Lists commands based on provided options. Filters include tenant UUID, aggregate UUID, domain, and time-based constraints (e.g., before/after timestamps). Sorting and pagination are also supported.

go
ListCommands(ctx context.Context, opts ...CommandRepositoryListOption) ([]Command, int64, error)

Returns a list of commands, the total amount of found commands, and an error if the operation fails.

OptionsDescription
CommandRepositoryListOptionWithTenantUuid(tenantUuid string)Filters commands by tenant UUID.
CommandRepositoryListOptionBefore(unixTimestamp int64)Filters commands created before the specified Unix timestamp.
CommandRepositoryListOptionAfter(unixTimestamp int64)Filters commands created after the specified Unix timestamp.
CommandRepositoryListOptionOffset(offset int64)Sets the offset for pagination.
CommandRepositoryListOptionLimit(limit int64)Sets the limit for the number of commands to retrieve.
CommandRepositoryListOptionOrderBy(orderBy string)Sets the field to order the commands by.
CommandRepositoryListOptionAscending(ascending bool)Sets the sorting order (ascending or descending).

Delete Command

Deletes an command by its unique UUID, ensuring the operation propagates through the underlying store.

go
DeleteCommand(ctx context.Context, opts ...CommandRepositoryDeleteOption) error
OptionsDescription
CommandRepositoryDeleteOptionWithCommandUuid(commandUuid string)Specifies the UUID of the command to delete.