RequestQueryBuilder class provides a fluent, chainable API for building complex queries from your frontend application. It generates properly formatted query strings that the NestJS CRUD backend can parse and execute.
Installation
The RequestQueryBuilder is part of the@nestjsx/crud-request package:
Basic Usage
1
Import the builder
2
Create a new instance
3
Chain methods and generate query string
Creating a Query Builder
Default Constructor
Create a new instance with default configuration:From Parameters Object
Create and configure in one step using a parameters object:The
create() method with parameters internally calls select(), setFilter(), sortBy(), and other methods based on the provided configuration.Selecting Fields
Control which fields are returned in the response:Filtering Data
Single Filter
Apply a single filter condition:Multiple Filters (AND)
Multiple filters are combined with AND logic:Array Notation
Use array notation for more concise syntax:OR Filters
UsesetOr() for OR logic:
Combining AND and OR
Comparison Operators
Advanced Search
Use thesearch() method for complex nested conditions:
When using
search(), any filters set with setFilter() or setOr() will be ignored. The search parameter takes precedence.Joining Relations
Basic Join
Load related entities:Join with Selected Fields
Control which fields are loaded from the relation:Multiple Joins
Array Notation for Joins
Sorting Results
Single Sort
Multiple Sorts
Array Notation for Sorting
Pagination
Limit and Offset
Page-based Pagination
Cache Control
Reset Cache
Force a fresh query bypassing any server-side cache:Soft Deletes
Include soft-deleted records in results:Complete Example
Here’s a comprehensive example combining multiple features:Using with HTTP Clients
Configuration
Global Options
Configure the builder globally for your application:Custom Parameter Names
If your API uses different parameter names:Accessing Query Object
Access the raw query object before converting to string:Query String Generation
Thequery() method generates the final URL query string:
The generated query string is also stored in the
queryString property: qb.queryStringTypeScript Types
The builder is fully typed for TypeScript users:Best Practices
- Reusable Query Builders: Create factory functions for common queries
- Type-Safe Filters: Use TypeScript enums for operators
- Dynamic Queries: Build queries based on user input
Next Steps
Query String Format
Learn about the query string format and parameters
Controllers
Set up CRUD controllers to handle these queries