fix(db-mongodb): sort by fields in relationships with draft: true (#12387)
Fixes sorting by fields in relationships, e.g `sort: "author.name"` when using `draft: true`. The existing test that includes check with `draft: true` was accidentally passing because it used to sort by the relationship field itself.
This commit is contained in:
@@ -57,12 +57,8 @@ const relationshipSort = ({
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [i, segment] of segments.entries()) {
|
for (let i = 0; i < segments.length; i++) {
|
||||||
if (versions && i === 0 && segment === 'version') {
|
const segment = segments[i]
|
||||||
segments.shift()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
const field = currentFields.find((each) => each.name === segment)
|
const field = currentFields.find((each) => each.name === segment)
|
||||||
|
|
||||||
if (!field) {
|
if (!field) {
|
||||||
@@ -71,6 +67,10 @@ const relationshipSort = ({
|
|||||||
|
|
||||||
if ('fields' in field) {
|
if ('fields' in field) {
|
||||||
currentFields = field.flattenedFields
|
currentFields = field.flattenedFields
|
||||||
|
if (field.name === 'version' && versions && i === 0) {
|
||||||
|
segments.shift()
|
||||||
|
i--
|
||||||
|
}
|
||||||
} else if (
|
} else if (
|
||||||
(field.type === 'relationship' || field.type === 'upload') &&
|
(field.type === 'relationship' || field.type === 'upload') &&
|
||||||
i !== segments.length - 1
|
i !== segments.length - 1
|
||||||
@@ -106,7 +106,7 @@ const relationshipSort = ({
|
|||||||
as: `__${path}`,
|
as: `__${path}`,
|
||||||
foreignField: '_id',
|
foreignField: '_id',
|
||||||
from: foreignCollection.Model.collection.name,
|
from: foreignCollection.Model.collection.name,
|
||||||
localField: relationshipPath,
|
localField: versions ? `version.${relationshipPath}` : relationshipPath,
|
||||||
pipeline: [
|
pipeline: [
|
||||||
{
|
{
|
||||||
$project: {
|
$project: {
|
||||||
|
|||||||
@@ -151,6 +151,7 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
|
|||||||
query: versionQuery,
|
query: versionQuery,
|
||||||
session: paginationOptions.options?.session ?? undefined,
|
session: paginationOptions.options?.session ?? undefined,
|
||||||
sort: paginationOptions.sort as object,
|
sort: paginationOptions.sort as object,
|
||||||
|
sortAggregation,
|
||||||
useEstimatedCount: paginationOptions.useEstimatedCount,
|
useEstimatedCount: paginationOptions.useEstimatedCount,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -670,18 +670,6 @@ describe('Relationships', () => {
|
|||||||
await payload.delete({ collection: 'directors', where: {} })
|
await payload.delete({ collection: 'directors', where: {} })
|
||||||
await payload.delete({ collection: 'movies', where: {} })
|
await payload.delete({ collection: 'movies', where: {} })
|
||||||
|
|
||||||
const director_1 = await payload.create({
|
|
||||||
collection: 'directors',
|
|
||||||
data: { name: 'Dan', localized: 'Dan' },
|
|
||||||
})
|
|
||||||
|
|
||||||
await payload.update({
|
|
||||||
collection: 'directors',
|
|
||||||
id: director_1.id,
|
|
||||||
locale: 'de',
|
|
||||||
data: { localized: 'Mr. Dan' },
|
|
||||||
})
|
|
||||||
|
|
||||||
const director_2 = await payload.create({
|
const director_2 = await payload.create({
|
||||||
collection: 'directors',
|
collection: 'directors',
|
||||||
data: { name: 'Mr. Dan', localized: 'Mr. Dan' },
|
data: { name: 'Mr. Dan', localized: 'Mr. Dan' },
|
||||||
@@ -694,6 +682,18 @@ describe('Relationships', () => {
|
|||||||
data: { localized: 'Dan' },
|
data: { localized: 'Dan' },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const director_1 = await payload.create({
|
||||||
|
collection: 'directors',
|
||||||
|
data: { name: 'Dan', localized: 'Dan' },
|
||||||
|
})
|
||||||
|
|
||||||
|
await payload.update({
|
||||||
|
collection: 'directors',
|
||||||
|
id: director_1.id,
|
||||||
|
locale: 'de',
|
||||||
|
data: { localized: 'Mr. Dan' },
|
||||||
|
})
|
||||||
|
|
||||||
const movie_1 = await payload.create({
|
const movie_1 = await payload.create({
|
||||||
collection: 'movies',
|
collection: 'movies',
|
||||||
depth: 0,
|
depth: 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user