Skip to content
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

fix(db-*): add delete version id for non-mongodb #10613

Open
wants to merge 2 commits 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
3 changes: 3 additions & 0 deletions packages/payload/src/versions/saveVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export const saveVersion = async ({
if (versionData._id) {
delete versionData._id
}
if (versionData.id) {
delete versionData.id
}

try {
if (autosave) {
Expand Down
32 changes: 32 additions & 0 deletions test/versions/collections/AutosaveWithMultiSelect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { CollectionConfig } from 'payload'

import { autosaveWithMultiSelectCollectionSlug } from '../slugs.js'

const AutosaveWithMultiSelectPosts: CollectionConfig = {
slug: autosaveWithMultiSelectCollectionSlug,
versions: {
drafts: {
autosave: {
interval: 2000,
},
},
},
fields: [
{
name: 'title',
label: 'Title',
type: 'text',
required: true,
unique: true,
localized: true,
},
{
name: 'tag',
type: 'select',
options: ['blog', 'essay', 'portfolio'],
hasMany: true,
},
],
}

export default AutosaveWithMultiSelectPosts
2 changes: 2 additions & 0 deletions test/versions/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
import AutosavePosts from './collections/Autosave.js'
import AutosaveWithMultiSelectPosts from './collections/AutosaveWithMultiSelect.js'
import CustomIDs from './collections/CustomIDs.js'
import DisablePublish from './collections/DisablePublish.js'
import DraftPosts from './collections/Drafts.js'
Expand All @@ -30,6 +31,7 @@ export default buildConfigWithDefaults({
DisablePublish,
Posts,
AutosavePosts,
AutosaveWithMultiSelectPosts,
DraftPosts,
DraftWithMax,
LocalizedPosts,
Expand Down
67 changes: 67 additions & 0 deletions test/versions/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { wait } from 'payload/shared'
import { fileURLToPath } from 'url'

import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import type { AutosaveMultiSelectPost } from './payload-types.js'

import { devUser } from '../credentials.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
Expand All @@ -16,6 +17,7 @@ import AutosaveGlobal from './globals/Autosave.js'
import {
autosaveCollectionSlug,
autoSaveGlobalSlug,
autosaveWithMultiSelectCollectionSlug,
draftCollectionSlug,
draftGlobalSlug,
localizedCollectionSlug,
Expand Down Expand Up @@ -654,6 +656,71 @@ describe('Versions', () => {
expect(Number(updatedUpdatedAt)).toBeGreaterThan(Number(createdUpdatedAt))
})

it('should update correct version at doc that has hasMany field when saving with autosave', async () => {
const firstDocTag: AutosaveMultiSelectPost['tag'] = ['blog', 'essay']
const doc = await payload.create({
collection: autosaveWithMultiSelectCollectionSlug,
data: {
title: 'title 1',
tag: firstDocTag,
_status: 'published',
},
draft: false,
})
await payload.update({
collection: autosaveWithMultiSelectCollectionSlug,
id: doc.id,
data: {
title: 'title 2',
tag: firstDocTag,
},
draft: true,
autosave: true,
})

const doc2 = await payload.create({
collection: autosaveWithMultiSelectCollectionSlug,
data: {
title: 'title 1-2',
tag: ['blog'],
_status: 'published',
},
draft: false,
})

await payload.update({
collection: autosaveWithMultiSelectCollectionSlug,
id: doc2.id,
data: {
tag: ['blog'],
title: 'title 2-2',
},
draft: true,
autosave: true,
})
await payload.update({
collection: autosaveWithMultiSelectCollectionSlug,
id: doc2.id,
data: {
tag: ['blog'],
title: 'title 3-2',
},
draft: true,
autosave: true,
})

const lastDocVersion = await payload.findVersions({
collection: autosaveWithMultiSelectCollectionSlug,
where: {
parent: {
equals: doc.id,
},
},
limit: 1,
})
expect(lastDocVersion.docs[0]?.version.tag).toEqual(firstDocTag)
})

it('should validate when publishing with the draft arg', async () => {
// no title (not valid for publishing)
const doc = await payload.create({
Expand Down
29 changes: 29 additions & 0 deletions test/versions/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Config {
'disable-publish': DisablePublish;
posts: Post;
'autosave-posts': AutosavePost;
'autosave-multi-select-posts': AutosaveMultiSelectPost;
'draft-posts': DraftPost;
'draft-with-max-posts': DraftWithMaxPost;
'localized-posts': LocalizedPost;
Expand All @@ -30,6 +31,7 @@ export interface Config {
'disable-publish': DisablePublishSelect<false> | DisablePublishSelect<true>;
posts: PostsSelect<false> | PostsSelect<true>;
'autosave-posts': AutosavePostsSelect<false> | AutosavePostsSelect<true>;
'autosave-multi-select-posts': AutosaveMultiSelectPostsSelect<false> | AutosaveMultiSelectPostsSelect<true>;
'draft-posts': DraftPostsSelect<false> | DraftPostsSelect<true>;
'draft-with-max-posts': DraftWithMaxPostsSelect<false> | DraftWithMaxPostsSelect<true>;
'localized-posts': LocalizedPostsSelect<false> | LocalizedPostsSelect<true>;
Expand Down Expand Up @@ -162,6 +164,18 @@ export interface DraftPost {
createdAt: string;
_status?: ('draft' | 'published') | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "autosave-multi-select-posts".
*/
export interface AutosaveMultiSelectPost {
id: string;
title: string;
tag?: ('blog' | 'essay' | 'portfolio')[] | null;
updatedAt: string;
createdAt: string;
_status?: ('draft' | 'published') | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "draft-with-max-posts".
Expand Down Expand Up @@ -336,6 +350,10 @@ export interface PayloadLockedDocument {
relationTo: 'autosave-posts';
value: string | AutosavePost;
} | null)
| ({
relationTo: 'autosave-multi-select-posts';
value: string | AutosaveMultiSelectPost;
} | null)
| ({
relationTo: 'draft-posts';
value: string | DraftPost;
Expand Down Expand Up @@ -438,6 +456,17 @@ export interface AutosavePostsSelect<T extends boolean = true> {
createdAt?: T;
_status?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "autosave-multi-select-posts_select".
*/
export interface AutosaveMultiSelectPostsSelect<T extends boolean = true> {
title?: T;
tag?: T;
updatedAt?: T;
createdAt?: T;
_status?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "draft-posts_select".
Expand Down
2 changes: 2 additions & 0 deletions test/versions/slugs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const autosaveCollectionSlug = 'autosave-posts'
export const autosaveWithMultiSelectCollectionSlug = 'autosave-multi-select-posts'

export const customIDSlug = 'custom-ids'

Expand All @@ -15,6 +16,7 @@ export const disablePublishGlobalSlug = 'disable-publish-global'

export const collectionSlugs = [
autosaveCollectionSlug,
autosaveWithMultiSelectCollectionSlug,
draftCollectionSlug,
postCollectionSlug,
versionCollectionSlug,
Expand Down