fix(db-mongodb): removes precedence of regular chars over international chars in sort (#7294)
## Description V2 PR [here](https://github.com/payloadcms/payload/pull/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
This commit is contained in:
@@ -54,6 +54,10 @@ export const find: Find = async function find(
|
|||||||
useEstimatedCount,
|
useEstimatedCount,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (locale && locale !== 'all' && locale !== '*') {
|
||||||
|
paginationOptions.collation = { locale, strength: 1 }
|
||||||
|
}
|
||||||
|
|
||||||
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
|
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
|
// 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,
|
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,
|
||||||
|
|||||||
@@ -72,6 +72,10 @@ export const findGlobalVersions: FindGlobalVersions = async function findGlobalV
|
|||||||
useEstimatedCount,
|
useEstimatedCount,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (locale && locale !== 'all' && locale !== '*') {
|
||||||
|
paginationOptions.collation = { locale, strength: 1 }
|
||||||
|
}
|
||||||
|
|
||||||
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
|
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
|
// 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,
|
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,
|
||||||
|
|||||||
@@ -69,6 +69,10 @@ export const findVersions: FindVersions = async function findVersions(
|
|||||||
useEstimatedCount,
|
useEstimatedCount,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (locale && locale !== 'all' && locale !== '*') {
|
||||||
|
paginationOptions.collation = { locale, strength: 1 }
|
||||||
|
}
|
||||||
|
|
||||||
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
|
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
|
// 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,
|
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,
|
||||||
|
|||||||
@@ -57,6 +57,10 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
|
|||||||
useEstimatedCount,
|
useEstimatedCount,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (locale && locale !== 'all' && locale !== '*') {
|
||||||
|
paginationOptions.collation = { locale, strength: 1 }
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!useEstimatedCount &&
|
!useEstimatedCount &&
|
||||||
Object.keys(versionQuery).length === 0 &&
|
Object.keys(versionQuery).length === 0 &&
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {
|
|||||||
blocksWithLocalizedSameName,
|
blocksWithLocalizedSameName,
|
||||||
defaultLocale,
|
defaultLocale,
|
||||||
englishTitle,
|
englishTitle,
|
||||||
|
hungarianLocale,
|
||||||
localizedPostsSlug,
|
localizedPostsSlug,
|
||||||
localizedSortSlug,
|
localizedSortSlug,
|
||||||
portugueseLocale,
|
portugueseLocale,
|
||||||
@@ -70,6 +71,11 @@ export default buildConfigWithDefaults({
|
|||||||
name: 'description',
|
name: 'description',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'localizedDescription',
|
||||||
|
localized: true,
|
||||||
|
type: 'text',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'localizedCheckbox',
|
name: 'localizedCheckbox',
|
||||||
localized: true,
|
localized: true,
|
||||||
@@ -310,6 +316,11 @@ export default buildConfigWithDefaults({
|
|||||||
label: 'Arabic',
|
label: 'Arabic',
|
||||||
rtl: true,
|
rtl: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
code: hungarianLocale,
|
||||||
|
label: 'Hungarian',
|
||||||
|
rtl: false,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
onInit: async (payload) => {
|
onInit: async (payload) => {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import configPromise from './config.js'
|
|||||||
import {
|
import {
|
||||||
defaultLocale,
|
defaultLocale,
|
||||||
englishTitle,
|
englishTitle,
|
||||||
|
hungarianLocale,
|
||||||
localizedPostsSlug,
|
localizedPostsSlug,
|
||||||
localizedSortSlug,
|
localizedSortSlug,
|
||||||
portugueseLocale,
|
portugueseLocale,
|
||||||
@@ -37,7 +38,6 @@ describe('Localization', () => {
|
|||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
;({ payload, restClient } = await initPayloadInt(configPromise))
|
;({ payload, restClient } = await initPayloadInt(configPromise))
|
||||||
|
|
||||||
// @ts-expect-error Force typing
|
|
||||||
post1 = await payload.create({
|
post1 = await payload.create({
|
||||||
collection,
|
collection,
|
||||||
data: {
|
data: {
|
||||||
@@ -45,7 +45,6 @@ describe('Localization', () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
// @ts-expect-error Force typing
|
|
||||||
postWithLocalizedData = await payload.create({
|
postWithLocalizedData = await payload.create({
|
||||||
collection,
|
collection,
|
||||||
data: {
|
data: {
|
||||||
@@ -185,7 +184,6 @@ describe('Localization', () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
// @ts-expect-error Force typing
|
|
||||||
localizedPost = await payload.update({
|
localizedPost = await payload.update({
|
||||||
id,
|
id,
|
||||||
collection,
|
collection,
|
||||||
@@ -276,6 +274,67 @@ describe('Localization', () => {
|
|||||||
|
|
||||||
expect(result.docs.map(({ id }) => id)).toContain(localizedPost.id)
|
expect(result.docs.map(({ id }) => id)).toContain(localizedPost.id)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (['mongodb'].includes(process.env.PAYLOAD_DATABASE)) {
|
||||||
|
describe('Localized sorting', () => {
|
||||||
|
let localizedAccentPostOne: LocalizedPost
|
||||||
|
let localizedAccentPostTwo: LocalizedPost
|
||||||
|
beforeEach(async () => {
|
||||||
|
localizedAccentPostOne = await payload.create({
|
||||||
|
collection,
|
||||||
|
data: {
|
||||||
|
title: 'non accent post',
|
||||||
|
localizedDescription: 'something',
|
||||||
|
},
|
||||||
|
locale: englishLocale,
|
||||||
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -352,7 +411,6 @@ describe('Localization', () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
// @ts-expect-error Force typing
|
|
||||||
withRelationship = await payload.create({
|
withRelationship = await payload.create({
|
||||||
collection: withLocalizedRelSlug,
|
collection: withLocalizedRelSlug,
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ export interface Config {
|
|||||||
globals: {
|
globals: {
|
||||||
'global-array': GlobalArray;
|
'global-array': GlobalArray;
|
||||||
};
|
};
|
||||||
locale: 'en' | 'es' | 'pt' | 'ar';
|
locale: 'en' | 'es' | 'hu' | 'pt' | 'ar';
|
||||||
user: User & {
|
user: User & {
|
||||||
collection: 'users';
|
collection: 'users';
|
||||||
};
|
};
|
||||||
@@ -70,6 +70,7 @@ export interface User {
|
|||||||
export interface LocalizedPost {
|
export interface LocalizedPost {
|
||||||
id: string;
|
id: string;
|
||||||
title?: string | null;
|
title?: string | null;
|
||||||
|
localizedDescription?: string | null;
|
||||||
description?: string | null;
|
description?: string | null;
|
||||||
localizedCheckbox?: boolean | null;
|
localizedCheckbox?: boolean | null;
|
||||||
children?: (string | LocalizedPost)[] | null;
|
children?: (string | LocalizedPost)[] | null;
|
||||||
@@ -316,6 +317,6 @@ export interface Auth {
|
|||||||
|
|
||||||
|
|
||||||
declare module 'payload' {
|
declare module 'payload' {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
export interface GeneratedTypes extends Config {}
|
export interface GeneratedTypes extends Config {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ export const relationSpanishTitle2 = `${relationSpanishTitle}2`
|
|||||||
export const defaultLocale = 'en'
|
export const defaultLocale = 'en'
|
||||||
export const spanishLocale = 'es'
|
export const spanishLocale = 'es'
|
||||||
export const portugueseLocale = 'pt'
|
export const portugueseLocale = 'pt'
|
||||||
|
export const hungarianLocale = 'hu'
|
||||||
|
|
||||||
// Slugs
|
// Slugs
|
||||||
export const localizedPostsSlug = 'localized-posts'
|
export const localizedPostsSlug = 'localized-posts'
|
||||||
|
|||||||
Reference in New Issue
Block a user