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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 21 additions & 2 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 @@ -300,6 +302,9 @@ const stripFields = ({

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

Expand All @@ -310,6 +315,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 +332,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 @@ -347,6 +359,9 @@ const stripFields = ({

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

Expand All @@ -357,6 +372,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
47 changes: 47 additions & 0 deletions test/database/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2738,4 +2738,51 @@ describe('database', () => {
expect(res.blocks[0]?.nested).toHaveLength(1)
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')
})
})
Loading