query

package
v0.0.0-...-51ec7a6 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package query parses and applies safe, resource-defined PostgreSQL queries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(selectQuery *bun.SelectQuery, params Params, definition Definition) *bun.SelectQuery

func ApplyPagination

func ApplyPagination(selectQuery *bun.SelectQuery, params Params) *bun.SelectQuery

func EscapeLike

func EscapeLike(value string) string

Types

type Definition

type Definition struct {
	Filters        []Field
	Searchable     []string
	Sortable       []SortField
	DefaultSort    []Sort
	DefaultPage    int
	DefaultPerPage int
	MaxPerPage     int
	// MaxPage bounds the deepest requestable page. Offset pagination degrades
	// with depth, and an unbounded page overflows the computed offset, so the
	// zero value resolves to a finite default rather than no limit.
	MaxPage int
	// DisableTotalCount skips the per-request COUNT that backs Total and Pages.
	// The count is a second scan of the filtered set on every list request.
	DisableTotalCount bool
	// SearchPredicate replaces the default case-insensitive contains search.
	// The default cannot use an index; supply this to route search through
	// pg_trgm, a tsvector column, or any other indexed strategy.
	SearchPredicate func(*bun.SelectQuery, string) *bun.SelectQuery
}

func (Definition) Validate

func (definition Definition) Validate() error

func (Definition) WithDefaults

func (definition Definition) WithDefaults() Definition

type Field

type Field struct {
	Name      string
	Column    string
	Type      ValueType
	Operators []Operator
}

type Filter

type Filter struct {
	Field    Field
	Operator Operator
	Value    any
}

type Operator

type Operator string
const (
	Eq       Operator = "eq"
	Neq      Operator = "neq"
	Gt       Operator = "gt"
	Gte      Operator = "gte"
	Lt       Operator = "lt"
	Lte      Operator = "lte"
	In       Operator = "in"
	Contains Operator = "contains"
)

type Params

type Params struct {
	Page    int
	PerPage int
	Search  string
	Filters []Filter
	Sort    []Sort
}

func Parse

func Parse(values url.Values, definition Definition) (Params, error)

type Sort

type Sort struct {
	Field      string
	Column     string
	Descending bool
}

type SortField

type SortField struct {
	Name   string
	Column string
}

type ValueType

type ValueType string
const (
	String    ValueType = "string"
	Bool      ValueType = "bool"
	Int       ValueType = "int"
	Float     ValueType = "float"
	UUID      ValueType = "uuid"
	Date      ValueType = "date"
	Timestamp ValueType = "timestamp"
)

Jump to

Keyboard shortcuts

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