Skip to content

Commit d64946c

Browse files
authored
fix(db-mongodb): ensure relationships are stored in ObjectID (#8932)
### What? Since the join field, we do store relationship fields values in `ObjectID`. This wasn't true if the field is nested to an array / blocks. ### Why? All relationship fields values should be stored in `ObjectID`. ### How? Fixes arrays / blocks handling in the `traverseFields.ts` function. Before it didn't run for them.
1 parent c41ef65 commit d64946c

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

packages/payload/src/utilities/traverseFields.ts

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1-
import type { Field, TabAsField } from '../fields/config/types.js'
1+
import type { ArrayField, BlocksField, Field, TabAsField } from '../fields/config/types.js'
22

33
import { fieldHasSubFields } from '../fields/config/types.js'
44

5+
const traverseArrayOrBlocksField = ({
6+
callback,
7+
data,
8+
field,
9+
parentRef,
10+
}: {
11+
callback: TraverseFieldsCallback
12+
data: Record<string, unknown>[]
13+
field: ArrayField | BlocksField
14+
parentRef?: unknown
15+
}) => {
16+
for (const ref of data) {
17+
let fields: Field[]
18+
if (field.type === 'blocks' && typeof ref?.blockType === 'string') {
19+
const block = field.blocks.find((block) => block.slug === ref.blockType)
20+
fields = block?.fields
21+
} else if (field.type === 'array') {
22+
fields = field.fields
23+
}
24+
25+
if (fields) {
26+
traverseFields({ callback, fields, parentRef, ref })
27+
}
28+
}
29+
}
30+
531
export type TraverseFieldsCallback = (args: {
632
/**
733
* The current field
@@ -68,11 +94,11 @@ export const traverseFields = ({
6894
})
6995
return
7096
}
71-
if (field.type !== 'tab' && fieldHasSubFields(field)) {
97+
if (field.type !== 'tab' && (fieldHasSubFields(field) || field.type === 'blocks')) {
7298
const parentRef = ref
7399
if ('name' in field && field.name) {
74100
if (typeof ref[field.name] === 'undefined') {
75-
if (field.type === 'array') {
101+
if (field.type === 'array' || field.type === 'blocks') {
76102
if (field.localized) {
77103
ref[field.name] = {}
78104
} else {
@@ -85,7 +111,33 @@ export const traverseFields = ({
85111
}
86112
ref = ref[field.name]
87113
}
88-
traverseFields({ callback, fields: field.fields, parentRef, ref })
114+
115+
if (field.type === 'blocks' || field.type === 'array') {
116+
if (field.localized) {
117+
for (const key in (ref ?? {}) as Record<string, unknown>) {
118+
const localeData = ref[key]
119+
if (!Array.isArray(localeData)) {
120+
continue
121+
}
122+
123+
traverseArrayOrBlocksField({
124+
callback,
125+
data: localeData,
126+
field,
127+
parentRef,
128+
})
129+
}
130+
} else if (Array.isArray(ref)) {
131+
traverseArrayOrBlocksField({
132+
callback,
133+
data: ref,
134+
field,
135+
parentRef,
136+
})
137+
}
138+
} else {
139+
traverseFields({ callback, fields: field.fields, parentRef, ref })
140+
}
89141
}
90142
})
91143
}

test/joins/int.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,6 @@ describe('Joins Field', () => {
643643
}
644644
}`
645645

646-
expect(true).toBeTruthy()
647646
const response = await restClient
648647
.GRAPHQL_POST({ body: JSON.stringify({ query }) })
649648
.then((res) => res.json())

0 commit comments

Comments
 (0)