Skip to content

Commit b3e8ddf

Browse files
authored
fix(db-mongodb): removes precedence of regular chars over international chars in sort (#7294)
## Description V2 PR [here](#6923) - [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
1 parent b6d4bc4 commit b3e8ddf

File tree

8 files changed

+94
-7
lines changed

8 files changed

+94
-7
lines changed

packages/db-mongodb/src/find.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export const find: Find = async function find(
5454
useEstimatedCount,
5555
}
5656

57+
if (locale && locale !== 'all' && locale !== '*') {
58+
paginationOptions.collation = { locale, strength: 1 }
59+
}
60+
5761
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
5862
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding
5963
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,

packages/db-mongodb/src/findGlobalVersions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export const findGlobalVersions: FindGlobalVersions = async function findGlobalV
7272
useEstimatedCount,
7373
}
7474

75+
if (locale && locale !== 'all' && locale !== '*') {
76+
paginationOptions.collation = { locale, strength: 1 }
77+
}
78+
7579
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
7680
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding
7781
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,

packages/db-mongodb/src/findVersions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ export const findVersions: FindVersions = async function findVersions(
6969
useEstimatedCount,
7070
}
7171

72+
if (locale && locale !== 'all' && locale !== '*') {
73+
paginationOptions.collation = { locale, strength: 1 }
74+
}
75+
7276
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
7377
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding
7478
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,

packages/db-mongodb/src/queryDrafts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
5757
useEstimatedCount,
5858
}
5959

60+
if (locale && locale !== 'all' && locale !== '*') {
61+
paginationOptions.collation = { locale, strength: 1 }
62+
}
63+
6064
if (
6165
!useEstimatedCount &&
6266
Object.keys(versionQuery).length === 0 &&

test/localization/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
blocksWithLocalizedSameName,
1313
defaultLocale,
1414
englishTitle,
15+
hungarianLocale,
1516
localizedPostsSlug,
1617
localizedSortSlug,
1718
portugueseLocale,
@@ -70,6 +71,11 @@ export default buildConfigWithDefaults({
7071
name: 'description',
7172
type: 'text',
7273
},
74+
{
75+
name: 'localizedDescription',
76+
localized: true,
77+
type: 'text',
78+
},
7379
{
7480
name: 'localizedCheckbox',
7581
localized: true,
@@ -310,6 +316,11 @@ export default buildConfigWithDefaults({
310316
label: 'Arabic',
311317
rtl: true,
312318
},
319+
{
320+
code: hungarianLocale,
321+
label: 'Hungarian',
322+
rtl: false,
323+
},
313324
],
314325
},
315326
onInit: async (payload) => {

test/localization/int.spec.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import configPromise from './config.js'
1212
import {
1313
defaultLocale,
1414
englishTitle,
15+
hungarianLocale,
1516
localizedPostsSlug,
1617
localizedSortSlug,
1718
portugueseLocale,
@@ -37,15 +38,13 @@ describe('Localization', () => {
3738
beforeAll(async () => {
3839
;({ payload, restClient } = await initPayloadInt(configPromise))
3940

40-
// @ts-expect-error Force typing
4141
post1 = await payload.create({
4242
collection,
4343
data: {
4444
title: englishTitle,
4545
},
4646
})
4747

48-
// @ts-expect-error Force typing
4948
postWithLocalizedData = await payload.create({
5049
collection,
5150
data: {
@@ -185,7 +184,6 @@ describe('Localization', () => {
185184
},
186185
})
187186

188-
// @ts-expect-error Force typing
189187
localizedPost = await payload.update({
190188
id,
191189
collection,
@@ -276,6 +274,67 @@ describe('Localization', () => {
276274

277275
expect(result.docs.map(({ id }) => id)).toContain(localizedPost.id)
278276
})
277+
278+
if (['mongodb'].includes(process.env.PAYLOAD_DATABASE)) {
279+
describe('Localized sorting', () => {
280+
let localizedAccentPostOne: LocalizedPost
281+
let localizedAccentPostTwo: LocalizedPost
282+
beforeEach(async () => {
283+
localizedAccentPostOne = await payload.create({
284+
collection,
285+
data: {
286+
title: 'non accent post',
287+
localizedDescription: 'something',
288+
},
289+
locale: englishLocale,
290+
})
291+
292+
localizedAccentPostTwo = await payload.create({
293+
collection,
294+
data: {
295+
title: 'accent post',
296+
localizedDescription: 'veterinarian',
297+
},
298+
locale: englishLocale,
299+
})
300+
301+
await payload.update({
302+
id: localizedAccentPostOne.id,
303+
collection,
304+
data: {
305+
title: 'non accent post',
306+
localizedDescription: 'valami',
307+
},
308+
locale: hungarianLocale,
309+
})
310+
311+
await payload.update({
312+
id: localizedAccentPostTwo.id,
313+
collection,
314+
data: {
315+
title: 'accent post',
316+
localizedDescription: 'állatorvos',
317+
},
318+
locale: hungarianLocale,
319+
})
320+
})
321+
322+
it('should sort alphabetically even with accented letters', async () => {
323+
const sortByDescriptionQuery = await payload.find({
324+
collection,
325+
sort: 'description',
326+
where: {
327+
title: {
328+
like: 'accent',
329+
},
330+
},
331+
locale: hungarianLocale,
332+
})
333+
334+
expect(sortByDescriptionQuery.docs[0].id).toEqual(localizedAccentPostTwo.id)
335+
})
336+
})
337+
}
279338
})
280339
})
281340

@@ -352,7 +411,6 @@ describe('Localization', () => {
352411
},
353412
})
354413

355-
// @ts-expect-error Force typing
356414
withRelationship = await payload.create({
357415
collection: withLocalizedRelSlug,
358416
data: {

test/localization/payload-types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface Config {
2727
globals: {
2828
'global-array': GlobalArray;
2929
};
30-
locale: 'en' | 'es' | 'pt' | 'ar';
30+
locale: 'en' | 'es' | 'hu' | 'pt' | 'ar';
3131
user: User & {
3232
collection: 'users';
3333
};
@@ -70,6 +70,7 @@ export interface User {
7070
export interface LocalizedPost {
7171
id: string;
7272
title?: string | null;
73+
localizedDescription?: string | null;
7374
description?: string | null;
7475
localizedCheckbox?: boolean | null;
7576
children?: (string | LocalizedPost)[] | null;
@@ -316,6 +317,6 @@ export interface Auth {
316317

317318

318319
declare module 'payload' {
319-
// @ts-ignore
320+
// @ts-ignore
320321
export interface GeneratedTypes extends Config {}
321-
}
322+
}

test/localization/shared.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const relationSpanishTitle2 = `${relationSpanishTitle}2`
88
export const defaultLocale = 'en'
99
export const spanishLocale = 'es'
1010
export const portugueseLocale = 'pt'
11+
export const hungarianLocale = 'hu'
1112

1213
// Slugs
1314
export const localizedPostsSlug = 'localized-posts'

0 commit comments

Comments
 (0)