Skip to content

Commit eb9e771

Browse files
authored
fix(db-postgres): Remove duplicate keys from response (#4747)
* Remove duplicate keys from response * Update to delete duplicates at a higher level and remove '_order' from array rows too
1 parent ee5390a commit eb9e771

File tree

3 files changed

+73
-18
lines changed

3 files changed

+73
-18
lines changed

packages/db-postgres/src/transform/read/traverseFields.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { fieldAffectsData } from 'payload/types'
77
import type { BlocksMap } from '../../utilities/createBlocksMap'
88

99
import { transformHasManyNumber } from './hasManyNumber'
10-
import { transformRelationship } from './relationship'
1110
import { transformHasManyText } from './hasManyText'
11+
import { transformRelationship } from './relationship'
1212

1313
type TraverseFieldsArgs = {
1414
/**
@@ -35,10 +35,6 @@ type TraverseFieldsArgs = {
3535
* An array of Payload fields to traverse
3636
*/
3737
fields: (Field | TabAsField)[]
38-
/**
39-
* All hasMany text fields, as returned by Drizzle, keyed on an object by field path
40-
*/
41-
texts: Record<string, Record<string, unknown>[]>
4238
/**
4339
* All hasMany number fields, as returned by Drizzle, keyed on an object by field path
4440
*/
@@ -55,6 +51,10 @@ type TraverseFieldsArgs = {
5551
* Data structure representing the nearest table from db
5652
*/
5753
table: Record<string, unknown>
54+
/**
55+
* All hasMany text fields, as returned by Drizzle, keyed on an object by field path
56+
*/
57+
texts: Record<string, Record<string, unknown>[]>
5858
}
5959

6060
// Traverse fields recursively, transforming data
@@ -66,11 +66,11 @@ export const traverseFields = <T extends Record<string, unknown>>({
6666
deletions,
6767
fieldPrefix,
6868
fields,
69-
texts,
7069
numbers,
7170
path,
7271
relationships,
7372
table,
73+
texts,
7474
}: TraverseFieldsArgs): T => {
7575
const sanitizedPath = path ? `${path}.` : path
7676

@@ -83,11 +83,11 @@ export const traverseFields = <T extends Record<string, unknown>>({
8383
deletions,
8484
fieldPrefix,
8585
fields: field.tabs.map((tab) => ({ ...tab, type: 'tab' })),
86-
texts,
8786
numbers,
8887
path,
8988
relationships,
9089
table,
90+
texts,
9191
})
9292
}
9393

@@ -103,17 +103,22 @@ export const traverseFields = <T extends Record<string, unknown>>({
103103
deletions,
104104
fieldPrefix,
105105
fields: field.fields,
106-
texts,
107106
numbers,
108107
path,
109108
relationships,
110109
table,
110+
texts,
111111
})
112112
}
113113

114114
if (fieldAffectsData(field)) {
115115
const fieldName = `${fieldPrefix || ''}${field.name}`
116116
const fieldData = table[fieldName]
117+
118+
if (fieldPrefix) {
119+
deletions.push(() => delete table[fieldName])
120+
}
121+
117122
if (field.type === 'array') {
118123
if (Array.isArray(fieldData)) {
119124
if (field.localized) {
@@ -135,13 +140,17 @@ export const traverseFields = <T extends Record<string, unknown>>({
135140
deletions,
136141
fieldPrefix: '',
137142
fields: field.fields,
138-
texts,
139143
numbers,
140144
path: `${sanitizedPath}${field.name}.${row._order - 1}`,
141145
relationships,
142146
table: row,
147+
texts,
143148
})
144149

150+
if ('_order' in rowResult) {
151+
delete rowResult._order
152+
}
153+
145154
arrayResult[locale].push(rowResult)
146155
}
147156

@@ -153,18 +162,23 @@ export const traverseFields = <T extends Record<string, unknown>>({
153162
row.id = row._uuid
154163
delete row._uuid
155164
}
165+
166+
if ('_order' in row) {
167+
delete row._order
168+
}
169+
156170
return traverseFields<T>({
157171
blocks,
158172
config,
159173
dataRef: row,
160174
deletions,
161175
fieldPrefix: '',
162176
fields: field.fields,
163-
texts,
164177
numbers,
165178
path: `${sanitizedPath}${field.name}.${i}`,
166179
relationships,
167180
table: row,
181+
texts,
168182
})
169183
})
170184
}
@@ -204,11 +218,11 @@ export const traverseFields = <T extends Record<string, unknown>>({
204218
deletions,
205219
fieldPrefix: '',
206220
fields: block.fields,
207-
texts,
208221
numbers,
209222
path: `${blockFieldPath}.${row._order - 1}`,
210223
relationships,
211224
table: row,
225+
texts,
212226
})
213227

214228
delete blockResult._order
@@ -235,11 +249,11 @@ export const traverseFields = <T extends Record<string, unknown>>({
235249
deletions,
236250
fieldPrefix: '',
237251
fields: block.fields,
238-
texts,
239252
numbers,
240253
path: `${blockFieldPath}.${i}`,
241254
relationships,
242255
table: row,
256+
texts,
243257
})
244258
}
245259

@@ -316,15 +330,15 @@ export const traverseFields = <T extends Record<string, unknown>>({
316330
transformHasManyText({
317331
field,
318332
locale,
319-
textRows: texts,
320333
ref: result,
334+
textRows: texts,
321335
})
322336
})
323337
} else {
324338
transformHasManyText({
325339
field,
326-
textRows: textPathMatch,
327340
ref: result,
341+
textRows: textPathMatch,
328342
})
329343
}
330344

@@ -420,13 +434,16 @@ export const traverseFields = <T extends Record<string, unknown>>({
420434
deletions,
421435
fieldPrefix: groupFieldPrefix,
422436
fields: field.fields,
423-
texts,
424437
numbers,
425438
path: `${sanitizedPath}${field.name}`,
426439
relationships,
427440
table,
441+
texts,
428442
})
429443
})
444+
if ('_order' in ref) {
445+
delete ref._order
446+
}
430447
} else {
431448
const groupData = {}
432449

@@ -437,12 +454,15 @@ export const traverseFields = <T extends Record<string, unknown>>({
437454
deletions,
438455
fieldPrefix: groupFieldPrefix,
439456
fields: field.fields,
440-
texts,
441457
numbers,
442458
path: `${sanitizedPath}${field.name}`,
443459
relationships,
444460
table,
461+
texts,
445462
})
463+
if ('_order' in ref) {
464+
delete ref._order
465+
}
446466
}
447467

448468
break

test/fields/collections/Group/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ const GroupFields: CollectionConfig = {
5858
},
5959
],
6060
},
61+
{
62+
name: 'arrayOfGroups',
63+
type: 'array',
64+
defaultValue: [
65+
{
66+
groupItem: {
67+
text: 'Hello world',
68+
},
69+
},
70+
],
71+
fields: [
72+
{
73+
name: 'groupItem',
74+
type: 'group',
75+
fields: [{ name: 'text', type: 'text' }],
76+
},
77+
],
78+
},
6179
{
6280
name: 'potentiallyEmptyGroup',
6381
type: 'group',

test/fields/int.spec.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe('Fields', () => {
112112
})
113113

114114
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
115-
// @ts-ignore
115+
// @ts-expect-error
116116
expect(localizedDoc.localizedHasMany.en).toEqual(localizedHasMany)
117117
})
118118
})
@@ -413,7 +413,7 @@ describe('Fields', () => {
413413
})
414414

415415
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
416-
// @ts-ignore
416+
// @ts-expect-error
417417
expect(localizedDoc.localizedHasMany.en).toEqual(localizedHasMany)
418418
})
419419
})
@@ -643,6 +643,14 @@ describe('Fields', () => {
643643
collection,
644644
})
645645

646+
expect(result.items[0]).toMatchObject({
647+
subArray: [
648+
{
649+
text: subArrayText,
650+
},
651+
],
652+
text: 'test',
653+
})
646654
expect(result.items[0].subArray[0].text).toStrictEqual(subArrayText)
647655
})
648656

@@ -707,6 +715,15 @@ describe('Fields', () => {
707715
expect(document.group.defaultParent).toStrictEqual(groupDefaultValue)
708716
expect(document.group.defaultChild).toStrictEqual(groupDefaultChild)
709717
})
718+
719+
it('should not have duplicate keys', async () => {
720+
expect(document.arrayOfGroups[0]).toMatchObject({
721+
id: expect.any(String),
722+
groupItem: {
723+
text: 'Hello world',
724+
},
725+
})
726+
})
710727
})
711728

712729
describe('tabs', () => {

0 commit comments

Comments
 (0)