Skip to content

Commit 77cdb17

Browse files
authored
fix(db-mongodb): virtual fields within blocks (#16107)
1 parent 185548a commit 77cdb17

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
"test:eval:rest-api:low-power": "cross-env EVAL_VARIANT=low-power NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 pnpm exec vitest --run --project eval eval.rest-api.spec",
156156
"test:eval:rest-api:skill": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 pnpm exec vitest --run --project eval eval.rest-api.spec",
157157
"test:eval:skill": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 vitest --run --project eval",
158-
"test:int": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 DISABLE_LOGGING=true vitest --project int",
158+
"test:int": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 DISABLE_LOGGING=true vitest --project int --no-cache",
159159
"test:int:firestore": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=firestore DISABLE_LOGGING=true vitest --project int",
160160
"test:int:postgres": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=postgres DISABLE_LOGGING=true vitest --project int",
161161
"test:int:sqlite": "cross-env NODE_OPTIONS=\"--no-deprecation --no-experimental-strip-types\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=sqlite DISABLE_LOGGING=true vitest --project int",

packages/db-mongodb/src/models/buildSchema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ const blocks: FieldSchemaGenerator<BlocksField> = (
268268
}
269269

270270
block.fields.forEach((blockField) => {
271+
if (fieldIsVirtual(blockField)) {
272+
return
273+
}
274+
271275
const addFieldSchema = getSchemaGenerator(blockField.type)
272276

273277
if (addFieldSchema) {

test/database/getConfig.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,26 @@ export const getConfig: () => Partial<Config> = () => ({
832832
},
833833
],
834834
},
835+
{
836+
name: 'blockWithVirtual',
837+
type: 'blocks',
838+
blocks: [
839+
{
840+
slug: 'blockWithVirtual',
841+
fields: [
842+
{
843+
name: 'text',
844+
type: 'text',
845+
},
846+
{
847+
name: 'virtualField',
848+
type: 'text',
849+
virtual: true,
850+
},
851+
],
852+
},
853+
],
854+
},
835855
],
836856
},
837857
{

test/database/int.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,6 +3290,32 @@ describe('database', () => {
32903290
expect(res.textWithinTabs).toBeUndefined()
32913291
})
32923292

3293+
it('should not save a virtual field inside a block to the db', async () => {
3294+
const created = await payload.create({
3295+
collection: fieldsPersistanceSlug,
3296+
data: {
3297+
blockWithVirtual: [
3298+
{
3299+
blockType: 'blockWithVirtual',
3300+
text: 'some text',
3301+
virtualField: 'should not be saved',
3302+
},
3303+
],
3304+
},
3305+
})
3306+
3307+
const resDb = (await payload.db.findOne({
3308+
collection: fieldsPersistanceSlug,
3309+
req: {} as PayloadRequest,
3310+
where: { id: { equals: created.id } },
3311+
})) as Record<string, unknown>
3312+
3313+
const block = (resDb.blockWithVirtual as Record<string, unknown>[])?.[0]
3314+
3315+
expect(block?.virtualField).toBeUndefined()
3316+
expect(block?.text).toBe('some text')
3317+
})
3318+
32933319
it('should allow virtual field with reference', async () => {
32943320
const post = await payload.create({ collection: 'posts', data: { title: 'my-title' } })
32953321
const { id } = await payload.create({

0 commit comments

Comments
 (0)