eventstore

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoSnapshotFound        = errors.New("no snapshot found")
	ErrNoAggreagateStateFound = errors.New("no aggregate state found")
	ErrFailedToMakeSnapshot   = errors.New("failed to make snapshot: no events found for the given aggregate id")
	ErrAggregateMustBePointer = errors.New("aggregate must be a pointer")
	ErrApplyEventToAggregate  = errors.New("aggregator must implement the ApplyEventToAggregate interface")
	ErrLastEventVersion       = errors.New("to use this method, aggregate must implement the LatestEventVersion interface")
)

Predefined errors

Functions

This section is empty.

Types

type AggregateType

type AggregateType interface {
	// AggregateType returns the type of the aggregate
	AggregateType() string
}

type ApplyEventToAggregate

type ApplyEventToAggregate interface {
	// ApplyEvent applies the event to the aggregate
	ApplyEvent(event Event) error
}

type Event

type Event struct {
	EventID      uuid.UUID `json:"event_id"`
	AggregateID  uuid.UUID `json:"aggregate_id"`
	EventType    string    `json:"event_type"`
	EventVersion int32     `json:"event_version"`
	EventData    []byte    `json:"event_data"`
	EventTime    int64     `json:"event_time"`
}

type EventData

type EventData interface {
	// EventData returns the data of the event
	EventData() []byte
}

type EventStore

type EventStore struct {
	// contains filtered or unexported fields
}

EventStore is the facade for the event store

func NewEventStore

func NewEventStore(ctx context.Context, db *sql.DB, eventStreamName string) (*EventStore, error)

NewEventStore creates a new event store

func (*EventStore) AppendEvent

func (es *EventStore) AppendEvent(ctx context.Context, aggregateID uuid.UUID, event interface{}) (Event, error)

AppendEvents appends events to the event store

func (*EventStore) EventStream

func (es *EventStore) EventStream() string

EventStream returns the event stream name

func (*EventStore) LoadCurrentState

func (es *EventStore) LoadCurrentState(ctx context.Context, aggregateID uuid.UUID, aggregate interface{}) error

LoadCurrentState loads the current state for the given aggregate id. It loads the latest snapshot and all events since the snapshot version. Then it applies all events to the snapshot and returns the state.

func (*EventStore) LoadEvents

func (es *EventStore) LoadEvents(ctx context.Context, aggregateID uuid.UUID) ([]Event, error)

LoadEvents loads all events for the given aggregate id

func (*EventStore) LoadEventsRange

func (es *EventStore) LoadEventsRange(ctx context.Context, aggregateID uuid.UUID, fromEventVersion, toEventVersion int32) ([]Event, error)

LoadEventsRange loads events for the given aggregate id and event version range

func (*EventStore) LoadLatestSnapshot

func (es *EventStore) LoadLatestSnapshot(ctx context.Context, aggregateID uuid.UUID, aggregate interface{}) (Snapshot, error)

LoadLatestSnapshot loads the latest snapshot for the given aggregate id and aggregate type

func (*EventStore) LoadNewestEvents

func (es *EventStore) LoadNewestEvents(ctx context.Context, aggregateID uuid.UUID, snapshotVersion int32) ([]Event, error)

LoadNewestEvents loads the newest events for the given aggregate id. Helpful when you have a snapshot and want to load all events since the snapshot.

func (*EventStore) LoadSnapshot

func (es *EventStore) LoadSnapshot(ctx context.Context, aggregateID uuid.UUID, aggregate interface{}, snapshotVersion int32) (Snapshot, error)

LoadSnapshot loads the snapshot for the given aggregate id and snapshot version

func (*EventStore) MakeSnapshot

func (es *EventStore) MakeSnapshot(ctx context.Context, aggregateID uuid.UUID, aggregate interface{}) error

MakeSnapshot makes a snapshot for the given aggregate id. Helpful when you want to load all events since the snapshot and apply them to the aggregator. It loads the latest snapshot and all events since the snapshot version. Then it applies all events to the snapshot and stores the new snapshot.

func (*EventStore) StoreSnapshot

func (es *EventStore) StoreSnapshot(ctx context.Context, aggregateID uuid.UUID, aggregate interface{}) error

StoreSnapshot stores a snapshot for the given aggregator. It stores the snapshot and then initializes the aggregator with the snapshot.

type EventTime

type EventTime interface {
	// EventTime returns the time of the event
	EventTime() int64
}

type EventType

type EventType interface {
	// EventType returns the type of the event
	EventType() string
}

type EventVersion

type EventVersion interface {
	// EventVersion returns the version of the event
	EventVersion() int32
}

type GetSnapshotData

type GetSnapshotData interface {
	// GetSnapshot returns the snapshot of the aggregate
	GetSnapshotData() ([]byte, error)
}

type LastEventVersion

type LastEventVersion interface {
	// LastEventVersion returns the version of the latest event
	LastEventVersion() int32
}

type LoadAggregateFromSnapshot

type LoadAggregateFromSnapshot interface {
	// LoadAggregateFromSnapshot loads the aggregate from the snapshot
	LoadSnapshot(snapshot Snapshot) error
}

type LoadEventsRangeParams

type LoadEventsRangeParams struct {
	AggregateID      uuid.UUID `json:"aggregate_id"`
	FromEventVersion int32     `json:"from_event_version"`
	ToEventVersion   int32     `json:"to_event_version"`
}

type LoadLatestSnapshotParams

type LoadLatestSnapshotParams struct {
	AggregateID   uuid.UUID `json:"aggregate_id"`
	AggregateType string    `json:"aggregate_type"`
}

type LoadNewestEventsParams

type LoadNewestEventsParams struct {
	AggregateID        uuid.UUID `json:"aggregate_id"`
	LatestEventVersion int32     `json:"latest_event_version"`
}

type LoadSnapshotParams

type LoadSnapshotParams struct {
	AggregateID     uuid.UUID `json:"aggregate_id"`
	AggregateType   string    `json:"aggregate_type"`
	SnapshotVersion int32     `json:"snapshot_version"`
}

type Snapshot

type Snapshot struct {
	SnapshotID         uuid.UUID `json:"snapshot_id"`
	AggregateID        uuid.UUID `json:"aggregate_id"`
	AggregateType      string    `json:"aggregate_type"`
	SnapshotVersion    int32     `json:"snapshot_version"`
	SnapshotData       []byte    `json:"snapshot_data"`
	SnapshotTime       int64     `json:"snapshot_time"`
	LatestEventVersion int32     `json:"latest_event_version"`
}

type StoreEventParams

type StoreEventParams struct {
	AggregateID uuid.UUID       `json:"aggregate_id"`
	EventType   string          `json:"event_type"`
	EventData   json.RawMessage `json:"event_data"`
	EventTime   int64           `json:"event_time"`
}

type StoreSnapshotParams

type StoreSnapshotParams struct {
	AggregateID        uuid.UUID       `json:"aggregate_id"`
	AggregateType      string          `json:"aggregate_type"`
	SnapshotData       json.RawMessage `json:"snapshot_data"`
	SnapshotTime       int64           `json:"snapshot_time"`
	LatestEventVersion int32           `json:"latest_event_version"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL