nestjsx / crud

NestJs CRUD for RESTful APIs

Home Page:https://github.com/nestjsx/crud/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Default relationship sorting overwritten by getMany query sorting

AmauryLiet opened this issue · comments

Hello,

TLDR: When any sort is given to getMany, relationship sorting specified in controller is ignored

Likely culprit: getSort in https://github.com/nestjsx/crud/blob/v5.0.1/packages/crud-typeorm/src/typeorm-crud.service.ts#L864-L870

  protected getSort(query: ParsedRequestParams, options: QueryOptions) {
    return query.sort && query.sort.length
      ? this.mapSort(query.sort) // is there really nothing we can get from options.sort in that case?
      : options.sort && options.sort.length
      ? this.mapSort(options.sort)
      : {};
  }

Thanks for the hard work on this highly useful lib!

I am facing an unexpected result when working with relationship sorting. Default sorting gets overridden as soon as any sorting is specified by the getMany request.

When I specify in MyController that myBaseEntity.myBasesortedRelationship should be sorted by their column order :

@Crud({
  model: {
    type: MyBaseEntity,
  },
  query: {
    join: {
      sortedRelationship: { eager: true },
    },
    sort: [
      {
        field: 'sortedRelationship.order',
        order: 'ASC',
      },
    ],
  },
})
class MyController {

I get expected sorted result out of the box (for both getMany and getOne).

However, when getMany is called with any sorting (such as something unrelated to the relationship : sort: [{ field: 'myBaseEntity.date' }]), the linked relationship stop being sorted at all.

For information, there is a related typeorm issue that makes fixing the issue trickier (at least trickier than getSort = mapSort([...query.sort, ...options.sort])): typeorm/typeorm#6294

Thanks for the reading