mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 19:17:32 +00:00
write model
This commit is contained in:
@@ -17,6 +17,21 @@ func NewAggregate(
|
||||
}
|
||||
}
|
||||
|
||||
func AggregateFromWriteModel(
|
||||
wm *WriteModel,
|
||||
typ AggregateType,
|
||||
version Version,
|
||||
) *Aggregate {
|
||||
return &Aggregate{
|
||||
id: wm.AggregateID,
|
||||
typ: typ,
|
||||
resourceOwner: wm.ResourceOwner,
|
||||
version: version,
|
||||
previousSequence: wm.ProcessedSequence,
|
||||
events: []EventPusher{},
|
||||
}
|
||||
}
|
||||
|
||||
//Aggregate is the basic implementation of aggregater
|
||||
type Aggregate struct {
|
||||
id string `json:"-"`
|
||||
|
@@ -167,6 +167,26 @@ func (es *Eventstore) LatestSequence(ctx context.Context, queryFactory *SearchQu
|
||||
return es.repo.LatestSequence(ctx, query)
|
||||
}
|
||||
|
||||
type queryReducer interface {
|
||||
reducer
|
||||
//Query returns the SearchQueryFactory for the events needed in reducer
|
||||
Query() *SearchQueryFactory
|
||||
}
|
||||
|
||||
//FilterToQueryReducer filters the events based on the search query of the query function,
|
||||
// appends all events to the reducer and calls it's reduce function
|
||||
func (es *Eventstore) FilterToQueryReducer(ctx context.Context, r queryReducer) error {
|
||||
events, err := es.FilterEvents(ctx, r.Query())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = r.AppendEvents(events...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return r.Reduce()
|
||||
}
|
||||
|
||||
//RegisterFilterEventMapper registers a function for mapping an eventstore event to an event
|
||||
func (es *Eventstore) RegisterFilterEventMapper(eventType EventType, mapper func(*repository.Event) (EventReader, error)) *Eventstore {
|
||||
if mapper == nil || eventType == "" {
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package eventstore
|
||||
|
||||
func NewReadModel() *ReadModel {
|
||||
return &ReadModel{
|
||||
func NewWriteModel() *WriteModel {
|
||||
return &WriteModel{
|
||||
Events: []EventReader{},
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ type WriteModel struct {
|
||||
AggregateID string `json:"-"`
|
||||
ProcessedSequence uint64 `json:"-"`
|
||||
Events []EventReader `json:"-"`
|
||||
ResourceOwner string `json:"-"`
|
||||
}
|
||||
|
||||
//AppendEvents adds all the events to the read model.
|
||||
@@ -24,22 +25,22 @@ func (rm *WriteModel) AppendEvents(events ...EventReader) *WriteModel {
|
||||
|
||||
//Reduce is the basic implementaion of reducer
|
||||
// If this function is extended the extending function should be the last step
|
||||
func (rm *WriteModel) Reduce() error {
|
||||
if len(rm.Events) == 0 {
|
||||
func (wm *WriteModel) Reduce() error {
|
||||
if len(wm.Events) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if rm.AggregateID == "" {
|
||||
rm.AggregateID = rm.Events[0].AggregateID()
|
||||
if wm.AggregateID == "" {
|
||||
wm.AggregateID = wm.Events[0].AggregateID()
|
||||
}
|
||||
if rm.ResourceOwner == "" {
|
||||
rm.ResourceOwner = rm.Events[0].ResourceOwner()
|
||||
if wm.ResourceOwner == "" {
|
||||
wm.ResourceOwner = wm.Events[0].ResourceOwner()
|
||||
}
|
||||
|
||||
rm.ProcessedSequence = rm.Events[len(rm.Events)-1].Sequence()
|
||||
wm.ProcessedSequence = wm.Events[len(wm.Events)-1].Sequence()
|
||||
|
||||
// all events processed and not needed anymore
|
||||
rm.Events = nil
|
||||
rm.Events = []EventReader{}
|
||||
wm.Events = nil
|
||||
wm.Events = []EventReader{}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user