entfilter

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultPageSize int = 20
	MaxPageSize     int = 50_000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Filter

type Filter[Q Querier[P, O, Q], O OrderOption, P Predicate] struct {
	Limit  int
	Offset int
	// contains filtered or unexported fields
}

Filter provides a mechanism to filter, order and paginate using Ent queries. Invoke the Apply method last to apply the remaining filters.

func NewFilter

func NewFilter[Q Querier[P, O, Q], O OrderOption, P Predicate](query Q, sf SortableFields) *Filter[Q, O, P]

NewFilter returns a new Filter. It panics if orderingFields is empty.

func (*Filter[Q, O, P]) AddDateRange

func (f *Filter[Q, O, P]) AddDateRange(column string, r *timerange.Range)

func (*Filter[Q, O, P]) Apply

func (f *Filter[Q, O, P]) Apply() (page, whole Q)

Apply filters, returning queriers of the filtered subset and the page.

func (*Filter[Q, O, P]) Contains

func (f *Filter[Q, O, P]) Contains(column string, value *string)

Contains adds a filter on column containing the given string.

func (*Filter[Q, O, P]) Equals

func (f *Filter[Q, O, P]) Equals(column string, value any)

Equals adds a filter on column being equal to value. If value implements the validator interface, value is validated before the filter is added.

func (*Filter[Q, O, P]) In

func (f *Filter[Q, O, P]) In(column string, values []any)

In adds a filter on column being equal to one of the given values. Each element in values that implements validator is validated before being added to the list of filter values.

func (*Filter[Q, O, P]) OrderBy

func (f *Filter[Q, O, P]) OrderBy(sort Sort)

OrderBy sets the query sort order.

func (*Filter[Q, O, P]) Page

func (f *Filter[Q, O, P]) Page(limit, offset int)

Page sets the query limit and offset criteria.

The actual query limit will be set to a value between one and MaxPageSize based on the input limit (x) as follows: (x < 0) -> MaxPageSize (x == 0) -> DefaultPageSize (0 < x < MaxPageSize) -> x (x >= MaxPageSize) -> MaxPageSize

The actual query offset will be set based on the input offset (y) as follows: (y <= 0) -> 0 (y > 0) -> y

type OrderOption

type OrderOption interface {
	~func(s *sql.Selector)
}

OrderOption (O) is the constraint for all Ent ordering options, e.g. batch.OrderOption, transfer.OrderOption and so on.

type Predicate

type Predicate interface {
	~func(s *sql.Selector)
}

Predicate (P) is the constraint for all Ent predicates, e.g. predicate.Batch, predicate.Transfer and so on.

type Querier

type Querier[P Predicate, O OrderOption, Q any] interface {
	Where(ps ...P) Q
	Limit(int) Q
	Offset(int) Q
	Order(...O) Q
	Clone() Q
}

Querier (Q) wraps queriers methods provided by Ent queries.

type Sort

type Sort []SortColumn

Sort determines how the filtered results are sorted by specifying a slice of sort columns. The first SortColumn has the highest sort precedence, and the last SortColumn the lowest precedence.

func NewSort

func NewSort() Sort

NewSort returns a new sort instance.

func (Sort) AddCol

func (s Sort) AddCol(name string, desc bool) Sort

AddCol adds a SortColumn to a Sort then returns the updated Sort.

type SortColumn

type SortColumn struct {
	// Name of the column on which to sort the results.
	Name string

	// Desc is true if the sort order is descending.
	Desc bool
}

SortColumn specifies a column name on which to sort results, and the direction of the sort (ascending or descending).

type SortableField

type SortableField struct {
	Name    string
	Default bool
}

type SortableFields

type SortableFields map[string]SortableField

SortableFields maps column names to Ent type field names. Usage examples: batchOrderingFields, transferOrderingFields...

func (SortableFields) Columns

func (sf SortableFields) Columns() []string

func (SortableFields) Default

func (sf SortableFields) Default() SortableField

Default returns the default sort field.

Jump to

Keyboard shortcuts

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