Skip to content

Releases: go-goyave/filter

v0.7.0

11 Jun 14:38
4b44f9e
Compare
Choose a tag to compare

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 in LOWER() if the column is a string, resulting in ORDER 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 function filter.NewRequest(query).
  • filter.Settings.Scope() and filter.Scope() now return an error instead of a *gorm.DB.
  • filter.Filter.Scope(), filter.Join.Scope(), filter.Sort.Scope() now take a filter.Blacklist instead of *filter.Settings.
  • filter.Validation is now a goyave.RuleSetFunc, and should be used with route.ValidateQuery().
  • The field fields in a request validated with filter.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

12 Apr 14:56
60c7424
Compare
Choose a tag to compare

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

03 Nov 14:52
1402f6e
Compare
Choose a tag to compare
  • 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

02 Nov 16:26
901baf2
Compare
Choose a tag to compare
  • Updated GORM.
  • Fixed some issues with the latest version of GORM.

v0.5.1

19 Oct 07:53
e3955fc
Compare
Choose a tag to compare
  • 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

19 Sep 14:36
e8601ee
Compare
Choose a tag to compare
  • Added ScopeUnpaginated() to make it possible to use the library without paginating.

v0.4.0

29 Aug 12:05
0f76542
Compare
Choose a tag to compare
  • Added support for computed columns
  • Added the ability to change the separator used for query parsing #5

v0.3.7

02 Aug 08:42
e305b7f
Compare
Choose a tag to compare
  • Fixed IsFinal not taken into account in filters and sorts.

v0.3.6

02 Aug 07:55
70465fd
Compare
Choose a tag to compare
  • Handle AS keyword in the raw join duplication prevention

v0.3.5

01 Aug 15:33
a4fb56e
Compare
Choose a tag to compare
  • Auto-join injector now also checks raw joins in Statement.Joins to avoid duplicate joins.