Getting StartedFirst endpoint
First endpoint
The shortest path to getting nestjs-rest-query working.
If you want to validate the library quickly, this is the shortest path:
- install the package
- configure the NestJS prerequisites
- decorate an endpoint with
@ApiDynamicQuery - inject
@QueryRules() - call
queryBuilderService.execute()
Minimal example
import { Controller, Get, Query } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
ApiDynamicQuery,
DynamicQueryDto,
QueryRules,
RulesConfig,
} from 'nestjs-rest-query';
import { UsersService } from './users.service';
@ApiTags('users')
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Get()
@ApiDynamicQuery({
filters: ['name', 'email', 'status'],
sorts: ['name', 'createdAt'],
fields: ['id', 'name', 'email', 'status', 'createdAt'],
})
findAll(@Query() query: DynamicQueryDto, @QueryRules() rules: RulesConfig) {
return this.usersService.findAll(query, rules);
}
}import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import {
DynamicQueryDto,
QueryBuilderService,
QueryResult,
RulesConfig,
} from 'nestjs-rest-query';
import { User } from './user.entity';
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private readonly usersRepo: Repository<User>,
private readonly queryBuilderService: QueryBuilderService
) {}
findAll(
query: DynamicQueryDto,
rules: RulesConfig
): Promise<QueryResult<User>> {
return this.queryBuilderService.execute(this.usersRepo, query, rules);
}
}Usage example
GET /users?filter[name][like]=ana&page=1&perPage=10Expected response:
{
"data": [
{
"id": 1,
"name": "Ana Lima",
"email": "ana@email.com",
"status": "active"
}
],
"page": 1,
"perPage": 10,
"total": 1,
"lastPage": 1
}Request lifecycle
Rendering Mermaid diagram...
If it worked, continue to the Usage Guide to see all parameters, operators, and whitelist options.