Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db-mongodb): ensure relationships are stored in ObjectID #8932

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions packages/payload/src/utilities/traverseFields.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Field, TabAsField } from '../fields/config/types.js'
import type { ArrayField, BlocksField, Field, TabAsField } from '../fields/config/types.js'

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

Expand Down Expand Up @@ -68,11 +68,11 @@ export const traverseFields = ({
})
return
}
if (field.type !== 'tab' && fieldHasSubFields(field)) {
if (field.type !== 'tab' && (fieldHasSubFields(field) || field.type === 'blocks')) {
const parentRef = ref
if ('name' in field && field.name) {
if (typeof ref[field.name] === 'undefined') {
if (field.type === 'array') {
if (field.type === 'array' || field.type === 'blocks') {
if (field.localized) {
ref[field.name] = {}
} else {
Expand All @@ -85,7 +85,51 @@ export const traverseFields = ({
}
ref = ref[field.name]
}
traverseFields({ callback, fields: field.fields, parentRef, ref })

if (field.type === 'blocks' || field.type === 'array') {
const traverseArrayOrBlocksField = ({
data,
DanRibbens marked this conversation as resolved.
Show resolved Hide resolved
field,
}: {
data: Record<string, unknown>[]
field: ArrayField | BlocksField
}) => {
for (const ref of data) {
let fields: Field[]
if (field.type === 'blocks' && typeof ref?.blockType === 'string') {
const block = field.blocks.find((block) => block.slug === ref.blockType)
fields = block?.fields
} else if (field.type === 'array') {
fields = field.fields
}

if (fields) {
traverseFields({ callback, fields, parentRef, ref })
}
}
}

if (field.localized) {
for (const key in (ref ?? {}) as Record<string, unknown>) {
const localeData = ref[key]
if (!Array.isArray(localeData)) {
continue
}

traverseArrayOrBlocksField({
data: localeData,
field,
})
}
} else if (Array.isArray(ref)) {
traverseArrayOrBlocksField({
data: ref,
field,
})
}
} else {
traverseFields({ callback, fields: field.fields, parentRef, ref })
}
}
})
}
Loading