This package provides a flexible, reusable criteria pattern implementation for querying data from various backends (SQL databases, HTTP APIs, in-memory datastores, etc.). It allows for building type-safe, composable queries with filtering, sorting, and pagination functionality.
- Type-safe implementation using Go generics - All interfaces are parameterized to ensure compile-time type checking
- Flexible filtering - Filter by field, operator, and value with multiple filter conditions
- Sorting - Sort by multiple fields in ascending or descending order
- Pagination - Paginate results with page and limit parameters
- Multi-backend support - Works with SQL databases, HTTP APIs, in-memory data, and custom backends
- Interface-based design - Easily extend for different backend systems
- SQL generation - Automatic generation of SQL queries with parameterized queries
- Validation - Validate criteria against allowed fields and operations
- SQL injection protection - Parameterized queries prevent SQL injection attacks
With Go 1.18+ generics support, the criteria pattern is implemented using type parameters to ensure type safety throughout the query building, execution, and mapping process. This eliminates the need for type assertions and reduces runtime errors.
QueryBuilder[Q any]- Builds a query of type Q from criteriaQueryExecutor[Q any, E any]- Executes a query of type Q and returns entities of type EDataMapper[E any, M any]- Maps between entity types E and model types MRepository[E any, M any, Q any]- Generic repository for querying entitiesCriteriaFilter[T any]- Converts an object to filtersCriteriaSort[T any]- Converts an object to sorts
To implement a custom backend, you need to create implementations of the following interfaces:
- A query type specific to your backend
- A
QueryBuilderimplementation that builds your query type - A
QueryExecutorimplementation that executes your query type - A
DataMapperimplementation that maps between your entities and domain models - A
Validatorimplementation that validates criteria for your backend
See the example_type_safe.go file for a complete in-memory backend implementation.
The following operators are available for filtering:
OperatorEqual- Equality comparisonOperatorNotEqual- Inequality comparisonOperatorGreaterThan- Greater than comparisonOperatorGreaterThanOrEqual- Greater than or equal comparisonOperatorLessThan- Less than comparisonOperatorLessThanOrEqual- Less than or equal comparisonOperatorContains- Contains substring (for string fields)OperatorIn- Value is in a list of valuesOperatorIsNull- Field is null
The following sort orders are available:
OrderAsc- Ascending orderOrderDesc- Descending order
- Define your domain models and database entities as separate types
- Use the
ValidationConfigto restrict which fields can be filtered and sorted - Set sensible default limits for pagination
- Use the type-safe generics to ensure compile-time checking
- Create repositories for specific entity types to simplify usage
This criteria pattern can be extended in several ways:
- Add support for more complex filter combinations (AND/OR logic)
- Implement custom operators specific to your domain
- Add support for aggregations or group-by operations
- Implement caching mechanisms for query results
- Add support for transactions