EventStore
The EventStore
interface in comby defines the foundational contract for implementing event storage systems. It provides methods for creating, retrieving, updating, deleting, and managing events in an event-sourced architecture. By implementing this interface, users can create custom event stores tailored to their specific requirements, enabling seamless integration with comby's CQRS and Event Sourcing framework.
Interface
The EventStore
interface provides methods for initialization, event management (creation, retrieval, modification, and deletion), metadata utilities (e.g., fetching totals or unique values), and maintenance (e.g., resetting, closing connections, or retrieving store metadata). Every concrete event store implements the EventStore
interface, which defines the following methods:
type EventStore interface {
// Init initializes the event store with the provided options.
Init(ctx context.Context, opts ...EventStoreOption) error
// Create adds a new event to the store.
Create(ctx context.Context, opts ...EventStoreCreateOption) error
// Get retrieves an event by its unique identifier.
Get(ctx context.Context, opts ...EventStoreGetOption) (Event, error)
// List retrieves a list of events based on the provided options.
List(ctx context.Context, opts ...EventStoreListOption) ([]Event, int64, error)
// Update modifies an existing event in the store.
Update(ctx context.Context, opts ...EventStoreUpdateOption) error
// Delete removes an event by its unique identifier.
Delete(ctx context.Context, opts ...EventStoreDeleteOption) error
// Total returns the total number of events in the store.
Total(ctx context.Context) int64
// UniqueList retrieves a list of unique values based on a specific field.
UniqueList(ctx context.Context, opts ...EventStoreUniqueListOption) ([]string, int64, error)
// Close closes the event store connection.
Close(ctx context.Context) error
// Options returns the configuration options of the event store.
Options() EventStoreOptions
// String returns a string representation of the event store.
String() string
// Info provides detailed information about the event store.
Info(ctx context.Context) (*EventStoreInfoModel, error)
// Reset clears all events from the store.
Reset(ctx context.Context) error
}
Options and Methods
Init
The Init method allows the event store to be configured with options such as read-only mode or logging levels. This ensures flexibility in adapting the store to different environments.
Options | Description |
---|---|
EventStoreOptionWithReadOnly(readOnly bool) | Specifies whether the store is read-only. |
EventStoreOptionWithAttribute(keys string, value any) | Sets custom attributes for the event store. |
EventStoreOptionWithCryptoService(cryptoService *comby.CryptoService) | Sets the crypto service for the command store to use. |
Create
The Create method enables the addition of new events to the store.
Options | Description |
---|---|
EventStoreCreateOptionWithEvent(evt Event) | Specifies the event to be added. |
EventStoreCreateOptionWithAttribute(keys string, value any) | Sets custom attributes for the Create option. |
Get
Events can be fetched individually using Get method.
Options | Description |
---|---|
EventStoreGetOptionWithEventUuid(eventUuid string) | Retrieves the specific event by its UUID. |
EventStoreGetOptionWithAttribute(keys string, value any) | Sets custom attributes for the Get option. |
List
Events can be fetched in bulk using List, with support for filtering and pagination.
Options | Description |
---|---|
EventStoreListOptionWithTenantUuid(tenantUuid string) | Filters events by tenant UUID. |
EventStoreListOptionWithAggregateUuid(aggregateUuid string) | Filters events by aggregate UUID. |
EventStoreListOptionWithDomains(domain ...string) | Filters events by domain. |
EventStoreListOptionWithDataType(dataType string) | Filters events by data type (=type as string of the domain event). |
EventStoreListOptionBefore(unixTimestamp int64) | Filters events created before the specified timestamp. |
EventStoreListOptionAfter(unixTimestamp int64) | Filters events created after the specified timestamp. |
EventStoreListOptionOffset(offset int64) | Sets the offset for pagination. |
EventStoreListOptionLimit(limit int64) | Sets the limit for pagination. |
EventStoreListOptionOrderBy(orderBy string) | Sets the field to order the events by. |
EventStoreListOptionAscending(ascending bool) | Sets the sorting order (ascending or descending). |
EventStoreListOptionWithAttribute(keys string, value any) | Sets custom attributes for the List object. |
Update
The Update method allows updates to existing events.
Options | Description |
---|---|
EventStoreUpdateOptionWithEvent(evt Event) | Specifies the event to be updated. |
EventStoreUpdateOptionWithAttribute(keys string, value any) | Sets custom attributes for the Update object. |
Delete
Events can be removed using the Delete method.
Options | Description |
---|---|
EventStoreDeleteOptionWithEventUuid(eventUuid string) | Specifies the UUID of the event to delete. |
EventStoreDeleteOptionWithAttribute(keys string, value any) | Sets custom attributes for the Delete object. |
Total
Retrieves the total count of events in the store.
UniqueList
Fetches unique values based on a specified field, enabling analytics and metadata exploration.
Options | Description |
---|---|
EventStoreUniqueListOptionWithTenantUuid(tenantUuid string) | Filters unique values by tenant UUID. |
EventStoreUniqueListOptionWithDomain(domain string) | Filters unique values by domain. |
EventStoreUniqueListOptionWithDbField(field string) | Specifies the field to retrieve unique values from. |
EventStoreUniqueListOptionOffset(offset int64) | Sets the offset for pagination. |
EventStoreUniqueListOptionLimit(limit int64) | Sets the limit for pagination. |
EventStoreUniqueListOptionAscending(ascending bool) | Sets the sorting order (ascending or descending). |
EventStoreUniqueListOptionWithAttribute(keys string, value any) | Sets custom attributes for the UniqueList object. |
Close
Gracefully closes the connection to the event store.
Options
Returns the configuration options of the event store.
String
Returns a string representation of the event store.
Info
Returns metadata about the store, such as the type of store, number of events, and connection details. Details are returned in the following structure:
type EventStoreInfoModel struct {
StoreType string `json:"storeType"` // Type of the event store.
LastItemCreatedAt int64 `json:"lastItemCreatedAt"` // Timestamp of the last created event.
NumItems int64 `json:"numItems"` // Number of events in the store.
ConnectionInfo string `json:"connectionInfo"` // Connection details of the event store.
}
Reset
Clears all events from the store, providing a clean slate for testing or initialization.
Extensibility and Implementation
comby provides the following default implementations of the EventStore
interface:
- In-Memory Store: Useful for testing and development, offering fast and transient event storage. Used by default if no specific store is configured.
- SQLite Store: A lightweight, file-based implementation suitable for small to medium-sized applications. The underlying used driver is modernc.org/sqlite which means the comby retains CGo-free. Link: https://github.com/gradientzero/comby-store-sqlite
- PostgreSQL Store: A robust, production-ready implementation leveraging PostgreSQL as the backend. The underlying used driver is lib/pq. Link: https://github.com/gradientzero/comby-store-postgres
Users can implement the EventStore interface to integrate with alternative storage systems, such as:
- NoSQL databases (e.g., MongoDB, DynamoDB).
- Distributed log systems (e.g., Apache Kafka, Pulsar).
- Cloud-native solutions (e.g., AWS S3, Google Cloud Firestore).
To create a custom event store, implement all methods in the interface, adhering to the specified contracts for behavior and parameters.