rql (REST Query Language) package provides a powerful query parser for REST APIs, enabling advanced filtering, sorting, pagination, and grouping through JSON query parameters.
Features
- Advanced Filtering: Support for multiple operators (eq, neq, like, gt, lt, etc.)
- Type Validation: Validates query parameters against struct definitions
- Sorting: Multi-field sorting with ascending/descending order
- Pagination: Built-in offset and limit support
- Search: Fuzzy search across multiple fields
- Group By: Result grouping capabilities
- Type Safety: Strong typing with struct tags
Installation
Quick Start
Define Your Model
Parse and Validate Query
Core Types
Query
Filter
Sort
Struct Tags
Userql tags to define field properties:
name: API field name (used in queries)type: Data type (number, string, datetime, bool)
Data Types
Supported data types and their operators:Number
Type:numberOperators:
eq, neq, gt, lt, gte, lte
String
Type:stringOperators:
eq, neq, like, notlike, in, notin, empty, notempty
Boolean
Type:boolOperators:
eq, neq
DateTime
Type:datetimeOperators:
eq, neq, gt, lt, gte, lteFormat: RFC3339 (ISO 8601)
Operators
| Operator | Description | Example |
|---|---|---|
eq | Equals | {"operator": "eq", "value": 10} |
neq | Not equals | {"operator": "neq", "value": 0} |
gt | Greater than | {"operator": "gt", "value": 100} |
lt | Less than | {"operator": "lt", "value": 50} |
gte | Greater than or equal | {"operator": "gte", "value": 18} |
lte | Less than or equal | {"operator": "lte", "value": 65} |
like | Pattern match | {"operator": "like", "value": "john"} |
notlike | Pattern not match | {"operator": "notlike", "value": "test"} |
in | In list | {"operator": "in", "value": "a,b,c"} |
notin | Not in list | {"operator": "notin", "value": "x,y"} |
empty | Is empty/null | {"operator": "empty"} |
notempty | Is not empty | {"operator": "notempty"} |
Validation
ValidateQuery
- Field names exist in struct
- Data types match struct definitions
- Operators are valid for field types
- Sort fields are valid
- Group by fields are valid
GetDataTypeOfField
Complete Example: REST API Handler
Query Examples
Basic Filtering
With Sorting
With Search
Date Range Query
Complex Query
Best Practices
Validate All Queries
Validate All Queries
Always validate user queries before execution:
Set Default Limits
Set Default Limits
Protect against large queries:
Use Struct Tags Consistently
Use Struct Tags Consistently
Document Supported Fields
Document Supported Fields
Provide API documentation showing:
- Available fields for filtering
- Supported operators per field
- Valid sort fields