zitadel/internal/eventstore/v2/search_query.go

162 lines
4.8 KiB
Go
Raw Normal View History

2020-09-24 06:52:10 +00:00
package eventstore
import (
"github.com/caos/zitadel/internal/errors"
2020-09-30 08:00:05 +00:00
"github.com/caos/zitadel/internal/eventstore/v2/repository"
2020-09-24 06:52:10 +00:00
)
2020-10-06 19:28:09 +00:00
//SearchQueryFactory represents the builder for your filter
// if invalid data are set the filter will fail
2020-09-24 06:52:10 +00:00
type SearchQueryFactory struct {
2020-09-30 08:00:05 +00:00
columns repository.Columns
2020-09-24 06:52:10 +00:00
limit uint64
desc bool
aggregateTypes []AggregateType
aggregateIDs []string
eventSequence uint64
eventTypes []EventType
resourceOwner string
}
2020-10-06 19:28:09 +00:00
// Columns defines which fields of the event are needed for the query
2020-09-30 08:00:05 +00:00
type Columns repository.Columns
2020-09-24 06:52:10 +00:00
const (
2020-10-06 19:28:09 +00:00
//ColumnsEvent represents all fields of an event
ColumnsEvent Columns = repository.ColumnsEvent
// ColumnsMaxSequence represents the latest sequence of the filtered events
ColumnsMaxSequence Columns = repository.ColumnsMaxSequence
2020-09-24 06:52:10 +00:00
)
2020-10-06 19:28:09 +00:00
// AggregateType is the object name
2020-09-30 08:00:05 +00:00
type AggregateType repository.AggregateType
2020-10-06 19:28:09 +00:00
// EventType is the description of the change
2020-09-30 08:00:05 +00:00
type EventType repository.EventType
2020-10-06 19:28:09 +00:00
// NewSearchQueryFactory creates a new factory for event filters
// aggregateTypes must contain at least one aggregate type
func NewSearchQueryFactory(columns Columns, aggregateTypes ...AggregateType) *SearchQueryFactory {
2020-09-24 06:52:10 +00:00
return &SearchQueryFactory{
2020-10-06 19:28:09 +00:00
columns: repository.Columns(columns),
2020-09-24 06:52:10 +00:00
aggregateTypes: aggregateTypes,
}
}
func (factory *SearchQueryFactory) Columns(columns Columns) *SearchQueryFactory {
2020-09-30 08:00:05 +00:00
factory.columns = repository.Columns(columns)
2020-09-24 06:52:10 +00:00
return factory
}
func (factory *SearchQueryFactory) Limit(limit uint64) *SearchQueryFactory {
factory.limit = limit
return factory
}
func (factory *SearchQueryFactory) SequenceGreater(sequence uint64) *SearchQueryFactory {
factory.eventSequence = sequence
return factory
}
func (factory *SearchQueryFactory) AggregateIDs(ids ...string) *SearchQueryFactory {
factory.aggregateIDs = ids
return factory
}
func (factory *SearchQueryFactory) EventTypes(types ...EventType) *SearchQueryFactory {
factory.eventTypes = types
return factory
}
func (factory *SearchQueryFactory) ResourceOwner(resourceOwner string) *SearchQueryFactory {
factory.resourceOwner = resourceOwner
return factory
}
func (factory *SearchQueryFactory) OrderDesc() *SearchQueryFactory {
factory.desc = true
return factory
}
func (factory *SearchQueryFactory) OrderAsc() *SearchQueryFactory {
factory.desc = false
return factory
}
2020-10-06 19:28:09 +00:00
func (factory *SearchQueryFactory) build() (*repository.SearchQuery, error) {
2020-09-24 06:52:10 +00:00
if factory == nil ||
len(factory.aggregateTypes) < 1 ||
2020-10-06 19:28:09 +00:00
factory.columns.Validate() != nil {
2020-09-24 06:52:10 +00:00
return nil, errors.ThrowPreconditionFailed(nil, "MODEL-tGAD3", "factory invalid")
}
2020-09-30 08:00:05 +00:00
filters := []*repository.Filter{
2020-09-24 06:52:10 +00:00
factory.aggregateTypeFilter(),
}
2020-09-30 08:00:05 +00:00
for _, f := range []func() *repository.Filter{
2020-09-24 06:52:10 +00:00
factory.aggregateIDFilter,
factory.eventSequenceFilter,
factory.eventTypeFilter,
factory.resourceOwnerFilter,
} {
if filter := f(); filter != nil {
2020-10-06 19:28:09 +00:00
if err := filter.Validate(); err != nil {
return nil, err
}
2020-09-24 06:52:10 +00:00
filters = append(filters, filter)
}
}
2020-09-30 08:00:05 +00:00
return &repository.SearchQuery{
Columns: repository.Columns(factory.columns),
2020-09-24 06:52:10 +00:00
Limit: factory.limit,
Desc: factory.desc,
Filters: filters,
}, nil
}
2020-09-30 08:00:05 +00:00
func (factory *SearchQueryFactory) aggregateIDFilter() *repository.Filter {
2020-09-24 06:52:10 +00:00
if len(factory.aggregateIDs) < 1 {
return nil
}
if len(factory.aggregateIDs) == 1 {
2020-10-06 19:28:09 +00:00
return repository.NewFilter(repository.FieldAggregateID, factory.aggregateIDs[0], repository.OperationEquals)
2020-09-24 06:52:10 +00:00
}
2020-10-06 19:28:09 +00:00
return repository.NewFilter(repository.FieldAggregateID, factory.aggregateIDs, repository.OperationIn)
2020-09-24 06:52:10 +00:00
}
2020-09-30 08:00:05 +00:00
func (factory *SearchQueryFactory) eventTypeFilter() *repository.Filter {
2020-09-24 06:52:10 +00:00
if len(factory.eventTypes) < 1 {
return nil
}
if len(factory.eventTypes) == 1 {
2020-10-06 19:28:09 +00:00
return repository.NewFilter(repository.FieldEventType, factory.eventTypes[0], repository.OperationEquals)
2020-09-24 06:52:10 +00:00
}
2020-10-06 19:28:09 +00:00
return repository.NewFilter(repository.FieldEventType, factory.eventTypes, repository.OperationIn)
2020-09-24 06:52:10 +00:00
}
2020-09-30 08:00:05 +00:00
func (factory *SearchQueryFactory) aggregateTypeFilter() *repository.Filter {
2020-09-24 06:52:10 +00:00
if len(factory.aggregateTypes) == 1 {
2020-10-06 19:28:09 +00:00
return repository.NewFilter(repository.FieldAggregateType, factory.aggregateTypes[0], repository.OperationEquals)
2020-09-24 06:52:10 +00:00
}
2020-10-06 19:28:09 +00:00
return repository.NewFilter(repository.FieldAggregateType, factory.aggregateTypes, repository.OperationIn)
2020-09-24 06:52:10 +00:00
}
2020-09-30 08:00:05 +00:00
func (factory *SearchQueryFactory) eventSequenceFilter() *repository.Filter {
2020-09-24 06:52:10 +00:00
if factory.eventSequence == 0 {
return nil
}
2020-10-06 19:28:09 +00:00
sortOrder := repository.OperationGreater
2020-09-24 06:52:10 +00:00
if factory.desc {
2020-10-06 19:28:09 +00:00
sortOrder = repository.OperationLess
2020-09-24 06:52:10 +00:00
}
2020-10-06 19:28:09 +00:00
return repository.NewFilter(repository.FieldSequence, factory.eventSequence, sortOrder)
2020-09-24 06:52:10 +00:00
}
2020-09-30 08:00:05 +00:00
func (factory *SearchQueryFactory) resourceOwnerFilter() *repository.Filter {
2020-09-24 06:52:10 +00:00
if factory.resourceOwner == "" {
return nil
}
2020-10-06 19:28:09 +00:00
return repository.NewFilter(repository.FieldResourceOwner, factory.resourceOwner, repository.OperationEquals)
2020-09-24 06:52:10 +00:00
}