Skip to content

Commit

Permalink
fix(db-postgres): Remove duplicate keys from response (#4747)
Browse files Browse the repository at this point in the history
* Remove duplicate keys from response

* Update to delete duplicates at a higher level and remove '_order' from array rows too
  • Loading branch information
paulpopus committed Jan 16, 2024
1 parent ee5390a commit eb9e771
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
52 changes: 36 additions & 16 deletions packages/db-postgres/src/transform/read/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { fieldAffectsData } from 'payload/types'
import type { BlocksMap } from '../../utilities/createBlocksMap'

import { transformHasManyNumber } from './hasManyNumber'
import { transformRelationship } from './relationship'
import { transformHasManyText } from './hasManyText'
import { transformRelationship } from './relationship'

type TraverseFieldsArgs = {
/**
Expand All @@ -35,10 +35,6 @@ type TraverseFieldsArgs = {
* An array of Payload fields to traverse
*/
fields: (Field | TabAsField)[]
/**
* All hasMany text fields, as returned by Drizzle, keyed on an object by field path
*/
texts: Record<string, Record<string, unknown>[]>
/**
* All hasMany number fields, as returned by Drizzle, keyed on an object by field path
*/
Expand All @@ -55,6 +51,10 @@ type TraverseFieldsArgs = {
* Data structure representing the nearest table from db
*/
table: Record<string, unknown>
/**
* All hasMany text fields, as returned by Drizzle, keyed on an object by field path
*/
texts: Record<string, Record<string, unknown>[]>
}

// Traverse fields recursively, transforming data
Expand All @@ -66,11 +66,11 @@ export const traverseFields = <T extends Record<string, unknown>>({
deletions,
fieldPrefix,
fields,
texts,
numbers,
path,
relationships,
table,
texts,
}: TraverseFieldsArgs): T => {
const sanitizedPath = path ? `${path}.` : path

Expand All @@ -83,11 +83,11 @@ export const traverseFields = <T extends Record<string, unknown>>({
deletions,
fieldPrefix,
fields: field.tabs.map((tab) => ({ ...tab, type: 'tab' })),
texts,
numbers,
path,
relationships,
table,
texts,
})
}

Expand All @@ -103,17 +103,22 @@ export const traverseFields = <T extends Record<string, unknown>>({
deletions,
fieldPrefix,
fields: field.fields,
texts,
numbers,
path,
relationships,
table,
texts,
})
}

if (fieldAffectsData(field)) {
const fieldName = `${fieldPrefix || ''}${field.name}`
const fieldData = table[fieldName]

if (fieldPrefix) {
deletions.push(() => delete table[fieldName])
}

if (field.type === 'array') {
if (Array.isArray(fieldData)) {
if (field.localized) {
Expand All @@ -135,13 +140,17 @@ export const traverseFields = <T extends Record<string, unknown>>({
deletions,
fieldPrefix: '',
fields: field.fields,
texts,
numbers,
path: `${sanitizedPath}${field.name}.${row._order - 1}`,
relationships,
table: row,
texts,
})

if ('_order' in rowResult) {
delete rowResult._order
}

arrayResult[locale].push(rowResult)
}

Expand All @@ -153,18 +162,23 @@ export const traverseFields = <T extends Record<string, unknown>>({
row.id = row._uuid
delete row._uuid
}

if ('_order' in row) {
delete row._order
}

return traverseFields<T>({
blocks,
config,
dataRef: row,
deletions,
fieldPrefix: '',
fields: field.fields,
texts,
numbers,
path: `${sanitizedPath}${field.name}.${i}`,
relationships,
table: row,
texts,
})
})
}
Expand Down Expand Up @@ -204,11 +218,11 @@ export const traverseFields = <T extends Record<string, unknown>>({
deletions,
fieldPrefix: '',
fields: block.fields,
texts,
numbers,
path: `${blockFieldPath}.${row._order - 1}`,
relationships,
table: row,
texts,
})

delete blockResult._order
Expand All @@ -235,11 +249,11 @@ export const traverseFields = <T extends Record<string, unknown>>({
deletions,
fieldPrefix: '',
fields: block.fields,
texts,
numbers,
path: `${blockFieldPath}.${i}`,
relationships,
table: row,
texts,
})
}

Expand Down Expand Up @@ -316,15 +330,15 @@ export const traverseFields = <T extends Record<string, unknown>>({
transformHasManyText({
field,
locale,
textRows: texts,
ref: result,
textRows: texts,
})
})
} else {
transformHasManyText({
field,
textRows: textPathMatch,
ref: result,
textRows: textPathMatch,
})
}

Expand Down Expand Up @@ -420,13 +434,16 @@ export const traverseFields = <T extends Record<string, unknown>>({
deletions,
fieldPrefix: groupFieldPrefix,
fields: field.fields,
texts,
numbers,
path: `${sanitizedPath}${field.name}`,
relationships,
table,
texts,
})
})
if ('_order' in ref) {
delete ref._order
}
} else {
const groupData = {}

Expand All @@ -437,12 +454,15 @@ export const traverseFields = <T extends Record<string, unknown>>({
deletions,
fieldPrefix: groupFieldPrefix,
fields: field.fields,
texts,
numbers,
path: `${sanitizedPath}${field.name}`,
relationships,
table,
texts,
})
if ('_order' in ref) {
delete ref._order
}
}

break
Expand Down
18 changes: 18 additions & 0 deletions test/fields/collections/Group/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ const GroupFields: CollectionConfig = {
},
],
},
{
name: 'arrayOfGroups',
type: 'array',
defaultValue: [
{
groupItem: {
text: 'Hello world',
},
},
],
fields: [
{
name: 'groupItem',
type: 'group',
fields: [{ name: 'text', type: 'text' }],
},
],
},
{
name: 'potentiallyEmptyGroup',
type: 'group',
Expand Down
21 changes: 19 additions & 2 deletions test/fields/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('Fields', () => {
})

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// @ts-expect-error
expect(localizedDoc.localizedHasMany.en).toEqual(localizedHasMany)
})
})
Expand Down Expand Up @@ -413,7 +413,7 @@ describe('Fields', () => {
})

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// @ts-expect-error
expect(localizedDoc.localizedHasMany.en).toEqual(localizedHasMany)
})
})
Expand Down Expand Up @@ -643,6 +643,14 @@ describe('Fields', () => {
collection,
})

expect(result.items[0]).toMatchObject({
subArray: [
{
text: subArrayText,
},
],
text: 'test',
})
expect(result.items[0].subArray[0].text).toStrictEqual(subArrayText)
})

Expand Down Expand Up @@ -707,6 +715,15 @@ describe('Fields', () => {
expect(document.group.defaultParent).toStrictEqual(groupDefaultValue)
expect(document.group.defaultChild).toStrictEqual(groupDefaultChild)
})

it('should not have duplicate keys', async () => {
expect(document.arrayOfGroups[0]).toMatchObject({
id: expect.any(String),
groupItem: {
text: 'Hello world',
},
})
})
})

describe('tabs', () => {
Expand Down

0 comments on commit eb9e771

Please sign in to comment.