Skip to content

Commit e042561

Browse files
authored
fix: forward locale in count and countGlobalVersions (3.x) (#17547)
Forwards the request `locale` to `payload.db.count` and `payload.db.countGlobalVersions` so queries on localized fields resolve against the correct locale. Both operations omitted `locale` (unlike `find` and `countVersions`). On adapters that key localized fields by locale, a `where` on a localized field resolved at an `undefined` locale and returned `0`, while an equivalent `find` returned matches. This surfaced with `versions.drafts.localizeStatus`, where `_status` is localized: ```ts await payload.count({ collection, where: { _status: { equals: 'published' } } }) // 0 await payload.find({ collection, where: { _status: { equals: 'published' } } }) // 10 ``` ```diff result = await payload.db.count({ collection: collectionConfig.slug, + locale: locale!, req, where: fullWhere, }) ```
1 parent 6402daa commit e042561

3 files changed

Lines changed: 88 additions & 1 deletion

File tree

packages/payload/src/collections/operations/count.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export const countOperation = async <TSlug extends CollectionSlug>(
8888

8989
result = await payload.db.count({
9090
collection: collectionConfig.slug,
91+
locale: req?.locale || undefined,
9192
req,
9293
where: fullWhere,
9394
})

packages/payload/src/globals/operations/countGlobalVersions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export const countGlobalVersionsOperation = async <TSlug extends GlobalSlug>(
7777

7878
const result = await payload.db.countGlobalVersions({
7979
global: global.slug,
80+
locale: req?.locale || undefined,
8081
req,
8182
where: fullWhere,
8283
})

test/localization/int.spec.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Payload, User, Where } from 'payload'
33
import path from 'path'
44
import { createLocalReq } from 'payload'
55
import { fileURLToPath } from 'url'
6-
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'
6+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'
77

88
import type { NextRESTClient } from '../__helpers/shared/NextRESTClient.js'
99
import type {
@@ -66,6 +66,60 @@ describe('Localization', () => {
6666
await payload.destroy()
6767
})
6868

69+
describe('count with localized field query', () => {
70+
const createdIDs: Array<number | string> = []
71+
72+
afterEach(async () => {
73+
for (const id of createdIDs) {
74+
await payload.delete({ collection: localizedPostsSlug, id })
75+
}
76+
createdIDs.length = 0
77+
})
78+
79+
it('should respect the requested locale and agree with find', async () => {
80+
const doc = await payload.create({
81+
collection: localizedPostsSlug,
82+
data: { title: 'count-en' },
83+
locale: englishLocale,
84+
})
85+
createdIDs.push(doc.id)
86+
87+
await payload.update({
88+
id: doc.id,
89+
collection: localizedPostsSlug,
90+
data: { title: 'count-es' },
91+
locale: spanishLocale,
92+
})
93+
94+
const englishWhere = { title: { equals: 'count-en' } }
95+
96+
const { totalDocs: countEn } = await payload.count({
97+
collection: localizedPostsSlug,
98+
locale: englishLocale,
99+
where: englishWhere,
100+
})
101+
102+
const { docs: findEn } = await payload.find({
103+
collection: localizedPostsSlug,
104+
locale: englishLocale,
105+
where: englishWhere,
106+
})
107+
108+
// count must agree with find for a localized-field query
109+
expect(countEn).toBe(findEn.length)
110+
expect(countEn).toBe(1)
111+
112+
// The same value must not match in a locale where the field holds a different value
113+
const { totalDocs: countEsForEnglishTitle } = await payload.count({
114+
collection: localizedPostsSlug,
115+
locale: spanishLocale,
116+
where: englishWhere,
117+
})
118+
119+
expect(countEsForEnglishTitle).toBe(0)
120+
})
121+
})
122+
69123
describe('Localization with fallback true', () => {
70124
let post1: LocalizedPost
71125
let postWithLocalizedData: LocalizedPost
@@ -4558,6 +4612,37 @@ describe('Localization', () => {
45584612
})
45594613
expect(result2.totalDocs).toBe(1)
45604614
})
4615+
4616+
it('should count global versions with query on localized field respecting locale', async () => {
4617+
await payload.updateGlobal({
4618+
slug: globalWithDraftsSlug,
4619+
data: { text: 'global count en', _status: 'published' },
4620+
locale: defaultLocale,
4621+
})
4622+
4623+
await payload.updateGlobal({
4624+
slug: globalWithDraftsSlug,
4625+
data: { text: 'global count es', _status: 'published' },
4626+
locale: spanishLocale,
4627+
})
4628+
4629+
const englishWhere = { 'version.text': { equals: 'global count en' } }
4630+
4631+
const inEnglish = await payload.countGlobalVersions({
4632+
global: globalWithDraftsSlug,
4633+
locale: defaultLocale,
4634+
where: englishWhere,
4635+
})
4636+
4637+
const inSpanish = await payload.countGlobalVersions({
4638+
global: globalWithDraftsSlug,
4639+
locale: spanishLocale,
4640+
where: englishWhere,
4641+
})
4642+
4643+
expect(inEnglish.totalDocs).toBeGreaterThan(0)
4644+
expect(inSpanish.totalDocs).toBe(0)
4645+
})
45614646
})
45624647
})
45634648

0 commit comments

Comments
 (0)