Releases: go-goyave/filter
v0.7.0
The library has been upgraded for Goyave v5.
The filters library didn't allow decoupling of the HTTP layer and the data layer because of its dependency to the *goyave.Request
. It was therefore impossible to move its uses to a repository, where they belongs. By creating a DTO for this specific use and changing the error handling, filters can now be properly integrated in the new architecture. They were also upgraded to take advantage of the generics.
filter.Settings
,filter.Scope()
,filter.ScopeUnpaginated()
now take a generic parameter representing the model being filtered.filter.Settings.CaseInsensitiveSort
new option wraps the sort values inLOWER()
if the column is a string, resulting inORDER BY LOWER(column)
.filter.Settings.DefaultSort
new option allows to define a list of sorts to be used if the user request didn't contain any sort.filter.Settings.Scope()
,filter.Settings.ScopeUnpaginated()
,filter.Scope()
,filter.ScopeUnpaginated()
now take a*filter.Request
instead of a*goyave.Request
. This request can be created from a HTTP query with the new functionfilter.NewRequest(query)
.filter.Settings.Scope()
andfilter.Scope()
now return anerror
instead of a*gorm.DB
.filter.Filter.Scope()
,filter.Join.Scope()
,filter.Sort.Scope()
now take afilter.Blacklist
instead of*filter.Settings
.filter.Validation
is now agoyave.RuleSetFunc
, and should be used withroute.ValidateQuery()
.- The field
fields
in a request validated withfilter.Validation
will now always be a[]string
. - The page info and records SQL queries are now executed inside a transaction.
- Validation error messages names had a "goyave-filter-" prefix added.
v0.6.0
Type safety
Added type-safety. The operators now generate valid SQL matching the column's data type. The data type is determined by the new struct tag fieldType
. If not provided, the type will be determined from GORM's data type. If GORM's data type is a custom type or a type that is not directly supported by this library, the type will fall back to -
(unsupported).
See the updated README for more details.
Breaking change
This change breaks custom operators: the function signature changed.
//Before
func(tx *gorm.DB, filter *filter.Filter, column string, dataType schema.DataType) *gorm.DB
// After
func(tx *gorm.DB, f *filter.Filter, column string, dataType filter.DataType) *gorm.DB
You may need to update your models by adding the new fieldType
struct tag on non-native types (such as arrays and enum types). Structures implementing driver.Valuer
should work without needing for a change.
type MyModel struct {
ID uint
SomeArray []string `gorm:"type:VARCHAR(255)[]" filterType:"text[]"`
SomeEnum MyCustomEnum `gorm:"type:my_custom_enum" filterType:"enum"`
}
Fields having a custom database type set (gorm:"type:DOUBLE PRECISION"
for example) should work too as the type detection is based on GORMDataType
and not on the actual DataType
.
Types from the guregu/null
library should work without having to add the fieldType
tag since they implement driver.Valuer
.
You may also need to update computed columns that would return a type that doesn't exactly match the struct field. This is common when working with JSON nested fields. Database engines usually always return text types from JSON operations, which is incompatible with numbers.
// This example is compatible with PostgreSQL.
// JSON processing may be different if you are using another database engine.
type MyModel struct {
ID uint
JSONColumn datatypes.JSON
SomeJSONField null.Int `gorm:"->;-:migration" computed:"(~~~ct~~~.json_column->>'fieldName')::int"`
}
v0.5.3
- Added support for computed columns in manual joins. This avoids "column doesn't exist" error when working with computed columns in relations.
- Fixed some issues related to selected columns when using manual joins.
v0.5.2
- Updated GORM.
- Fixed some issues with the latest version of GORM.
v0.5.1
- Fixed automatic column selection not working in joins. Before the fix, only the ID and foreign keys were automatically selected if no field were provided by the client in the "join".
v0.5.0
- Added
ScopeUnpaginated()
to make it possible to use the library without paginating.
v0.4.0
v0.3.7
- Fixed
IsFinal
not taken into account in filters and sorts.
v0.3.6
- Handle
AS
keyword in the raw join duplication prevention
v0.3.5
- Auto-join injector now also checks raw joins in
Statement.Joins
to avoid duplicate joins.