Skip to content

Commit

Permalink
fix(db-mongodb): removes precedence of regular chars over internation…
Browse files Browse the repository at this point in the history
…al chars in sort (#6923)

## Description

Fixes #6719 

- [x] I have read and understand the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## Checklist:

- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] Existing test suite passes locally with my changes
  • Loading branch information
PatrikKozak authored Jul 22, 2024
1 parent 6d7ef91 commit 0058660
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/db-mongodb/src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export const find: Find = async function find(
useEstimatedCount,
}

if (locale && locale !== 'all' && locale !== '*') {
paginationOptions.collation = { locale, strength: 1 }
}

if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,
Expand Down
4 changes: 4 additions & 0 deletions packages/db-mongodb/src/findGlobalVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export const findGlobalVersions: FindGlobalVersions = async function findGlobalV
useEstimatedCount,
}

if (locale && locale !== 'all' && locale !== '*') {
paginationOptions.collation = { locale, strength: 1 }
}

if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,
Expand Down
4 changes: 4 additions & 0 deletions packages/db-mongodb/src/findVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export const findVersions: FindVersions = async function findVersions(
useEstimatedCount,
}

if (locale && locale !== 'all' && locale !== '*') {
paginationOptions.collation = { locale, strength: 1 }
}

if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,
Expand Down
4 changes: 4 additions & 0 deletions packages/db-mongodb/src/queryDrafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
useEstimatedCount,
}

if (locale && locale !== 'all' && locale !== '*') {
paginationOptions.collation = { locale, strength: 1 }
}

if (
!useEstimatedCount &&
Object.keys(versionQuery).length === 0 &&
Expand Down
12 changes: 12 additions & 0 deletions test/localization/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
blocksWithLocalizedSameName,
defaultLocale,
englishTitle,
hungarianLocale,
localizedPostsSlug,
localizedSortSlug,
portugueseLocale,
Expand Down Expand Up @@ -80,6 +81,11 @@ export default buildConfigWithDefaults({
name: 'description',
type: 'text',
},
{
name: 'localizedDescription',
localized: true,
type: 'text',
},
{
name: 'localizedCheckbox',
localized: true,
Expand Down Expand Up @@ -321,6 +327,11 @@ export default buildConfigWithDefaults({
label: 'Arabic',
rtl: true,
},
{
code: hungarianLocale,
label: 'Hungarian',
rtl: false,
},
],
},
onInit: async (payload) => {
Expand Down Expand Up @@ -381,6 +392,7 @@ export default buildConfigWithDefaults({
title: relationEnglishTitle2,
},
})

await payload.update({
id: localizedPost.id,
collection,
Expand Down
65 changes: 64 additions & 1 deletion test/localization/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { RESTClient } from '../helpers/rest'
import { arrayCollectionSlug } from './collections/Array'
import { nestedToArrayAndBlockCollectionSlug } from './collections/NestedToArrayAndBlock'
import configPromise from './config'
import { defaultLocale, localizedSortSlug } from './shared'
import { defaultLocale, hungarianLocale, localizedSortSlug } from './shared'
import {
englishTitle,
localizedPostsSlug,
Expand Down Expand Up @@ -292,6 +292,69 @@ describe('Localization', () => {
expect(result.docs.map(({ id }) => id)).toContain(localizedPost.id)
})
})

if (['mongoose'].includes(process.env.PAYLOAD_DATABASE)) {
describe('Localized sorting', () => {
let localizedAccentPostOne: LocalizedPost
let localizedAccentPostTwo: LocalizedPost
beforeEach(async () => {
// @ts-expect-error Force typing
localizedAccentPostOne = await payload.create({
collection,
data: {
title: 'non accent post',
localizedDescription: 'something',
},
locale: englishLocale,
})

// @ts-expect-error Force typing
localizedAccentPostTwo = await payload.create({
collection,
data: {
title: 'accent post',
localizedDescription: 'veterinarian',
},
locale: englishLocale,
})

await payload.update({
id: localizedAccentPostOne.id,
collection,
data: {
title: 'non accent post',
localizedDescription: 'valami',
},
locale: hungarianLocale,
})

await payload.update({
id: localizedAccentPostTwo.id,
collection,
data: {
title: 'accent post',
localizedDescription: 'állatorvos',
},
locale: hungarianLocale,
})
})

it('should sort alphabetically even with accented letters', async () => {
const sortByDescriptionQuery = await payload.find({
collection,
sort: 'description',
where: {
title: {
like: 'accent',
},
},
locale: hungarianLocale,
})

expect(sortByDescriptionQuery.docs[0].id).toEqual(localizedAccentPostTwo.id)
})
})
}
})

describe('Localized Sort Count', () => {
Expand Down
1 change: 1 addition & 0 deletions test/localization/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const relationSpanishTitle2 = `${relationSpanishTitle}2`
export const defaultLocale = 'en'
export const spanishLocale = 'es'
export const portugueseLocale = 'pt'
export const hungarianLocale = 'hu'

// Slugs
export const localizedPostsSlug = 'localized-posts'
Expand Down

0 comments on commit 0058660

Please sign in to comment.