Skip to content

Commit 3fa834a

Browse files
authored
fix(db-sqlite): scheduled publish does not show upcoming events because of wrong nested json querying (#15911)
Fixes #12749 After: (upcoming events are displayed correctly) <img width="1443" height="833" alt="image" src="https://github.com/user-attachments/assets/a2e8677b-c621-4461-b9e8-0e43c82c94ea" /> The reason is because json querying was handled wrongly in the adapter for this query that we send from the frontend: ``` where[and][2][input.doc.value][equals] = id where[and][3][input.doc.relationTo][equals] = collectionSlug ```
1 parent 560cabe commit 3fa834a

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

packages/drizzle/src/sqlite/createJSONQuery/convertPathToJSONTraversal.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ export const convertPathToJSONTraversal = (incomingSegments: string[]): string =
44
const segments = [...incomingSegments]
55
segments.shift()
66

7-
return segments.reduce((res, segment) => {
7+
return segments.reduce((res, segment, i) => {
88
const formattedSegment = Number.isNaN(parseInt(segment))
99
? `'${sanitizePathSegment(segment)}'`
1010
: segment
11-
return `${res}->>${formattedSegment}`
11+
const isLast = i === segments.length - 1
12+
return `${res}${isLast ? '->>' : '->'}${formattedSegment}`
1213
}, '')
1314
}

test/fields/int.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3807,6 +3807,68 @@ describe('Fields', () => {
38073807
expect(docs[0].id).toBe(json_1.id)
38083808
})
38093809

3810+
it('should query 2-level nested object properties', async () => {
3811+
const docId = '42'
3812+
const collectionSlug = 'posts'
3813+
3814+
const matchingDoc = await payload.create({
3815+
collection: 'json-fields',
3816+
data: {
3817+
json: { doc: { value: docId, relationTo: collectionSlug } },
3818+
},
3819+
})
3820+
3821+
// different ID, same relationTo — should NOT match the and query
3822+
await payload.create({
3823+
collection: 'json-fields',
3824+
data: {
3825+
json: { doc: { value: '99', relationTo: collectionSlug } },
3826+
},
3827+
})
3828+
3829+
// same ID, different relationTo — should NOT match the and query
3830+
await payload.create({
3831+
collection: 'json-fields',
3832+
data: {
3833+
json: { doc: { value: docId, relationTo: 'other' } },
3834+
},
3835+
})
3836+
3837+
const { docs: equalsDocs } = await payload.find({
3838+
collection: 'json-fields',
3839+
where: {
3840+
'json.doc.value': { equals: docId },
3841+
},
3842+
})
3843+
3844+
expect(equalsDocs.map(({ id }) => id)).toContain(matchingDoc.id)
3845+
3846+
const { docs: existsDocs } = await payload.find({
3847+
collection: 'json-fields',
3848+
where: {
3849+
'json.doc.value': { exists: true },
3850+
},
3851+
})
3852+
3853+
expect(existsDocs.map(({ id }) => id)).toContain(matchingDoc.id)
3854+
3855+
// mirrors the pattern from scheduled jobs frontend query:
3856+
// where[and][2][input.doc.value][equals] = id
3857+
// where[and][3][input.doc.relationTo][equals] = collectionSlug
3858+
const { docs: andDocs } = await payload.find({
3859+
collection: 'json-fields',
3860+
where: {
3861+
and: [
3862+
{ 'json.doc.value': { equals: docId } },
3863+
{ 'json.doc.relationTo': { equals: collectionSlug } },
3864+
],
3865+
},
3866+
})
3867+
3868+
expect(andDocs).toHaveLength(1)
3869+
expect(andDocs[0]?.id).toBe(matchingDoc.id)
3870+
})
3871+
38103872
it('should disallow unsafe query paths', async () => {
38113873
await expect(
38123874
payload.find({

0 commit comments

Comments
 (0)