nestjs-query icon indicating copy to clipboard operation
nestjs-query copied to clipboard

Support fallback column for sorting when the value is NULL

Open rostaingc opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe.

I have a list of tasks that is sorted and paginated. Each task can have a plannedStartDate and an actualStartDate.

I show the tasks in a table with a column 'Start date'

  • if the task has an actualStartDate it displays the actualStartDate
  • if actualStartDate is null, it displays the plannedStartDate

I would like to sort my list with nestjs-query on the 'Start Date' equivalent to this SQL:

ORDER BY CASE 
    WHEN actualStartDate IS NOT NULL THEN actualStartDate
    ELSE plannedStartDate
END

If I put the columns actualStartDate and plannedStartDate in the SortField list, it will sort the tasks by actualStartDate first and then by plannedStartDate, but it will not 'mix' the two and sort the tasks on this combined column.

Have you read the Contributing Guidelines?

Yes

Describe alternatives you've considered For the moment the only solution I see it to use a custom SQL query instead of nestjs-query.

rostaingc avatar Jan 03 '23 10:01 rostaingc

How would you propose to fit this inside the Query<T>?

TriPSs avatar Jan 04 '23 14:01 TriPSs

What about something like

sorting = [
  {
    field: 'actualStartDate'
    fieldFallback: {
      field: 'plannedStartDate'
    }
]

The typing could be:

export interface FallbackSortField<T> {
  field: keyof T;
  fieldFallback: FallbackSortField<T>;
}

export interface SortField<T> extends FallbackSortField<T> {
  direction: SortDirection;
  nulls?: SortNulls;
}

What do you think?

ValentinVignal avatar Oct 19 '23 06:10 ValentinVignal

Interesting @ValentinVignal, could you make a PR?

TriPSs avatar May 04 '24 15:05 TriPSs

I can give it a try :)

ValentinVignal avatar May 05 '24 04:05 ValentinVignal