Documentation ¶
Index ¶
- Constants
- type FieldResolver
- type FilterData
- type Provider
- func (s *Provider) AddFilter(filter FilterData) *Provider
- func (s *Provider) AddSort(field SortField) *Provider
- func (s *Provider) CountColumn(countColumn string) *Provider
- func (s *Provider) Exec(items any) (*Result, error)
- func (s *Provider) Filter(filter []FilterData) *Provider
- func (s *Provider) Page(page int) *Provider
- func (s *Provider) Parse(urlQuery string) error
- func (s *Provider) ParseAndExec(urlQuery string, modelsSlice any) (*Result, error)
- func (s *Provider) PerPage(perPage int) *Provider
- func (s *Provider) Query(query *dbx.SelectQuery) *Provider
- func (s *Provider) Sort(sort []SortField) *Provider
- type Result
- type SimpleFieldResolver
- type SortField
Constants ¶
const ( PageQueryParam string = "page" PerPageQueryParam string = "perPage" SortQueryParam string = "sort" FilterQueryParam string = "filter" )
url search query params
const ( SortAsc string = "ASC" SortDesc string = "DESC" )
sort field directions
const DefaultPerPage int = 30
DefaultPerPage specifies the default returned search result items.
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 ¶
AddSort appends the provided SortField to the existing provider's sort field.
func (*Provider) CountColumn ¶ added in v0.3.2
CountColumn specifies an optional distinct column to use in the SELECT COUNT query.
func (*Provider) Exec ¶
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 ¶
Page sets the `page` field of the current search provider.
Normalization on the `page` value is done during `Exec()`.
func (*Provider) Parse ¶
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 ¶
ParseAndExec is a short conventient method to trigger both `Parse()` and `Exec()` in a single call.
func (*Provider) PerPage ¶
PerPage sets the `perPage` field of the current search provider.
Normalization on the `perPage` value is done during `Exec()`.
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 ¶
SortField defines a single search sort field.
func ParseSortFromString ¶
ParseSortFromString parses the provided string expression into a slice of SortFields.
Example:
fields := search.ParseSortFromString("-name,+created")