diff --git a/packages/db-mongodb/src/updateVersion.ts b/packages/db-mongodb/src/updateVersion.ts index 1fcc005ddb..5e1b356ce3 100644 --- a/packages/db-mongodb/src/updateVersion.ts +++ b/packages/db-mongodb/src/updateVersion.ts @@ -67,7 +67,7 @@ export const updateVersion: UpdateVersion = async function updateVersion( return null } - transform({ adapter: this, data: doc, fields, operation: 'write' }) + transform({ adapter: this, data: doc, fields, operation: 'read' }) return doc } diff --git a/test/versions/int.spec.ts b/test/versions/int.spec.ts index 82e1e34b15..097e224314 100644 --- a/test/versions/int.spec.ts +++ b/test/versions/int.spec.ts @@ -1,8 +1,8 @@ -import type { Payload } from 'payload'; +import type { Payload } from 'payload' import { schedulePublishHandler } from '@payloadcms/ui/utilities/schedulePublishHandler' import path from 'path' -import { createLocalReq , ValidationError } from 'payload' +import { createLocalReq, ValidationError } from 'payload' import { wait } from 'payload/shared' import { fileURLToPath } from 'url' @@ -689,6 +689,62 @@ describe('Versions', () => { { id: doc.id, message: 'The following field is invalid: Title' }, ]) }) + + it('should update with autosave: true', async () => { + // Save a draft + const { id } = await payload.create({ + collection: autosaveCollectionSlug, + draft: true, + data: { title: 'my-title', description: 'some-description', _status: 'draft' }, + }) + + // Autosave the same draft, calls db.updateVersion + const updated1 = await payload.update({ + collection: autosaveCollectionSlug, + id, + data: { + title: 'new-title', + }, + autosave: true, + draft: true, + }) + + const versionsCount = await payload.countVersions({ + collection: autosaveCollectionSlug, + where: { + parent: { + equals: id, + }, + }, + }) + + // This should not create a new version + const updated2 = await payload.update({ + collection: autosaveCollectionSlug, + id, + data: { + title: 'new-title-2', + }, + autosave: true, + draft: true, + }) + + const versionsCountAfter = await payload.countVersions({ + collection: autosaveCollectionSlug, + where: { + parent: { + equals: id, + }, + }, + }) + + expect(versionsCount.totalDocs).toBe(versionsCountAfter.totalDocs) + expect(updated1.id).toBe(id) + expect(updated1.title).toBe('new-title') + + expect(updated2.id).toBe(id) + expect(updated2.title).toBe('new-title-2') + }) }) describe('Update Many', () => {