search

package
v0.7.8 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: MIT Imports: 14 Imported by: 102

Documentation

Index

Constants

View Source
const (
	PageQueryParam    string = "page"
	PerPageQueryParam string = "perPage"
	SortQueryParam    string = "sort"
	FilterQueryParam  string = "filter"
)

url search query params

View Source
const (
	SortAsc  string = "ASC"
	SortDesc string = "DESC"
)

sort field directions

View Source
const DefaultPerPage int = 30

DefaultPerPage specifies the default returned search result items.

View Source
const MaxPerPage int = 400

MaxPerPage specifies the maximum allowed search result items returned in a single page.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldResolver

type FieldResolver interface {
	// UpdateQuery allows to updated the provided db query based on the
	// resolved search fields (eg. adding joins aliases, etc.).
	//
	// Called internally by `search.Provider` before executing the search request.
	UpdateQuery(query *dbx.SelectQuery) error

	// Resolve parses the provided field and returns a properly
	// formatted db identifier (eg. NULL, quoted column, placeholder parameter, etc.).
	Resolve(field string) (name string, placeholderParams dbx.Params, err error)
}

FieldResolver defines an interface for managing search fields.

type FilterData

type FilterData string

FilterData is a filter expession string following the `fexpr` package grammar.

Example:

var filter FilterData = "id = null || (name = 'test' && status = true)"
resolver := search.NewSimpleFieldResolver("id", "name", "status")
expr, err := filter.BuildExpr(resolver)

func (FilterData) BuildExpr

func (f FilterData) BuildExpr(fieldResolver FieldResolver) (dbx.Expression, error)

BuildExpr parses the current filter data and returns a new db WHERE expression.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider represents a single configured search provider instance.

func NewProvider

func NewProvider(fieldResolver FieldResolver) *Provider

NewProvider creates and returns a new search provider.

Example:

baseQuery := db.Select("*").From("user")
fieldResolver := search.NewSimpleFieldResolver("id", "name")
models := []*YourDataStruct{}

result, err := search.NewProvider(fieldResolver).
	Query(baseQuery).
	ParseAndExec("page=2&filter=id>0&sort=-name", &models)

func (*Provider) AddFilter

func (s *Provider) AddFilter(filter FilterData) *Provider

AddFilter appends the provided FilterData to the existing provider's filter field.

func (*Provider) AddSort

func (s *Provider) AddSort(field SortField) *Provider

AddSort appends the provided SortField to the existing provider's sort field.

func (*Provider) CountColumn added in v0.3.2

func (s *Provider) CountColumn(countColumn string) *Provider

CountColumn specifies an optional distinct column to use in the SELECT COUNT query.

func (*Provider) Exec

func (s *Provider) Exec(items any) (*Result, error)

Exec executes the search provider and fills/scans the provided `items` slice with the found models.

func (*Provider) Filter

func (s *Provider) Filter(filter []FilterData) *Provider

Filter sets the `filter` field of the current search provider.

func (*Provider) Page

func (s *Provider) Page(page int) *Provider

Page sets the `page` field of the current search provider.

Normalization on the `page` value is done during `Exec()`.

func (*Provider) Parse

func (s *Provider) Parse(urlQuery string) error

Parse parses the search query parameter from the provided query string and assigns the found fields to the current search provider.

The data from the "sort" and "filter" query parameters are appended to the existing provider's `sort` and `filter` fields (aka. using `AddSort` and `AddFilter`).

func (*Provider) ParseAndExec

func (s *Provider) ParseAndExec(urlQuery string, modelsSlice any) (*Result, error)

ParseAndExec is a short conventient method to trigger both `Parse()` and `Exec()` in a single call.

func (*Provider) PerPage

func (s *Provider) PerPage(perPage int) *Provider

PerPage sets the `perPage` field of the current search provider.

Normalization on the `perPage` value is done during `Exec()`.

func (*Provider) Query

func (s *Provider) Query(query *dbx.SelectQuery) *Provider

Query sets the base query that will be used to fetch the search items.

func (*Provider) Sort

func (s *Provider) Sort(sort []SortField) *Provider

Sort sets the `sort` field of the current search provider.

type Result

type Result struct {
	Page       int `json:"page"`
	PerPage    int `json:"perPage"`
	TotalItems int `json:"totalItems"`
	TotalPages int `json:"totalPages"`
	Items      any `json:"items"`
}

Result defines the returned search result structure.

type SimpleFieldResolver

type SimpleFieldResolver struct {
	// contains filtered or unexported fields
}

SimpleFieldResolver defines a generic search resolver that allows only its listed fields to be resolved and take part in a search query.

If `allowedFields` are empty no fields filtering is applied.

func NewSimpleFieldResolver

func NewSimpleFieldResolver(allowedFields ...string) *SimpleFieldResolver

NewSimpleFieldResolver creates a new `SimpleFieldResolver` with the provided `allowedFields`.

Each `allowedFields` could be a plain string (eg. "name") or a regexp pattern (eg. `^\w+[\w\.]*$`).

func (*SimpleFieldResolver) Resolve

func (r *SimpleFieldResolver) Resolve(field string) (resultName string, placeholderParams dbx.Params, err error)

Resolve implements `search.Resolve` interface.

Returns error if `field` is not in `r.allowedFields`.

func (*SimpleFieldResolver) UpdateQuery

func (r *SimpleFieldResolver) UpdateQuery(query *dbx.SelectQuery) error

UpdateQuery implements `search.UpdateQuery` interface.

type SortField

type SortField struct {
	Name      string `json:"name"`
	Direction string `json:"direction"`
}

SortField defines a single search sort field.

func ParseSortFromString

func ParseSortFromString(str string) (fields []SortField)

ParseSortFromString parses the provided string expression into a slice of SortFields.

Example:

fields := search.ParseSortFromString("-name,+created")

func (*SortField) BuildExpr

func (s *SortField) BuildExpr(fieldResolver FieldResolver) (string, error)

BuildExpr resolves the sort field into a valid db sort expression.

Jump to

Keyboard shortcuts

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