Skip to content

Commit fd67d46

Browse files
authored
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.
1 parent 8219c04 commit fd67d46

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

packages/db-mongodb/src/queries/buildSortParam.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,8 @@ const relationshipSort = ({
5757
return false
5858
}
5959

60-
for (const [i, segment] of segments.entries()) {
61-
if (versions && i === 0 && segment === 'version') {
62-
segments.shift()
63-
continue
64-
}
65-
60+
for (let i = 0; i < segments.length; i++) {
61+
const segment = segments[i]
6662
const field = currentFields.find((each) => each.name === segment)
6763

6864
if (!field) {
@@ -71,6 +67,10 @@ const relationshipSort = ({
7167

7268
if ('fields' in field) {
7369
currentFields = field.flattenedFields
70+
if (field.name === 'version' && versions && i === 0) {
71+
segments.shift()
72+
i--
73+
}
7474
} else if (
7575
(field.type === 'relationship' || field.type === 'upload') &&
7676
i !== segments.length - 1
@@ -106,7 +106,7 @@ const relationshipSort = ({
106106
as: `__${path}`,
107107
foreignField: '_id',
108108
from: foreignCollection.Model.collection.name,
109-
localField: relationshipPath,
109+
localField: versions ? `version.${relationshipPath}` : relationshipPath,
110110
pipeline: [
111111
{
112112
$project: {

packages/db-mongodb/src/queryDrafts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
151151
query: versionQuery,
152152
session: paginationOptions.options?.session ?? undefined,
153153
sort: paginationOptions.sort as object,
154+
sortAggregation,
154155
useEstimatedCount: paginationOptions.useEstimatedCount,
155156
})
156157
} else {

test/relationships/int.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -670,28 +670,28 @@ describe('Relationships', () => {
670670
await payload.delete({ collection: 'directors', where: {} })
671671
await payload.delete({ collection: 'movies', where: {} })
672672

673-
const director_1 = await payload.create({
673+
const director_2 = await payload.create({
674674
collection: 'directors',
675-
data: { name: 'Dan', localized: 'Dan' },
675+
data: { name: 'Mr. Dan', localized: 'Mr. Dan' },
676676
})
677677

678678
await payload.update({
679679
collection: 'directors',
680-
id: director_1.id,
680+
id: director_2.id,
681681
locale: 'de',
682-
data: { localized: 'Mr. Dan' },
682+
data: { localized: 'Dan' },
683683
})
684684

685-
const director_2 = await payload.create({
685+
const director_1 = await payload.create({
686686
collection: 'directors',
687-
data: { name: 'Mr. Dan', localized: 'Mr. Dan' },
687+
data: { name: 'Dan', localized: 'Dan' },
688688
})
689689

690690
await payload.update({
691691
collection: 'directors',
692-
id: director_2.id,
692+
id: director_1.id,
693693
locale: 'de',
694-
data: { localized: 'Dan' },
694+
data: { localized: 'Mr. Dan' },
695695
})
696696

697697
const movie_1 = await payload.create({

0 commit comments

Comments
 (0)