diff --git a/packages/db-mongodb/src/queries/buildSortParam.ts b/packages/db-mongodb/src/queries/buildSortParam.ts index 2503e659f73..6868df87649 100644 --- a/packages/db-mongodb/src/queries/buildSortParam.ts +++ b/packages/db-mongodb/src/queries/buildSortParam.ts @@ -8,7 +8,7 @@ type Args = { config: SanitizedConfig fields: Field[] locale: string - sort: string + sort: string | undefined timestamps: boolean } @@ -28,6 +28,7 @@ export const buildSortParam = ({ }: Args): PaginateOptions['sort'] => { let sortProperty: string let sortDirection: SortDirection = 'desc' + const isSortMultipleField = sort.includes(',') if (!sort) { if (timestamps) { @@ -35,6 +36,24 @@ export const buildSortParam = ({ } else { sortProperty = '_id' } + } else if (isSortMultipleField) { + const sortFields = sort.split(',') + return sortFields.reduce((acc, sortField) => { + const isDesc = sortField.indexOf('-') === 0 + let currentSortProperty = sortField.replace(/^-/, '') + + if (currentSortProperty === 'id') { + currentSortProperty = '_id' + } else { + currentSortProperty = getLocalizedSortProperty({ + config, + fields, + locale, + segments: currentSortProperty.split('.'), + }) + } + return [...acc, `${isDesc ? '-' : ''}${currentSortProperty}`] + }, []) } else if (sort.indexOf('-') === 0) { sortProperty = sort.substring(1) } else { diff --git a/packages/db-postgres/src/find/findMany.ts b/packages/db-postgres/src/find/findMany.ts index 41ce010ff8d..29c796e01f9 100644 --- a/packages/db-postgres/src/find/findMany.ts +++ b/packages/db-postgres/src/find/findMany.ts @@ -55,9 +55,9 @@ export const findMany = async function find({ const selectDistinctMethods: ChainedMethods = [] - if (orderBy?.order && orderBy?.column) { + if (orderBy.length) { selectDistinctMethods.push({ - args: [orderBy.order(orderBy.column)], + args: orderBy.map(({ column, order }) => order(column)), method: 'orderBy', }) } @@ -115,7 +115,7 @@ export const findMany = async function find({ if (where) { findManyArgs.where = where } - findManyArgs.orderBy = orderBy.order(orderBy.column) + findManyArgs.orderBy = orderBy.map(({ column, order }) => order(column)) } const findPromise = db.query[tableName].findMany(findManyArgs) diff --git a/packages/db-postgres/src/queries/buildQuery.ts b/packages/db-postgres/src/queries/buildQuery.ts index 4cc21a50b1c..a6120a3b3ff 100644 --- a/packages/db-postgres/src/queries/buildQuery.ts +++ b/packages/db-postgres/src/queries/buildQuery.ts @@ -31,7 +31,7 @@ type Result = { orderBy: { column: GenericColumn order: typeof asc | typeof desc - } + }[] selectFields: Record where: SQL } @@ -49,54 +49,56 @@ const buildQuery = async function buildQuery({ const joins: BuildQueryJoins = {} const joinAliases: BuildQueryJoinAliases = [] - const orderBy: Result['orderBy'] = { - column: null, - order: null, - } + let orderBy: Result['orderBy'] = [] if (sort) { - let sortPath - - if (sort[0] === '-') { - sortPath = sort.substring(1) - orderBy.order = desc - } else { - sortPath = sort - orderBy.order = asc - } - - try { - const { columnName: sortTableColumnName, table: sortTable } = getTableColumnFromPath({ - adapter, - collectionPath: sortPath, - fields, - joinAliases, - joins, - locale, - pathSegments: sortPath.replace(/__/g, '.').split('.'), - selectFields, - tableName, - value: sortPath, + orderBy = sort + .split(',') + .map((sortString) => { + const sortPath = sortString.replace(/^-/, '') + try { + const { columnName: sortTableColumnName, table: sortTable } = getTableColumnFromPath({ + adapter, + collectionPath: sortPath, + fields, + joinAliases, + joins, + locale, + pathSegments: sortPath.replace(/__/g, '.').split('.'), + selectFields, + tableName, + value: sortPath, + }) + return { + column: sortTable?.[sortTableColumnName] ?? null, + order: sortString[0] === '-' ? desc : asc, + } + } catch (err) { + // continue + } }) - orderBy.column = sortTable?.[sortTableColumnName] - } catch (err) { - // continue - } + .filter((sortInfo) => !!sortInfo) } - if (!orderBy?.column) { - orderBy.order = desc + if (!orderBy.length) { + const firstOrderBy = { + column: null, + order: null, + } + firstOrderBy.order = desc const createdAt = adapter.tables[tableName]?.createdAt if (createdAt) { - orderBy.column = createdAt + firstOrderBy.column = createdAt } else { - orderBy.column = adapter.tables[tableName].id + firstOrderBy.column = adapter.tables[tableName].id } + + orderBy = [firstOrderBy] } - if (orderBy.column) { - selectFields.sort = orderBy.column + if (orderBy.length) { + selectFields.sort = orderBy[0].column } let where: SQL diff --git a/packages/payload/src/versions/drafts/getQueryDraftsSort.ts b/packages/payload/src/versions/drafts/getQueryDraftsSort.ts index 3da4ec9350e..1a7dbd19e83 100644 --- a/packages/payload/src/versions/drafts/getQueryDraftsSort.ts +++ b/packages/payload/src/versions/drafts/getQueryDraftsSort.ts @@ -5,13 +5,16 @@ export const getQueryDraftsSort = (sort: string): string => { if (!sort) return sort - let direction = '' - let orderBy = sort - - if (sort[0] === '-') { - direction = '-' - orderBy = sort.substring(1) - } - - return `${direction}version.${orderBy}` + return sort + .split(',') + .map((sortString) => { + let direction = '' + let orderBy = sortString + if (sortString[0] === '-') { + direction = '-' + orderBy = sortString.substring(1) + } + return `${direction}version.${orderBy}` + }) + .join(',') }