SoftwareBrothers / adminjs

AdminJS is an admin panel for apps written in node.js
https://adminjs.co
MIT License
8.06k stars 646 forks source link

[Bug]: @adminjs/relations default sorting not working in oneToManyHandler #1664

Open Pekket opened 2 months ago

Pekket commented 2 months ago

Contact Details

No response

What happened?

Im trying to sort my oneToMany table on configured sort options.

export const requestLogsResource = (prisma: PrismaClient) => ({
  resource: {
    model: getModelByName('request_logs'),
    client: prisma,
  },
  features: [targetRelationSettingsFeature()],
  options: {
    id: 'requestLogs',
    navigation: false,
    sort: {
      direction: 'desc',
      sortBy: 'timestamp',
    },
    listProperties: ['timestamp', 'question'],
... 
image

Sorting is only working when clicking on the header table of timestamp. This because query parameters are added to the url. When investigating this problem i found that oneToManyHandler (@adminjs/relations/lib/actions/one-to-many/one-to-many.handler.js) is defaulting its query parameter direction to asc.

export const oneToManyHandler = async (a, b, c = {}, d) => {
  const {
    relation: e,
    targetResource: f,
  } = b, {
    sortBy: g,
    direction: h = 'asc',
    filters: i = {},
    perPage: j = d._admin.options.settings?.defaultPerPage || 10,
    page: k = 1,
  } = c;

This causes the sortSetter (adminjs/src/backend/services/sort-setter/sort-setter) to always default back to asc in code. const computedDirection = direction || options.direction || DEFAULT_DIRECTION

🚀🚀🚀 Proposed solution: remove default of direction in oneToManyHandler function.

Bug prevalence

Visting overview page with relations

AdminJS dependencies version

"@adminjs/express": "^6.1.0",
"@adminjs/prisma": "^5.0.1",
"@adminjs/relations": "^1.1.2",
"@prisma/client": "^5.6.0",
"adminjs": "^7.8.1",

What browsers do you see the problem on?

No response

Relevant log output

No response

Relevant code that's giving you issues

No response