{"modulePath":"github.com/get-eventually/go-eventually/core","version":"v0.2.1","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package aggregate\n\nPackage aggregate defines interfaces and types necessary to allow users to define their own Aggregate types.\n\n## Variables\n\n```go\nvar ErrRootNotFound = fmt.Errorf(\"aggregate: root not found\")\n```\nErrRootNotFound is returned when the Aggregate Root requested through a Repository was not found.\n\n## Functions\n\n```go\nfunc RecordThat[I ID](root Root[I], event ...event.Envelope) error\n```\nRecordThat records the Domain Event for the specified Aggregate Root.\n\nAn error is typically returned if applying the Domain Event on the Aggregate Root instance fails with an error.\n\n```go\nfunc RehydrateFromEvents[I ID](root Root[I], eventStream event.StreamRead) error\n```\nRehydrateFromEvents rehydrates an Aggregate Root from a read-only Event Stream.\n\n```go\nfunc RehydrateFromState[I ID, Src Root[I], Dst any](\n\tv version.Version,\n\tdst Dst,\n\tdeserializer serde.Deserializer[Src, Dst],\n) (Src, error)\n```\nRehydrateFromState rehydrates an aggregate.Root instance using a state type, typically coming from an external state type (e.g. Protobuf type) and aggregate.Repository implementation (e.g. eventuallypostgres.AggregateRepository).\n\n## Types\n\n```go\ntype Aggregate interface {\n\t// Apply applies the specified Event to the Aggregate Root,\n\t// by causing a state change in the Aggregate Root instance.\n\t//\n\t// Since this method cause a state change, implementors should make sure\n\t// to use pointer semantics on their Aggregate Root method receivers.\n\t//\n\t// Please note, this method should not perform any kind of external request\n\t// and should be, save for the Aggregate Root state mutation, free of side effects.\n\t// For this reason, this method does not include a context.Context instance\n\t// in the input parameters.\n\tApply(event.Event) error\n}\n```\nAggregate is the segregated interface, part of the Aggregate Root interface, that describes the left-folding behavior of Domain Events to update the Aggregate Root state.\n\n```go\ntype BaseRoot struct {\n\t// contains filtered or unexported fields\n}\n```\nBaseRoot segregates and completes the aggregate.Root interface implementation when embedded to a user-defined Aggregate Root type.\n\nBaseRoot provides some common traits, such as tracking the current Aggregate Root version, and the recorded-but-uncommitted Domain Events, through the aggregate.RecordThat function.\n\n```go\nfunc (br *BaseRoot) FlushRecordedEvents() []event.Envelope\n```\nFlushRecordedEvents returns the list of uncommitted, recorded Domain Events through the Aggregate Root.\n\nThe internal list kept by aggregate.BaseRoot is reset.\n\n```go\nfunc (br BaseRoot) Version() version.Version\n```\nVersion returns the current version of the Aggregate Root instance.\n\n```go\ntype EventSourcedRepository[I ID, T Root[I]] struct {\n\t// contains filtered or unexported fields\n}\n```\nEventSourcedRepository provides an aggregate.Repository interface implementation that uses an event.Store to store and load the state of the Aggregate Root.\n\n```go\nfunc NewEventSourcedRepository[I ID, T Root[I]](eventStore event.Store, typ Type[I, T]) EventSourcedRepository[I, T]\n```\nNewEventSourcedRepository returns a new EventSourcedRepository implementation to store and load Aggregate Roots, specified by the aggregate.Type, using the provided event.Store implementation.\n\n```go\nfunc (repo EventSourcedRepository[I, T]) Get(ctx context.Context, id I) (T, error)\n```\nGet returns the Aggregate Root with the specified id.\n\naggregate.ErrRootNotFound is returned if no Aggregate Root was found with that id.\n\nAn error is returned if the underlying Event Store fails, or if an error occurs while trying to rehydrate the Aggregate Root state from its Event Stream.\n\n```go\nfunc (repo EventSourcedRepository[I, T]) Save(ctx context.Context, root T) error\n```\nSave stores the Aggregate Root to the Event Store, by adding the new, uncommitted Domain Events recorded through the Root, if any.\n\nAn error is returned if the underlying Event Store fails.\n\n```go\ntype Factory[I ID, T Root[I]] func() T\n```\nFactory is a function that creates new zero-valued instances of an aggregate.Root implementation.\n\n```go\ntype FusedRepository[I ID, T Root[I]] struct {\n\tGetter[I, T]\n\tSaver[I, T]\n}\n```\nFusedRepository is a convenience type that can be used to fuse together different implementations for the Getter and Saver Repository interface components.\n\n```go\ntype Getter[I ID, T Root[I]] interface {\n\tGet(ctx context.Context, id I) (T, error)\n}\n```\nGetter is an Aggregate Repository interface component, that can be used for retrieving Aggregate Roots from some storage.\n\n```go\ntype ID interface {\n\tfmt.Stringer\n}\n```\nID represents an Aggregate ID type.\n\nAggregate IDs should be able to be marshaled into a string format, in order to be saved onto a named Event Stream.\n\n```go\ntype Internal interface {\n\tFlushRecordedEvents() []event.Envelope\n}\n```\nInternal contains some Aggregate Root methods that are used by internal packages and modules for this library.\n\nDirect usage of these methods are discouraged.\n\n```go\ntype Repository[I ID, T Root[I]] interface {\n\tGetter[I, T]\n\tSaver[I, T]\n}\n```\nRepository is an interface used to get Aggregate Roots from and save them to some kind of storage, depending on the implementation.\n\n```go\ntype Root[I ID] interface {\n\tAggregate\n\tInternal\n\n\t// AggregateID returns the Aggregate Root identifier.\n\tAggregateID() I\n\n\t// Version returns the current Aggregate Root version.\n\t// The version gets updated each time a new event is recorded\n\t// through the aggregate.RecordThat function.\n\tVersion() version.Version\n\t// contains filtered or unexported methods\n}\n```\nRoot is the interface describing an Aggregate Root instance.\n\nThis interface should be implemented by your Aggregate Root types. Make sure your Aggregate Root types embed the aggregate.BaseRoot type to complete the implementation of this interface.\n\n```go\ntype Saver[I ID, T Root[I]] interface {\n\tSave(ctx context.Context, root T) error\n}\n```\nSaver is an Aggregate Repository interface component, that can be used for storing Aggregate Roots in some storage.\n\n```go\ntype Type[I ID, T Root[I]] struct {\n\tName    string\n\tFactory func() T\n}\n```\nType represents the type of an Aggregate, which will expose the name of the Aggregate (used as Event Store type).\n\nIf your Aggregate implementation uses pointers, use the factory to return a non-nil instance of the type.\n\n","path":"github.com/get-eventually/go-eventually/core/aggregate","name":"aggregate","synopsis":"Package aggregate defines interfaces and types necessary to allow users to define their own Aggregate types.","isRedistributable":true}
