fix(db-postgres): select hasMany: true with autosave doesn't work properly (#11012)

Previously, select fields with `hasMany: true` didn't save properly in
Postgres on autosave.
This commit is contained in:
Sasha
2025-02-06 23:47:53 +02:00
committed by GitHub
parent 05e6f3326b
commit 3ad56cd86f
6 changed files with 50 additions and 5 deletions

View File

@@ -97,6 +97,7 @@ export const transformArray = ({
data: arrayRow,
fieldPrefix: '',
fields: field.flattenedFields,
insideArrayOrBlock: true,
locales: newRow.locales,
numbers,
parentTableName: arrayTableName,

View File

@@ -101,6 +101,7 @@ export const transformBlocks = ({
data: blockRow,
fieldPrefix: '',
fields: matchedBlock.flattenedFields,
insideArrayOrBlock: true,
locales: newRow.locales,
numbers,
parentTableName: blockTableName,

View File

@@ -42,6 +42,10 @@ type Args = {
fieldPrefix: string
fields: FlattenedField[]
forcedLocale?: string
/**
* Tracks whether the current traversion context is from array or block.
*/
insideArrayOrBlock?: boolean
locales: {
[locale: string]: Record<string, unknown>
}
@@ -77,6 +81,7 @@ export const traverseFields = ({
fieldPrefix,
fields,
forcedLocale,
insideArrayOrBlock = false,
locales,
numbers,
parentTableName,
@@ -230,6 +235,7 @@ export const traverseFields = ({
fieldPrefix: `${fieldName}_`,
fields: field.flattenedFields,
forcedLocale: localeKey,
insideArrayOrBlock,
locales,
numbers,
parentTableName,
@@ -258,6 +264,7 @@ export const traverseFields = ({
existingLocales,
fieldPrefix: `${fieldName}_`,
fields: field.flattenedFields,
insideArrayOrBlock,
locales,
numbers,
parentTableName,
@@ -420,7 +427,7 @@ export const traverseFields = ({
Object.entries(data[field.name]).forEach(([localeKey, localeData]) => {
if (Array.isArray(localeData)) {
const newRows = transformSelects({
id: data._uuid || data.id,
id: insideArrayOrBlock ? data._uuid || data.id : undefined,
data: localeData,
locale: localeKey,
})
@@ -431,7 +438,7 @@ export const traverseFields = ({
}
} else if (Array.isArray(data[field.name])) {
const newRows = transformSelects({
id: data._uuid || data.id,
id: insideArrayOrBlock ? data._uuid || data.id : undefined,
data: data[field.name],
locale: withinArrayOrBlockLocale,
})

View File

@@ -4,12 +4,12 @@ import { selectVersionsFieldsSlug } from '../../slugs.js'
const SelectVersionsFields: CollectionConfig = {
slug: selectVersionsFieldsSlug,
versions: true,
versions: { drafts: { autosave: true } },
fields: [
{
type: 'select',
hasMany: true,
options: ['a', 'b', 'c'],
options: ['a', 'b', 'c', 'd'],
name: 'hasMany',
},
{

View File

@@ -702,6 +702,40 @@ describe('Fields', () => {
expect(block.blocks[0]?.hasManyBlocks).toStrictEqual(['a', 'b'])
})
it('should work with autosave ', async () => {
let data = await payload.create({
collection: 'select-versions-fields',
data: { hasMany: ['a', 'b', 'c'] },
})
expect(data.hasMany).toStrictEqual(['a', 'b', 'c'])
data = await payload.update({
id: data.id,
collection: 'select-versions-fields',
data: { hasMany: ['a'] },
draft: true,
})
expect(data.hasMany).toStrictEqual(['a'])
data = await payload.update({
id: data.id,
collection: 'select-versions-fields',
data: { hasMany: ['a', 'b', 'c', 'd'] },
draft: true,
autosave: true,
})
expect(data.hasMany).toStrictEqual(['a', 'b', 'c', 'd'])
data = await payload.update({
id: data.id,
collection: 'select-versions-fields',
data: { hasMany: ['a'] },
draft: true,
autosave: true,
})
expect(data.hasMany).toStrictEqual(['a'])
})
})
describe('number', () => {

View File

@@ -487,7 +487,7 @@ export interface LexicalAccessControl {
*/
export interface SelectVersionsField {
id: string;
hasMany?: ('a' | 'b' | 'c')[] | null;
hasMany?: ('a' | 'b' | 'c' | 'd')[] | null;
array?:
| {
hasManyArr?: ('a' | 'b' | 'c')[] | null;
@@ -504,6 +504,7 @@ export interface SelectVersionsField {
| null;
updatedAt: string;
createdAt: string;
_status?: ('draft' | 'published') | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
@@ -2170,6 +2171,7 @@ export interface SelectVersionsFieldsSelect<T extends boolean = true> {
};
updatedAt?: T;
createdAt?: T;
_status?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema