Skip to content

Commit

Permalink
fix(db-postgres): error saving nested arrays with versions (#4302)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRibbens committed Nov 29, 2023
1 parent 4cfe473 commit 3514bfb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/db-postgres/src/upsertRow/insertArrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const insertArrays = async ({ adapter, arrays, db, parentRows }: Args): P
}
}

const parentID = parentRows[parentRowIndex].id
const parentID = parentRows[parentRowIndex].id || parentRows[parentRowIndex]._parentID

// Add any sub arrays that need to be created
// We will call this recursively below
Expand Down
79 changes: 45 additions & 34 deletions test/fields/collections/Array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,62 @@ import { ArrayRowLabel } from './LabelComponent'
export const arrayDefaultValue = [{ text: 'row one' }, { text: 'row two' }]

const ArrayFields: CollectionConfig = {
slug: arrayFieldsSlug,
admin: {
enableRichTextLink: false,
},
fields: [
{
name: 'items',
type: 'array',
required: true,
defaultValue: arrayDefaultValue,
fields: [
{
name: 'text',
type: 'text',
required: true,
type: 'text',
},
{
name: 'subArray',
fields: [
{
name: 'text',
type: 'text',
},
],
type: 'array',
},
],
required: true,
type: 'array',
},
{
name: 'collapsedArray',
type: 'array',
admin: {
initCollapsed: true,
},
fields: [
{
name: 'text',
type: 'text',
required: true,
type: 'text',
},
],
admin: {
initCollapsed: true,
},
type: 'array',
},
{
name: 'localized',
type: 'array',
required: true,
localized: true,
defaultValue: arrayDefaultValue,
fields: [
{
name: 'text',
type: 'text',
required: true,
type: 'text',
},
],
localized: true,
required: true,
type: 'array',
},
{
type: 'array',
name: 'readOnly',
admin: {
readOnly: true,
Expand All @@ -68,75 +76,78 @@ const ArrayFields: CollectionConfig = {
],
fields: [
{
type: 'text',
name: 'text',
type: 'text',
},
],
type: 'array',
},
{
type: 'array',
name: 'potentiallyEmptyArray',
fields: [
{
type: 'text',
name: 'text',
type: 'text',
},
{
type: 'group',
name: 'groupInRow',
fields: [
{
type: 'text',
name: 'textInGroupInRow',
type: 'text',
},
],
type: 'group',
},
],
type: 'array',
},
{
type: 'array',
name: 'rowLabelAsFunction',
fields: [
{
name: 'title',
type: 'text',
},
],
admin: {
description: 'Row labels rendered from a function.',
components: {
RowLabel: ({ data }) => data.title,
},
description: 'Row labels rendered from a function.',
},
},
{
type: 'array',
name: 'rowLabelAsComponent',
fields: [
{
name: 'title',
type: 'text',
},
],
type: 'array',
},
{
name: 'rowLabelAsComponent',
admin: {
description: 'Row labels rendered as react components.',
components: {
RowLabel: ArrayRowLabel,
},
description: 'Row labels rendered as react components.',
},
fields: [
{
name: 'title',
type: 'text',
},
],
type: 'array',
},
{
name: 'arrayWithMinRows',
type: 'array',
minRows: 2,
fields: [
{
name: 'text',
type: 'text',
},
],
minRows: 2,
type: 'array',
},
],
slug: arrayFieldsSlug,
versions: true,
}

export default ArrayFields
26 changes: 26 additions & 0 deletions test/fields/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,32 @@ describe('Fields', () => {
expect(doc.localized).toMatchObject(arrayDefaultValue)
})

it('should create with nested array', async () => {
const subArrayText = 'something expected'
const doc = await payload.create({
collection,
data: {
items: [
{
subArray: [
{
text: subArrayText,
},
],
text: 'test',
},
],
},
})

const result = await payload.findByID({
id: doc.id,
collection,
})

expect(result.items[0].subArray[0].text).toStrictEqual(subArrayText)
})

it('should update without overwriting other locales with defaultValue', async () => {
const localized = [{ text: 'unique' }]
const enText = 'english'
Expand Down

0 comments on commit 3514bfb

Please sign in to comment.