filter

package
v1.24.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeUUID = "uuid" // UUID columns: eq/ne use value as-is; LIKE uses CAST(column AS TEXT)
)

Field type constants for AllowedFields

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 maps field names to types: "string", "int", "time", TypeUUID, etc.
	// Use TypeUUID for UUID columns so that LIKE is applied as CAST(column AS TEXT) LIKE ?.
	// If empty, all fields are allowed.
	AllowedFields map[string]string
	// 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{}
	FieldType string // from Config.AllowedFields; used e.g. for UUID LIKE cast
}

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