diff --git a/packages/db-mongodb/src/utilities/transform.ts b/packages/db-mongodb/src/utilities/transform.ts index e5633f1acf9..011a963be90 100644 --- a/packages/db-mongodb/src/utilities/transform.ts +++ b/packages/db-mongodb/src/utilities/transform.ts @@ -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') { @@ -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) + } } } @@ -300,6 +308,9 @@ const stripFields = ({ if (maybeBlock) { fields = maybeBlock.flattenedFields + } else { + localeData[i] = null + hasNull = true } } @@ -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 }) @@ -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') { @@ -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) + } } } @@ -347,6 +370,9 @@ const stripFields = ({ if (maybeBlock) { fields = maybeBlock.flattenedFields + } else { + fieldData[i] = null + hasNull = true } } @@ -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 }) diff --git a/test/access-control/config.ts b/test/access-control/config.ts index 5040400a4b3..f82fce76bda 100644 --- a/test/access-control/config.ts +++ b/test/access-control/config.ts @@ -1,4 +1,4 @@ -/* eslint-disable no-restricted-exports */ + import { fileURLToPath } from 'node:url' import path from 'path' const filename = fileURLToPath(import.meta.url) diff --git a/test/admin-root/app/(payload)/importMap.js b/test/admin-root/app/(payload)/importMap.js index 7e732f32064..c0cfa3580dd 100644 --- a/test/admin-root/app/(payload)/importMap.js +++ b/test/admin-root/app/(payload)/importMap.js @@ -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, } diff --git a/test/database/int.spec.ts b/test/database/int.spec.ts index e2b04bde95a..5fec4f070d8 100644 --- a/test/database/int.spec.ts +++ b/test/database/int.spec.ts @@ -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)) { diff --git a/test/database/up-down-migration/migrations/20250624_142145.json b/test/database/up-down-migration/migrations/20250624_214621.json similarity index 99% rename from test/database/up-down-migration/migrations/20250624_142145.json rename to test/database/up-down-migration/migrations/20250624_214621.json index 5f1b0649967..3e1e61171ee 100644 --- a/test/database/up-down-migration/migrations/20250624_142145.json +++ b/test/database/up-down-migration/migrations/20250624_214621.json @@ -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", diff --git a/test/database/up-down-migration/migrations/20250624_142145.ts b/test/database/up-down-migration/migrations/20250624_214621.ts similarity index 100% rename from test/database/up-down-migration/migrations/20250624_142145.ts rename to test/database/up-down-migration/migrations/20250624_214621.ts diff --git a/test/database/up-down-migration/migrations/index.ts b/test/database/up-down-migration/migrations/index.ts index 7a353ffcca5..91d190b4551 100644 --- a/test/database/up-down-migration/migrations/index.ts +++ b/test/database/up-down-migration/migrations/index.ts @@ -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', }, ]