filter

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

AllowedOperators is a map of all allowed operators

Functions

func ApplyFilters

func ApplyFilters(query *gorm.DB, filters []Filter) *gorm.DB

ApplyFilters applies filters to a GORM query

func ApplyFiltersFromContext

func ApplyFiltersFromContext(c *fiber.Ctx, query *gorm.DB, config *Config) (*gorm.DB, error)

ApplyFiltersFromContext is a convenience function that parses and applies filters

Types

type Config

type Config struct {
	// AllowedFields is a map of allowed field names to their types
	// If empty, all fields are allowed
	AllowedFields map[string]string // "field_name" -> "string", "int", "time", etc.
	// FieldMapping maps query parameter names to database column names
	// If empty, field names are used as-is
	FieldMapping map[string]string
	// CustomValidators allows custom validation for specific fields
	CustomValidators map[string]func(value string) error
}

Config holds filter configuration

type Filter

type Filter struct {
	Field    string
	Operator Operator
	Value    interface{}
}

Filter represents a single filter condition

func ParseFilters

func ParseFilters(c *fiber.Ctx, config *Config) ([]Filter, error)

ParseFilters parses filters from Fiber query parameters Format: field_operator=value (e.g., created_at_gte=2024-01-01, status_eq=active)

type Operator

type Operator string

Operator represents a filter operator

const (
	// OperatorEQ equals
	OperatorEQ Operator = "eq"
	// OperatorNE not equals
	OperatorNE Operator = "ne"
	// OperatorGT greater than
	OperatorGT Operator = "gt"
	// OperatorGTE greater than or equal
	OperatorGTE Operator = "gte"
	// OperatorLT less than
	OperatorLT Operator = "lt"
	// OperatorLTE less than or equal
	OperatorLTE Operator = "lte"
	// OperatorLIKE like (for strings)
	OperatorLIKE Operator = "like"
	// OperatorIN in (for arrays)
	OperatorIN Operator = "in"
	// OperatorNOTIN not in (for arrays)
	OperatorNOTIN Operator = "not_in"
)

Jump to

Keyboard shortcuts

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