PG Filters
A powerful Rust helper to generate PostgreSQL SQL for pagination, sorting, and advanced filtering with support for complex AND/OR conditions.
Usage
Column Definitions
First, define your column types:
use HashMap;
use ColumnDef;
Simple Filtering
Basic filtering with multiple AND conditions:
use ;
use ;
use SortedColumn;
let columns = setup_columns;
// Create simple conditions
let name_condition = Condition;
let age_condition = Condition;
let filters = new?;
let sql = filters.sql?;
// Results in: WHERE (LOWER(name) = LOWER('John') AND age > 18) ORDER BY age DESC, name ASC LIMIT 10 OFFSET 0
Complex Filtering
Example with complex AND/OR conditions:
use ;
let columns = setup_columns;
// Create individual conditions
let name_condition = Condition;
let age_condition = Condition;
let city_condition = Condition;
// Create a complex filter: (name = 'John' AND age > 18) OR city IN ('New York', 'London')
let filter_group = Group ;
let filters = new?;
let sql = filters.sql?;
// Results in: WHERE ((LOWER(name) = LOWER('John') AND age > 18) OR city IN ('New York', 'London')) ORDER BY name ASC LIMIT 10 OFFSET 0
JSON Filter Support
PG Filters supports creating filters from JSON input:
use ;
let columns = setup_columns;
let json_filters = vec!;
let filter_builder = from_json_filters?;
Pagination with Filtered Count
When you need to apply filtering rules for pagination:
let columns = setup_columns;
let filtering_options = new;
// Create count filters
let count_filters = new?;
// Get total count with filters applied
let count_sql = count_filters.count_sql?;
let total_rows = db.query_one.await?;
let total_records = total_rows.;
// Create pagination options
let pagination = new;
// Create final filters with pagination
let filters = new?;
Supported Column Types
- Text - Text/VARCHAR/CHAR columns
- Integer - INTEGER columns
- BigInt - BIGINT columns
- SmallInt - SMALLINT columns
- Boolean - BOOLEAN columns
- DoublePrecision - DOUBLE PRECISION columns
- Real - REAL columns
- Date - DATE columns
- Timestamp - TIMESTAMP columns
- TimestampTz - TIMESTAMP WITH TIME ZONE columns
- Uuid - UUID columns (case-sensitive comparison)
- Json/Jsonb - JSON and JSONB columns
- And many more (see documentation for full list)
Valid Filtering Operators
The filtering supports various operators for different column types:
Filtering Operators
Can be upper or lower case:
- "="
- "!="
- ">"
- ">="
- "<"
- "<="
- "LIKE"
- "NOT LIKE"
- "IN"
- "NOT IN"
- "IS NULL"
- "IS NOT NULL"
- "STARTS WITH"
- "ENDS WITH"
Case Sensitivity
By default, text searches are case-insensitive. You can make them case-sensitive using:
let columns = setup_columns;
case_sensitive;
Type-Aware Filtering
PG Filters now handles different column types appropriately:
- Text columns use case-insensitive comparison by default (can be made case-sensitive)
- UUID columns always use case-sensitive comparison
- Numeric columns use direct comparison
- Date/Time types use appropriate format
- Boolean values are handled correctly
- JSON fields use appropriate operators
Pagination Details
The pagination information is returned in the following structure:
See the tests for more examples.
License
Licensed under either of these:
- MIT (https://opensource.org/licenses/MIT)
- Apache-2.0 (https://www.apache.org/licenses/LICENSE-2.0)