nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.08k stars 538 forks source link

How to use RequestQueryParser as typeorm query parameters in the controller #721

Open gino8080 opened 3 years ago

gino8080 commented 3 years ago

Hello ,

I would like to understand how to use RequestQueryParser in the controller to make the typeorm query

this is my pseudo code


@Crud({
  model: {
    type: Examination,
  },
  query: {
    join: {
      exams: {
        eager: true,
      },
      user: {
        eager: true,
      },
      reports: {
        eager: true,
      },
      "reports.exam": {
        eager: true,
      },
    },
  },
})
@Controller('api/examinations')
@ApiTags('examinations')
export class ExaminationsController implements CrudController<Examination> {
  constructor(public service: ExaminationsService) { }

  get base(): CrudController<Examination> {
    return this;
  }

  //this is a custom controller
  @UseGuards(JwtAuthGuard)
  @Post('userExams')
  async userExams(
    @Request() req, //need this to get the user from jwt
    @Body() body: any //sending a RequestQueryBuilder from client
  ) {
    const parser = RequestQueryParser.create();
    const parsed = parser.parseQuery(body)
   /*
  parsed is womething like
  parsed = {
    fields: [],
    paramsFilter: [],
    filter: [
    { field: 'date', operator: '$gte', value: '2021-07-01 00:00' },
    { field: 'date', operator: '$lte', value: '2021-07-22 23:59' }
    ],
    ...
  }
  how to use it in my find method?
should I convert / parse before sending to find method 
 */

 const data = await this.service.find({  parsed ?!?!?   })
gino8080 commented 3 years ago

any help please?