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:
@@ -97,6 +97,7 @@ export const transformArray = ({
|
|||||||
data: arrayRow,
|
data: arrayRow,
|
||||||
fieldPrefix: '',
|
fieldPrefix: '',
|
||||||
fields: field.flattenedFields,
|
fields: field.flattenedFields,
|
||||||
|
insideArrayOrBlock: true,
|
||||||
locales: newRow.locales,
|
locales: newRow.locales,
|
||||||
numbers,
|
numbers,
|
||||||
parentTableName: arrayTableName,
|
parentTableName: arrayTableName,
|
||||||
|
|||||||
@@ -101,6 +101,7 @@ export const transformBlocks = ({
|
|||||||
data: blockRow,
|
data: blockRow,
|
||||||
fieldPrefix: '',
|
fieldPrefix: '',
|
||||||
fields: matchedBlock.flattenedFields,
|
fields: matchedBlock.flattenedFields,
|
||||||
|
insideArrayOrBlock: true,
|
||||||
locales: newRow.locales,
|
locales: newRow.locales,
|
||||||
numbers,
|
numbers,
|
||||||
parentTableName: blockTableName,
|
parentTableName: blockTableName,
|
||||||
|
|||||||
@@ -42,6 +42,10 @@ type Args = {
|
|||||||
fieldPrefix: string
|
fieldPrefix: string
|
||||||
fields: FlattenedField[]
|
fields: FlattenedField[]
|
||||||
forcedLocale?: string
|
forcedLocale?: string
|
||||||
|
/**
|
||||||
|
* Tracks whether the current traversion context is from array or block.
|
||||||
|
*/
|
||||||
|
insideArrayOrBlock?: boolean
|
||||||
locales: {
|
locales: {
|
||||||
[locale: string]: Record<string, unknown>
|
[locale: string]: Record<string, unknown>
|
||||||
}
|
}
|
||||||
@@ -77,6 +81,7 @@ export const traverseFields = ({
|
|||||||
fieldPrefix,
|
fieldPrefix,
|
||||||
fields,
|
fields,
|
||||||
forcedLocale,
|
forcedLocale,
|
||||||
|
insideArrayOrBlock = false,
|
||||||
locales,
|
locales,
|
||||||
numbers,
|
numbers,
|
||||||
parentTableName,
|
parentTableName,
|
||||||
@@ -230,6 +235,7 @@ export const traverseFields = ({
|
|||||||
fieldPrefix: `${fieldName}_`,
|
fieldPrefix: `${fieldName}_`,
|
||||||
fields: field.flattenedFields,
|
fields: field.flattenedFields,
|
||||||
forcedLocale: localeKey,
|
forcedLocale: localeKey,
|
||||||
|
insideArrayOrBlock,
|
||||||
locales,
|
locales,
|
||||||
numbers,
|
numbers,
|
||||||
parentTableName,
|
parentTableName,
|
||||||
@@ -258,6 +264,7 @@ export const traverseFields = ({
|
|||||||
existingLocales,
|
existingLocales,
|
||||||
fieldPrefix: `${fieldName}_`,
|
fieldPrefix: `${fieldName}_`,
|
||||||
fields: field.flattenedFields,
|
fields: field.flattenedFields,
|
||||||
|
insideArrayOrBlock,
|
||||||
locales,
|
locales,
|
||||||
numbers,
|
numbers,
|
||||||
parentTableName,
|
parentTableName,
|
||||||
@@ -420,7 +427,7 @@ export const traverseFields = ({
|
|||||||
Object.entries(data[field.name]).forEach(([localeKey, localeData]) => {
|
Object.entries(data[field.name]).forEach(([localeKey, localeData]) => {
|
||||||
if (Array.isArray(localeData)) {
|
if (Array.isArray(localeData)) {
|
||||||
const newRows = transformSelects({
|
const newRows = transformSelects({
|
||||||
id: data._uuid || data.id,
|
id: insideArrayOrBlock ? data._uuid || data.id : undefined,
|
||||||
data: localeData,
|
data: localeData,
|
||||||
locale: localeKey,
|
locale: localeKey,
|
||||||
})
|
})
|
||||||
@@ -431,7 +438,7 @@ export const traverseFields = ({
|
|||||||
}
|
}
|
||||||
} else if (Array.isArray(data[field.name])) {
|
} else if (Array.isArray(data[field.name])) {
|
||||||
const newRows = transformSelects({
|
const newRows = transformSelects({
|
||||||
id: data._uuid || data.id,
|
id: insideArrayOrBlock ? data._uuid || data.id : undefined,
|
||||||
data: data[field.name],
|
data: data[field.name],
|
||||||
locale: withinArrayOrBlockLocale,
|
locale: withinArrayOrBlockLocale,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ import { selectVersionsFieldsSlug } from '../../slugs.js'
|
|||||||
|
|
||||||
const SelectVersionsFields: CollectionConfig = {
|
const SelectVersionsFields: CollectionConfig = {
|
||||||
slug: selectVersionsFieldsSlug,
|
slug: selectVersionsFieldsSlug,
|
||||||
versions: true,
|
versions: { drafts: { autosave: true } },
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
type: 'select',
|
type: 'select',
|
||||||
hasMany: true,
|
hasMany: true,
|
||||||
options: ['a', 'b', 'c'],
|
options: ['a', 'b', 'c', 'd'],
|
||||||
name: 'hasMany',
|
name: 'hasMany',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -702,6 +702,40 @@ describe('Fields', () => {
|
|||||||
|
|
||||||
expect(block.blocks[0]?.hasManyBlocks).toStrictEqual(['a', 'b'])
|
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', () => {
|
describe('number', () => {
|
||||||
|
|||||||
@@ -487,7 +487,7 @@ export interface LexicalAccessControl {
|
|||||||
*/
|
*/
|
||||||
export interface SelectVersionsField {
|
export interface SelectVersionsField {
|
||||||
id: string;
|
id: string;
|
||||||
hasMany?: ('a' | 'b' | 'c')[] | null;
|
hasMany?: ('a' | 'b' | 'c' | 'd')[] | null;
|
||||||
array?:
|
array?:
|
||||||
| {
|
| {
|
||||||
hasManyArr?: ('a' | 'b' | 'c')[] | null;
|
hasManyArr?: ('a' | 'b' | 'c')[] | null;
|
||||||
@@ -504,6 +504,7 @@ export interface SelectVersionsField {
|
|||||||
| null;
|
| null;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
|
_status?: ('draft' | 'published') | null;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* This interface was referenced by `Config`'s JSON-Schema
|
* This interface was referenced by `Config`'s JSON-Schema
|
||||||
@@ -2170,6 +2171,7 @@ export interface SelectVersionsFieldsSelect<T extends boolean = true> {
|
|||||||
};
|
};
|
||||||
updatedAt?: T;
|
updatedAt?: T;
|
||||||
createdAt?: T;
|
createdAt?: T;
|
||||||
|
_status?: T;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* This interface was referenced by `Config`'s JSON-Schema
|
* This interface was referenced by `Config`'s JSON-Schema
|
||||||
|
|||||||
Reference in New Issue
Block a user