Documentation ¶
Index ¶
- Variables
- func ConvertArgsToSafeType(args []string, dataType DataType) ([]any, bool)
- func ConvertToSafeType(arg string, dataType DataType) (any, bool)
- func Scope[T any](db *gorm.DB, request *Request, dest *[]T) (*database.Paginator[T], error)
- func ScopeUnpaginated[T any](db *gorm.DB, request *Request, dest *[]T) *gorm.DB
- func Validation(_ *goyave.Request) v.RuleSet
- type Blacklist
- type DataType
- type FieldsValidator
- type Filter
- type FilterValidator
- type Join
- type JoinValidator
- type Operator
- type Request
- type Search
- type Settings
- type Sort
- type SortOrder
- type SortValidator
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultPageSize the default pagination page size if the "per_page" query param // isn't provided. DefaultPageSize = 10 )
var ( // Operators definitions. The key is the query representation of the operator, (e.g. "$eq"). Operators = map[string]*Operator{ "$eq": {Function: basicComparison("="), RequiredArguments: 1}, "$ne": {Function: basicComparison("<>"), RequiredArguments: 1}, "$gt": {Function: basicComparison(">"), RequiredArguments: 1}, "$lt": {Function: basicComparison("<"), RequiredArguments: 1}, "$gte": {Function: basicComparison(">="), RequiredArguments: 1}, "$lte": {Function: basicComparison("<="), RequiredArguments: 1}, "$starts": { Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB { if dataType != DataTypeText && dataType != DataTypeEnum { return filter.Where(tx, "FALSE") } query := castEnumAsText(column, dataType) + " LIKE ?" value := sqlutil.EscapeLike(filter.Args[0]) + "%" return filter.Where(tx, query, value) }, RequiredArguments: 1, }, "$ends": { Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB { if dataType != DataTypeText && dataType != DataTypeEnum { return filter.Where(tx, "FALSE") } query := castEnumAsText(column, dataType) + " LIKE ?" value := "%" + sqlutil.EscapeLike(filter.Args[0]) return filter.Where(tx, query, value) }, RequiredArguments: 1, }, "$cont": { Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB { if dataType != DataTypeText && dataType != DataTypeEnum { return filter.Where(tx, "FALSE") } query := castEnumAsText(column, dataType) + " LIKE ?" value := "%" + sqlutil.EscapeLike(filter.Args[0]) + "%" return filter.Where(tx, query, value) }, RequiredArguments: 1, }, "$excl": { Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB { if dataType != DataTypeText && dataType != DataTypeEnum { return filter.Where(tx, "FALSE") } query := castEnumAsText(column, dataType) + " NOT LIKE ?" value := "%" + sqlutil.EscapeLike(filter.Args[0]) + "%" return filter.Where(tx, query, value) }, RequiredArguments: 1, }, "$in": {Function: multiComparison("IN"), RequiredArguments: 1}, "$notin": {Function: multiComparison("NOT IN"), RequiredArguments: 1}, "$isnull": { Function: func(tx *gorm.DB, filter *Filter, column string, _ DataType) *gorm.DB { return filter.Where(tx, column+" IS NULL") }, RequiredArguments: 0, }, "$istrue": { Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB { if dataType != DataTypeBool { return filter.Where(tx, "FALSE") } return filter.Where(tx, column+" IS TRUE") }, RequiredArguments: 0, }, "$isfalse": { Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB { if dataType != DataTypeBool { return filter.Where(tx, "FALSE") } return filter.Where(tx, column+" IS FALSE") }, RequiredArguments: 0, }, "$notnull": { Function: func(tx *gorm.DB, filter *Filter, column string, _ DataType) *gorm.DB { return filter.Where(tx, column+" IS NOT NULL") }, RequiredArguments: 0, }, "$between": { Function: func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB { if dataType.IsArray() { return filter.Where(tx, "FALSE") } args, ok := ConvertArgsToSafeType(filter.Args[:2], dataType) if !ok { return filter.Where(tx, "FALSE") } query := castEnumAsText(column, dataType) + " BETWEEN ? AND ?" return filter.Where(tx, query, args...) }, RequiredArguments: 2, }, } )
var Separator = "||"
Separator the separator used when parsing the query
Functions ¶
func ConvertArgsToSafeType ¶ added in v0.6.0
ConvertArgsToSafeType converts a slice of string arguments to safe type that matches the column's data type in the same way as `ConvertToSafeType`. If any of the values in the given slice could not be converted, returns false.
func ConvertToSafeType ¶ added in v0.6.0
ConvertToSafeType convert the string argument to a safe type that matches the column's data type. Returns false if the input could not be converted.
func ScopeUnpaginated ¶ added in v0.5.0
ScopeUnpaginated using the default FilterSettings. See `FilterSettings.ScopeUnpaginated()` for more details.
func Validation ¶ added in v0.7.0
Validation returns a new RuleSet for query validation.
Types ¶
type Blacklist ¶
type Blacklist struct { Relations map[string]*Blacklist // FieldsBlacklist prevent the fields in this list to be selected or to // be used in filters and sorts. FieldsBlacklist []string // RelationsBlacklist prevent joining the relations in this list. RelationsBlacklist []string // IsFinal if true, prevent joining any relation IsFinal bool }
Blacklist definition of blacklisted relations and fields.
type DataType ¶ added in v0.6.0
type DataType string
DataType is determined by the `filterType` struct tag (see `DataType` for available options). If not given, uses GORM's general DataType. Raw database data types are not supported so it is recommended to always specify a `filterType` in this scenario.
const ( DataTypeText DataType = "text" DataTypeTextArray DataType = "text[]" DataTypeEnum DataType = "enum" DataTypeEnumArray DataType = "enum[]" DataTypeBool DataType = "bool" DataTypeBoolArray DataType = "bool[]" DataTypeInt8 DataType = "int8" DataTypeInt8Array DataType = "int8[]" DataTypeInt16 DataType = "int16" DataTypeInt16Array DataType = "int16[]" DataTypeInt32 DataType = "int32" DataTypeInt32Array DataType = "int32[]" DataTypeInt64 DataType = "int64" DataTypeInt64Array DataType = "int64[]" DataTypeUint8 DataType = "uint8" DataTypeUint8Array DataType = "uint8[]" DataTypeUint16 DataType = "uint16" DataTypeUint16Array DataType = "uint16[]" DataTypeUint32 DataType = "uint32" DataTypeUint32Array DataType = "uint32[]" DataTypeUint64 DataType = "uint64" DataTypeUint64Array DataType = "uint64[]" DataTypeFloat32 DataType = "float32" DataTypeFloat32Array DataType = "float32[]" DataTypeFloat64 DataType = "float64" DataTypeFloat64Array DataType = "float64[]" DataTypeTime DataType = "time" DataTypeTimeArray DataType = "time[]" // DataTypeUnsupported all fields with this tag will be ignored in filters and search. DataTypeUnsupported DataType = "-" )
Supported DataTypes
type FieldsValidator ¶ added in v0.7.0
type FieldsValidator struct {
v.BaseValidator
}
FieldsValidator splits the string field under validation by comma and trims every element.
func (*FieldsValidator) IsType ¶ added in v0.7.0
func (v *FieldsValidator) IsType() bool
IsType returns true
func (*FieldsValidator) Name ¶ added in v0.7.0
func (v *FieldsValidator) Name() string
Name returns the string name of the validator.
type Filter ¶
Filter structured representation of a filter query. The generic parameter is the type pointer type of the model.
func ParseFilter ¶
ParseFilter parse a string in format "field||$operator||value" and return a Filter struct. The filter string must satisfy the used operator's "RequiredArguments" constraint, otherwise an error is returned.
type FilterValidator ¶ added in v0.7.0
type FilterValidator struct { v.BaseValidator Or bool }
FilterValidator checks the `filter` format and converts it to `*Filter` struct.
func (*FilterValidator) IsType ¶ added in v0.7.0
func (v *FilterValidator) IsType() bool
IsType returns true
func (*FilterValidator) Name ¶ added in v0.7.0
func (v *FilterValidator) Name() string
Name returns the string name of the validator.
type Join ¶
Join structured representation of a join query.
type JoinValidator ¶ added in v0.7.0
type JoinValidator struct {
v.BaseValidator
}
JoinValidator checks the `sort` format and converts it to `*Join` struct.
func (*JoinValidator) IsType ¶ added in v0.7.0
func (v *JoinValidator) IsType() bool
IsType returns true
func (*JoinValidator) Name ¶ added in v0.7.0
func (v *JoinValidator) Name() string
Name returns the string name of the validator.
type Operator ¶
type Operator struct { Function func(tx *gorm.DB, filter *Filter, column string, dataType DataType) *gorm.DB RequiredArguments uint8 }
Operator used by filters to build the SQL query. The operator function modifies the GORM statement (most of the time by adding a WHERE condition) then returns the modified statement.
Operators may need arguments (e.g. "$eq", equals needs a value to compare the field to); RequiredArguments define the minimum number of arguments a client must send in order to use this operator in a filter. RequiredArguments is checked during Filter parsing.
Operators may return the given tx without change if they don't support the given dataType or add a condition that will always be false.
type Request ¶ added in v0.7.0
type Request struct { Search typeutil.Undefined[string] Filter typeutil.Undefined[[]*Filter] Or typeutil.Undefined[[]*Filter] Sort typeutil.Undefined[[]*Sort] Join typeutil.Undefined[[]*Join] Fields typeutil.Undefined[[]string] Page typeutil.Undefined[int] PerPage typeutil.Undefined[int] }
Request DTO for a filter query. Any non-present option will be ignored.
func NewRequest ¶ added in v0.7.0
NewRequest creates a filter request from an HTTP request's query. Uses the following entries in the query, expected to be validated:
- search
- filter
- or
- sort
- join
- fields
- page
- per_page
If a field in the query doesn't match the expected type (non-validated) for the filtering option, it will be ignored without an error.
type Settings ¶
type Settings[T any] struct { // DefaultSort if not nil and not empty, and if the request is not providing any // sort, the request will be sorted according to the `*Sort` defined in this slice. // If `DisableSort` is enabled, this has no effect. DefaultSort []*Sort // FieldsSearch allows search for these fields FieldsSearch []string // SearchOperator is used by the search scope, by default it use the $cont operator SearchOperator *Operator Blacklist // DisableFields ignore the "fields" query if true. DisableFields bool // DisableFilter ignore the "filter" query if true. DisableFilter bool // DisableSort ignore the "sort" query if true. DisableSort bool // DisableJoin ignore the "join" query if true. DisableJoin bool // DisableSearch ignore the "search" query if true. DisableSearch bool // CaseInsensitiveSort if true, the sort will wrap the value in `LOWER()` if it's a string, // resulting in `ORDER BY LOWER(column)`. CaseInsensitiveSort bool }
Settings settings to disable certain features and/or blacklist fields and relations. The generic type is the pointer type of the model.
func (*Settings[T]) Scope ¶
func (s *Settings[T]) Scope(db *gorm.DB, request *Request, dest *[]T) (*database.Paginator[T], error)
Scope apply all filters, sorts and joins defined in the request's data to the given `*gorm.DB` and process pagination. Returns the resulting `*database.Paginator`. The given request is expected to be validated using `ApplyValidation`.
func (*Settings[T]) ScopeUnpaginated ¶ added in v0.5.0
ScopeUnpaginated apply all filters, sorts and joins defined in the request's data to the given `*gorm.DB` without any pagination. Returns the `*gorm.DB` result, which can be used to check for database errors. The records will be added in the given `dest` slice. The given request is expected to be validated using `ApplyValidation`.
type Sort ¶
Sort structured representation of a sort query. The generic parameter is the type pointer type of the model.
type SortValidator ¶ added in v0.7.0
type SortValidator struct {
v.BaseValidator
}
SortValidator checks the `sort` format and converts it to `*Sort` struct.
func (*SortValidator) IsType ¶ added in v0.7.0
func (v *SortValidator) IsType() bool
IsType returns true
func (*SortValidator) Name ¶ added in v0.7.0
func (v *SortValidator) Name() string
Name returns the string name of the validator.