Documentation ¶
Index ¶
- Constants
- Variables
- func StorageName(s AggregateState) string
- type Aggregate
- func (a *Aggregate[S]) Changes() []*Event[S]
- func (a *Aggregate[S]) Check() error
- func (a *Aggregate[S]) CheckAll() []error
- func (a *Aggregate[S]) Clone() *Aggregate[S]
- func (a *Aggregate[S]) ID() string
- func (a *Aggregate[S]) MarshalJSON() ([]byte, error)
- func (a *Aggregate[S]) Metadata() Metadata
- func (a *Aggregate[S]) Must(ii ...func(*Aggregate[S]) error)
- func (a *Aggregate[S]) MustAlso(ii ...func(*Aggregate[S]) error)
- func (a *Aggregate[S]) State() S
- func (a *Aggregate[S]) Type() string
- func (a *Aggregate[S]) Version() int
- type AggregateState
- type AggregateStateEventMapper
- type AggregateVersion
- type Event
- func (e *Event[S]) Aggregate() *Aggregate[S]
- func (e *Event[S]) Apply(aggregate *Aggregate[S])
- func (e *Event[S]) ID() string
- func (e *Event[S]) MarshalJSON() ([]byte, error)
- func (e *Event[S]) Metadata() Metadata
- func (e *Event[S]) OccurredAt() time.Time
- func (e *Event[S]) State() EventState[S]
- func (e *Event[S]) Type() string
- type EventState
- type EventStore
- type HistoryParams
- type Identifiable
- type InceptionRecorder
- type LoadParams
- type Metadata
- type ModificationRecorder
- type StorageNamer
- type Transaction
Constants ¶
const LastVersion = 0
Variables ¶
var ( // ErrAggregateNotFound is returned when an aggregate is not found. ErrAggregateNotFound = errors.New("aggregate not found") )
Functions ¶
func StorageName ¶ added in v0.2.0
func StorageName(s AggregateState) string
Types ¶
type Aggregate ¶
type Aggregate[S AggregateState] struct { Invariants []func(*Aggregate[S]) error // contains filtered or unexported fields }
Aggregate represents an aggregate in event sourcing.
func InitAggregate ¶
func InitAggregate[S AggregateState](id string, version int, state S) *Aggregate[S]
InitAggregate initializes an aggregate with the provided id, version, and state.
func InitZeroAggregate ¶
func InitZeroAggregate[S AggregateState](state S) *Aggregate[S]
InitZeroAggregate initializes an aggregate with the zero state.
func (*Aggregate[S]) Check ¶
Check checks all invariants of the aggregate and returns the first error encountered. Check returns nil if all Invariants passed without failing.
func (*Aggregate[S]) CheckAll ¶
CheckAll checks all invariants of the aggregate and returns errors for every failed Invariants. CheckAll returns nil if all Invariants passed without failing.
func (*Aggregate[S]) MarshalJSON ¶
MarshalJSON marshals the aggregate into JSON format.
func (*Aggregate[S]) MustAlso ¶
MustAlso appends given invariants to the existing list of invariants.
func (*Aggregate[S]) State ¶
func (a *Aggregate[S]) State() S
State returns the current state of the aggregate.
type AggregateState ¶
type AggregateState interface { Type() string // Returns the type of the aggregate state. Zero() AggregateState // Returns a zero value of the aggregate state. }
AggregateState is an interface defining methods that should be implemented by any state that is part of an aggregate.
type AggregateStateEventMapper ¶
type AggregateStateEventMapper[S AggregateState] interface { EventsMap() map[string]EventState[S] }
type AggregateVersion ¶
type AggregateVersion int
type Event ¶
type Event[S AggregateState] struct { // contains filtered or unexported fields }
Event represents an event in event sourcing.
func InitEvent ¶
func InitEvent[S AggregateState](id string, occurredAt time.Time, state EventState[S], aggregate *Aggregate[S], data []byte, metadata []byte) (*Event[S], error)
InitEvent initializes an Event with provided id, occurredAt, state, aggregate, data, and metadata.
func NewEvent ¶
func NewEvent[S AggregateState](event EventState[S], metadata map[string]any) *Event[S]
NewEvent creates a new event with the provided state and metadata.
func (*Event[S]) Apply ¶
Apply applies the Event to an Aggregate. In the context of event sourcing, state changes are recorded as a sequence of events. When an event is applied to an aggregate, it changes the state of the aggregate. Here's a detailed breakdown of the steps:
It first increments the version of the Aggregate using the incrementVersion method. This helps in tracking the evolution of the aggregate. Each event applied to the aggregate increases its version number.
It then appends the Event itself to the changes slice of the Aggregate. This allows the aggregate to keep track of all events that have been applied to it.
It checks whether the state of the Event implements the Identifiable interface. If it does, it sets the ID of the Aggregate to the ID provided by the Event state's AggregateID method.
It applies the state of the Event to the Aggregate using the Apply method of the EventState. This is where the actual state change occurs.
Finally, it sets the Event's aggregate field to a clone of the updated Aggregate. This is crucial for keeping an accurate record of the state of the Aggregate at the time when the event was applied.
By enabling the event sourcing pattern in the provided Go codebase, this method allows the state of an Aggregate to evolve over time through the application of Events.
func (*Event[S]) MarshalJSON ¶
MarshalJSON marshals the event into JSON format.
func (*Event[S]) OccurredAt ¶
OccurredAt returns the time when the event occurred.
func (*Event[S]) State ¶
func (e *Event[S]) State() EventState[S]
State returns the state of the event.
type EventState ¶
type EventState[S AggregateState] interface { Type() string Apply(aggregate *Aggregate[S]) }
func ParseEvent ¶
func ParseEvent[S AggregateState](t string, eventsMap map[string]EventState[S]) (EventState[S], error)
type EventStore ¶
type EventStore[S AggregateState] interface { Save(ctx context.Context, tx Transaction, aggregate ...*Aggregate[S]) (err error) Load(ctx context.Context, tx Transaction, aggregateID string, version AggregateVersion) (*Aggregate[S], error) History(ctx context.Context, tx Transaction, aggregateID string, fromVersion int, limit int) ([]*Event[S], error) }
EventStore represents a storage for events. It provides methods to save, load, and retrieve the history of an aggregate.
type HistoryParams ¶
type HistoryParams struct { AggregateID string AggregateVersion AggregateVersion }
type Identifiable ¶
type Identifiable interface { // AggregateID returns the ID of the aggregate that is associated with the identifiable object. AggregateID() string }
Identifiable is an interface representing objects that can be identified by an Aggregate ID. In the context of this event sourcing system, it's used to ensure that the state of an event can provide an Aggregate ID when the event is applied to an aggregate.
type InceptionRecorder ¶
type InceptionRecorder[S AggregateState] interface { // RecordInception marks the time at which the entity was brought into existence. RecordInception(inceptionTime *time.Time, aggregate *Aggregate[S]) }
InceptionRecorder represents an interface for entities that are capable of recording the time at which they were brought into existence.
type LoadParams ¶
type LoadParams[S AggregateState] struct { AggregateID string Aggregate S AggregateVersion AggregateVersion }
type ModificationRecorder ¶
type ModificationRecorder[S AggregateState] interface { // RecordModification marks the time at which the entity was recently modified. RecordModification(modificationTime *time.Time, aggregate *Aggregate[S]) }
ModificationRecorder represents an interface for entities that are capable of recording the time at which they were recently modified.
type StorageNamer ¶ added in v0.2.0
type StorageNamer interface {
StorageName() string
}