简体   繁体   中英

How to get nested URL query in Nest.js

I have NestJS application I have to get the query from the URL. Problem is that my creationDate is an object and I can't get it as the nested object via @Query. Here is example of the route:

xxx/yyy/orders?key1=value&key2=value&key3=value&createdAt[lte]=2021-07-01&createdAt[gte]=2011-03-30&orderBy=desc&limit=500

I am trying to get createdAt[lte] and createdAt[gte] as the nested object

export interface QueryI {
key1: string;
key2: string;
key3: string;
key4: string;
createdAt: {
    lte: string,
    gte: string,
}
orderBy: 'desc' | 'asc';
limit: number;
}

Here is my controller:

@Get('route')
getAll(
    @Query() query: QueryI): Promise<void> { 
    return this.myService.findAll(query);
}

but this gives me following result

{

key1: 'value',        
  key2: 'value',
  key3: 'value',       
  key4: 'value',
  'createdAt[lte]': 'some date',
  'createdAt[gte]': 'some date',
  orderBy: 'desc',
  limit: '500'

}

I have tried JSON.stringify and consulted simillar questions on SOW but no luck. Thank you

Switch QueryI from an interface to a class.

Working on NestJS 7.

Url used for testing

http://localhost:3000/api/v1/store/search?nestedObj[key1]=yolo&nestedObj[key2]=sup%20bruh&el=test

DTO Used

export class TestDto {
  el: string;
  nestedObj: {
    key1: string;
    key2: string;
  };
}

Controller

@Controller('v1/store')
@UsePipes(new ValidationPipe({ exceptionFactory: getFormattedErrors }))
@UseFilters(DomainExceptionFilter)
export class TestController{
@Get('search')
  public async searchStore(@Query() testDto: TestDto) {
    console.log(testDto);
    return 1;
  }
}
// Console log - { nestedObj: { key1: 'yolo', key2: 'sup bruh' }, el: 'test' }

Well I have found a partial solution and I will post it here untill better answer comes along: I am extracting the nested properties into seperate queries and creating my model. It's not perfect but it's doing the job for now...

controller.ts

 getAllOrders(
    @Query() query,
    @Query('createdAt[lte]') lte: string, 
    @Query('createdAt[gte]') gte: string): Promise<void> { 
    const mappedObject = new QueryParameters(query, lte, gte)
    return this.orderService.findAll(erpCode, mappedObject);
}

createdAtModel.ts

    export class CreatedAt {
    lte: string;
    gte: string;

    constructor(lte: string, gte: string){
        this.lte = lte;
        this.gte = gte;
    };
}

my-model.ts

import { CreatedAt } from "./created-at";

export class QueryParameters {
    //other keys
    createdAt: CreatedAt;

    constructor(query, lte, gte){
        //other keys
        this.createdAt = new CreatedAt(lte, gte)
    }
}

I am sure that better answer lies in using the NestJS validation pipes but I didn't manage to find the one that works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM