Filtering System
Relevant source files
The following files were used as context for generating this wiki page:
- packages/query-graphql/tests/types/query/filter.type.spec.ts
- packages/core/tests/helpers.spec.ts
- packages/query-typeorm/tests/query/filter-query.builder.spec.ts
- packages/query-sequelize/tests/query/filter-query.builder.spec.ts
- packages/query-mongoose/src/query/filter-query.builder.ts
- packages/query-typeorm/tests/query/where.builder.spec.ts
The Filtering System provides a unified way to define, transform, and apply complex search criteria across different persistence layers and API protocols. It allows developers to create type-safe filters that support logical groupings, field-level comparisons, and deep relational queries.
Sources: packages/core/tests/helpers.spec.ts, packages/query-graphql/tests/types/query/filter.type.spec.ts
Core Architecture
The system operates by converting high-level Filter<DTO> objects into database-specific query structures. This process involves decorators for GraphQL schema generation and specialized builder classes for SQL and NoSQL backends.
The diagram shows the transformation flow from an incoming request to database-specific filter implementations.
Sources: packages/query-mongoose/src/query/filter-query.builder.ts:35-42, packages/query-typeorm/tests/query/filter-query.builder.spec.ts:161-170
Filter Components and Logic
Logical Groupings
Filters support complex boolean logic through and and or arrays. These groupings can be nested infinitely to build deep query structures.
- AND: Requires all conditions within the array to be true.
- OR: Requires at least one condition within the array to be true.
Sources: packages/core/tests/helpers.spec.ts:255-273, packages/query-typeorm/tests/query/where.builder.spec.ts:41-86
Comparison Operators
The system supports a standard set of operators for different data types:
| Operator | Description | Supported Types |
|---|---|---|
eq / neq | Equal / Not Equal | All |
gt / gte | Greater Than / or Equal | Numeric, Date, String |
lt / lte | Less Than / or Equal | Numeric, Date, String |
in / notIn | Value in List | All |
like / notLike | Case-sensitive Pattern Match | String |
iLike / notILike | Case-insensitive Pattern Match | String |
is / isNot | Null Check | All |
between | Range Check | Numeric, Date |
Sources: packages/core/tests/helpers.spec.ts:133-248, packages/query-graphql/tests/types/query/filter.type.spec.ts:153-205
GraphQL Integration
The @FilterableField decorator marks DTO properties as queryable. The FilterType factory function generates the corresponding GraphQL input types.
Configuration Options
You can customize filter generation using the following options:
- allowedComparisons: Restricts the operators available for a specific field.
- filterRequired: Makes the filter argument mandatory in the GraphQL schema.
- filterDepth: Controls how deep the filter can traverse relations (default is often 1, but can be set to
Number.POSITIVE_INFINITY).
Sources: packages/query-graphql/tests/types/query/filter.type.spec.ts:153-176, packages/query-graphql/tests/types/query/filter.type.spec.ts:327-360
Class relationship between decorators and the DTOs that define the filtering schema.
Sources: packages/query-graphql/tests/types/query/filter.type.spec.ts:45-120
Persistence Implementation
TypeORM and Sequelize
For SQL backends, the WhereBuilder recursively iterates through the filter object. It handles table aliases and joins for relational filtering. When filtering on "many" relations (One-to-Many or Many-to-Many), the system utilizes skip/take instead of limit/offset to ensure correct pagination results.
Sources: packages/query-typeorm/tests/query/filter-query.builder.spec.ts:208-280, packages/query-sequelize/tests/query/filter-query.builder.spec.ts:25-45
Mongoose
In Mongoose, the FilterQueryBuilder converts the filter into a QueryFilter<Entity>. It maps DTO fields to schema keys and translates operators into MongoDB syntax (e.g., eq becomes $eq).
Sources: packages/query-mongoose/src/query/filter-query.builder.ts:44-50, packages/query-mongoose/src/query/filter-query.builder.ts:74-80
Internal Utilities
The core package provides helpers for manual filter manipulation:
- applyFilter: Evaluates a filter object against an in-memory array of DTOs.
- transformFilter: Maps DTO field names to Entity field names based on a provided
QueryFieldMap. - mergeFilters: Combines multiple filter objects into a single
andgrouping.
Sources: packages/core/tests/helpers.spec.ts:133-140, packages/core/tests/helpers.spec.ts:60-75, packages/core/tests/helpers.spec.ts:696-708
Summary
The Filtering System provides a robust, type-safe abstraction for complex data retrieval. By decoupling filter definitions from database implementations, it enables consistent querying capabilities across GraphQL and REST interfaces while automatically handling the complexities of relational joins and database-specific syntax.