Skip to content

fix(db-mongodb): strip deleted from the config blocks from the result #12869

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

Merged
merged 6 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 43 additions & 13 deletions packages/db-mongodb/src/utilities/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ const stripFields = ({
continue
}

for (const data of localeData) {
let hasNull = false
for (let i = 0; i < localeData.length; i++) {
const data = localeData[i]
let fields: FlattenedField[] | null = null

if (field.type === 'array') {
Expand All @@ -286,11 +288,17 @@ const stripFields = ({
let maybeBlock: FlattenedBlock | undefined = undefined

if (field.blockReferences) {
const maybeBlockReference = field.blockReferences.find(
(each) => typeof each === 'object' && each.slug === data.blockType,
)
if (maybeBlockReference && typeof maybeBlockReference === 'object') {
maybeBlock = maybeBlockReference
const maybeBlockReference = field.blockReferences.find((each) => {
const slug = typeof each === 'string' ? each : each.slug
return slug === data.blockType
})

if (maybeBlockReference) {
if (typeof maybeBlockReference === 'object') {
maybeBlock = maybeBlockReference
} else {
maybeBlock = config.blocks?.find((each) => each.slug === maybeBlockReference)
}
}
}

Expand All @@ -300,6 +308,9 @@ const stripFields = ({

if (maybeBlock) {
fields = maybeBlock.flattenedFields
} else {
localeData[i] = null
hasNull = true
}
}

Expand All @@ -310,6 +321,10 @@ const stripFields = ({
stripFields({ config, data, fields, reservedKeys })
}

if (hasNull) {
fieldData[localeKey] = localeData.filter(Boolean)
}

continue
} else {
stripFields({ config, data: localeData, fields: field.flattenedFields, reservedKeys })
Expand All @@ -323,7 +338,10 @@ const stripFields = ({
continue
}

for (const data of fieldData) {
let hasNull = false

for (let i = 0; i < fieldData.length; i++) {
const data = fieldData[i]
let fields: FlattenedField[] | null = null

if (field.type === 'array') {
Expand All @@ -332,12 +350,17 @@ const stripFields = ({
let maybeBlock: FlattenedBlock | undefined = undefined

if (field.blockReferences) {
const maybeBlockReference = field.blockReferences.find(
(each) => typeof each === 'object' && each.slug === data.blockType,
)

if (maybeBlockReference && typeof maybeBlockReference === 'object') {
maybeBlock = maybeBlockReference
const maybeBlockReference = field.blockReferences.find((each) => {
const slug = typeof each === 'string' ? each : each.slug
return slug === data.blockType
})

if (maybeBlockReference) {
if (typeof maybeBlockReference === 'object') {
maybeBlock = maybeBlockReference
} else {
maybeBlock = config.blocks?.find((each) => each.slug === maybeBlockReference)
}
}
}

Expand All @@ -347,6 +370,9 @@ const stripFields = ({

if (maybeBlock) {
fields = maybeBlock.flattenedFields
} else {
fieldData[i] = null
hasNull = true
}
}

Expand All @@ -357,6 +383,10 @@ const stripFields = ({
stripFields({ config, data, fields, reservedKeys })
}

if (hasNull) {
data[field.name] = fieldData.filter(Boolean)
}

continue
} else {
stripFields({ config, data: fieldData, fields: field.flattenedFields, reservedKeys })
Expand Down
2 changes: 1 addition & 1 deletion test/access-control/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable no-restricted-exports */
import { fileURLToPath } from 'node:url'
import path from 'path'
const filename = fileURLToPath(import.meta.url)
Expand Down
2 changes: 1 addition & 1 deletion test/admin-root/app/(payload)/importMap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CustomView as CustomView_c4f0e2747eca2be436a06a63cea31567 } from '../../CustomView/index.js'

export const importMap = {
"/CustomView/index.js#CustomView": CustomView_c4f0e2747eca2be436a06a63cea31567
'/CustomView/index.js#CustomView': CustomView_c4f0e2747eca2be436a06a63cea31567,
}
47 changes: 47 additions & 0 deletions test/database/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2714,6 +2714,53 @@ describe('database', () => {
expect(res.blocks[0]?.nested[0]?.nested).toHaveLength(0)
})

it('should ignore blocks that exist in the db but not in the config', async () => {
// not possible w/ SQL anyway
// eslint-disable-next-line jest/no-conditional-in-test
if (payload.db.name !== 'mongoose') {
return
}

const res = await payload.db.collections['blocks-docs']?.collection.insertOne({
testBlocks: [
{
id: '1',
blockType: 'cta',
text: 'valid block',
},
{
id: '2',
blockType: 'cta_2',
text: 'non-valid block',
},
],
testBlocksLocalized: {
en: [
{
id: '1',
blockType: 'cta',
text: 'valid block',
},
{
id: '2',
blockType: 'cta_2',
text: 'non-valid block',
},
],
},
})

const doc = await payload.findByID({
collection: 'blocks-docs',
id: res?.insertedId?.toHexString() as string,
locale: 'en',
})
expect(doc.testBlocks).toHaveLength(1)
expect(doc.testBlocks[0].id).toBe('1')
expect(doc.testBlocksLocalized).toHaveLength(1)
expect(doc.testBlocksLocalized[0].id).toBe('1')
})

it('should CRUD with blocks as JSON in SQL adapters', async () => {
// eslint-disable-next-line jest/no-conditional-in-test
if (!('drizzle' in payload.db)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "e7129a5b-a5af-490a-8ad9-318e3caeca26",
"id": "a3dd8ca0-5e09-407b-9178-e0ff7f15da59",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
Expand Down
8 changes: 4 additions & 4 deletions test/database/up-down-migration/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as migration_20250624_142145 from './20250624_142145.js'
import * as migration_20250624_214621 from './20250624_214621.js'

export const migrations = [
{
up: migration_20250624_142145.up,
down: migration_20250624_142145.down,
name: '20250624_142145',
up: migration_20250624_214621.up,
down: migration_20250624_214621.down,
name: '20250624_214621',
},
]