2020-09-24 06:52:10 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-09-24 06:52:10 +00:00
|
|
|
)
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
//SearchQueryBuilder represents the builder for your filter
|
2020-10-06 19:28:09 +00:00
|
|
|
// if invalid data are set the filter will fail
|
2020-12-01 13:44:19 +00:00
|
|
|
type SearchQueryBuilder struct {
|
2021-07-06 11:55:57 +00:00
|
|
|
columns repository.Columns
|
|
|
|
limit uint64
|
|
|
|
desc bool
|
|
|
|
resourceOwner string
|
|
|
|
queries []*SearchQuery
|
|
|
|
}
|
|
|
|
|
|
|
|
type SearchQuery struct {
|
|
|
|
builder *SearchQueryBuilder
|
2020-09-24 06:52:10 +00:00
|
|
|
aggregateTypes []AggregateType
|
|
|
|
aggregateIDs []string
|
|
|
|
eventSequence uint64
|
|
|
|
eventTypes []EventType
|
2020-11-23 18:31:12 +00:00
|
|
|
eventData map[string]interface{}
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
|
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-12-01 13:44:19 +00:00
|
|
|
// NewSearchQueryBuilder creates a new factory for event filters
|
2020-10-06 19:28:09 +00:00
|
|
|
// aggregateTypes must contain at least one aggregate type
|
2021-07-06 11:55:57 +00:00
|
|
|
func NewSearchQueryBuilder(columns Columns) *SearchQueryBuilder {
|
2020-12-01 13:44:19 +00:00
|
|
|
return &SearchQueryBuilder{
|
2021-07-06 11:55:57 +00:00
|
|
|
columns: repository.Columns(columns),
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
//Columns defines which fields are set
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) Columns(columns Columns) *SearchQueryBuilder {
|
2020-09-30 08:00:05 +00:00
|
|
|
factory.columns = repository.Columns(columns)
|
2020-09-24 06:52:10 +00:00
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
//Limit defines how many events are returned maximally.
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) Limit(limit uint64) *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.limit = limit
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
//ResourceOwner defines the resource owner (org) of the events
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) ResourceOwner(resourceOwner string) *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.resourceOwner = resourceOwner
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
//OrderDesc changes the sorting order of the returned events to descending
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) OrderDesc() *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.desc = true
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
//OrderAsc changes the sorting order of the returned events to ascending
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) OrderAsc() *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.desc = false
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
//AddQuery creates a new sub query.
|
|
|
|
//All fields in the sub query are AND-connected in the storage request.
|
|
|
|
//Multiple sub queries are OR-connected in the storage request.
|
|
|
|
func (factory *SearchQueryBuilder) AddQuery() *SearchQuery {
|
|
|
|
query := &SearchQuery{
|
|
|
|
builder: factory,
|
|
|
|
}
|
|
|
|
factory.queries = append(factory.queries, query)
|
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
//Or creates a new sub query on the search query builder
|
|
|
|
func (query SearchQuery) Or() *SearchQuery {
|
|
|
|
return query.builder.AddQuery()
|
|
|
|
}
|
|
|
|
|
|
|
|
//AggregateTypes filters for events with the given aggregate types
|
|
|
|
func (query *SearchQuery) AggregateTypes(types ...AggregateType) *SearchQuery {
|
|
|
|
query.aggregateTypes = types
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
//SequenceGreater filters for events with sequence greater the requested sequence
|
|
|
|
func (query *SearchQuery) SequenceGreater(sequence uint64) *SearchQuery {
|
|
|
|
query.eventSequence = sequence
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
//AggregateIDs filters for events with the given aggregate id's
|
|
|
|
func (query *SearchQuery) AggregateIDs(ids ...string) *SearchQuery {
|
|
|
|
query.aggregateIDs = ids
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
//EventTypes filters for events with the given event types
|
|
|
|
func (query *SearchQuery) EventTypes(types ...EventType) *SearchQuery {
|
|
|
|
query.eventTypes = types
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
//EventData filters for events with the given event data.
|
|
|
|
//Use this call with care as it will be slower than the other filters.
|
|
|
|
func (query *SearchQuery) EventData(data map[string]interface{}) *SearchQuery {
|
|
|
|
query.eventData = data
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
//Builder returns the SearchQueryBuilder of the sub query
|
|
|
|
func (query *SearchQuery) Builder() *SearchQueryBuilder {
|
|
|
|
return query.builder
|
2020-11-23 18:31:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) build() (*repository.SearchQuery, error) {
|
2020-09-24 06:52:10 +00:00
|
|
|
if factory == nil ||
|
2021-07-06 11:55:57 +00:00
|
|
|
len(factory.queries) < 1 ||
|
2020-10-06 19:28:09 +00:00
|
|
|
factory.columns.Validate() != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "MODEL-4m9gs", "factory invalid")
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2021-07-06 11:55:57 +00:00
|
|
|
filters := make([][]*repository.Filter, len(factory.queries))
|
|
|
|
|
|
|
|
for i, query := range factory.queries {
|
|
|
|
for _, f := range []func() *repository.Filter{
|
|
|
|
query.aggregateTypeFilter,
|
|
|
|
query.aggregateIDFilter,
|
|
|
|
query.eventSequenceFilter,
|
|
|
|
query.eventTypeFilter,
|
|
|
|
query.eventDataFilter,
|
|
|
|
query.builder.resourceOwnerFilter,
|
|
|
|
} {
|
|
|
|
if filter := f(); filter != nil {
|
|
|
|
if err := filter.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filters[i] = append(filters[i], filter)
|
2020-10-06 19:28:09 +00:00
|
|
|
}
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 08:00:05 +00:00
|
|
|
return &repository.SearchQuery{
|
2021-01-28 05:35:26 +00:00
|
|
|
Columns: factory.columns,
|
2020-09-24 06:52:10 +00:00
|
|
|
Limit: factory.limit,
|
|
|
|
Desc: factory.desc,
|
|
|
|
Filters: filters,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
func (query *SearchQuery) aggregateIDFilter() *repository.Filter {
|
|
|
|
if len(query.aggregateIDs) < 1 {
|
2020-09-24 06:52:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-06 11:55:57 +00:00
|
|
|
if len(query.aggregateIDs) == 1 {
|
|
|
|
return repository.NewFilter(repository.FieldAggregateID, query.aggregateIDs[0], repository.OperationEquals)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2021-07-06 11:55:57 +00:00
|
|
|
return repository.NewFilter(repository.FieldAggregateID, query.aggregateIDs, repository.OperationIn)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
func (query *SearchQuery) eventTypeFilter() *repository.Filter {
|
|
|
|
if len(query.eventTypes) < 1 {
|
2020-09-24 06:52:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-06 11:55:57 +00:00
|
|
|
if len(query.eventTypes) == 1 {
|
|
|
|
return repository.NewFilter(repository.FieldEventType, repository.EventType(query.eventTypes[0]), repository.OperationEquals)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2021-07-06 11:55:57 +00:00
|
|
|
eventTypes := make([]repository.EventType, len(query.eventTypes))
|
|
|
|
for i, eventType := range query.eventTypes {
|
2021-02-08 10:30:30 +00:00
|
|
|
eventTypes[i] = repository.EventType(eventType)
|
|
|
|
}
|
|
|
|
return repository.NewFilter(repository.FieldEventType, eventTypes, repository.OperationIn)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
func (query *SearchQuery) aggregateTypeFilter() *repository.Filter {
|
|
|
|
if len(query.aggregateTypes) == 1 {
|
|
|
|
return repository.NewFilter(repository.FieldAggregateType, repository.AggregateType(query.aggregateTypes[0]), repository.OperationEquals)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2021-07-06 11:55:57 +00:00
|
|
|
aggregateTypes := make([]repository.AggregateType, len(query.aggregateTypes))
|
|
|
|
for i, aggregateType := range query.aggregateTypes {
|
2021-02-08 10:30:30 +00:00
|
|
|
aggregateTypes[i] = repository.AggregateType(aggregateType)
|
|
|
|
}
|
|
|
|
return repository.NewFilter(repository.FieldAggregateType, aggregateTypes, repository.OperationIn)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
func (query *SearchQuery) eventSequenceFilter() *repository.Filter {
|
|
|
|
if query.eventSequence == 0 {
|
2020-09-24 06:52:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-06 19:28:09 +00:00
|
|
|
sortOrder := repository.OperationGreater
|
2021-07-06 11:55:57 +00:00
|
|
|
if query.builder.desc {
|
2020-10-06 19:28:09 +00:00
|
|
|
sortOrder = repository.OperationLess
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2021-07-06 11:55:57 +00:00
|
|
|
return repository.NewFilter(repository.FieldSequence, query.eventSequence, sortOrder)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) 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
|
|
|
}
|
2020-11-23 18:31:12 +00:00
|
|
|
|
2021-07-06 11:55:57 +00:00
|
|
|
func (query *SearchQuery) eventDataFilter() *repository.Filter {
|
|
|
|
if len(query.eventData) == 0 {
|
2020-11-23 18:31:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-06 11:55:57 +00:00
|
|
|
return repository.NewFilter(repository.FieldEventData, query.eventData, repository.OperationJSONContains)
|
2020-11-23 18:31:12 +00:00
|
|
|
}
|