1
- import type { DocumentEvent } from 'payload'
2
- import type { fieldSchemaToJSON } from 'payload/shared'
1
+ import type { DocumentEvent , FieldSchemaJSON } from 'payload'
3
2
4
3
import type { PopulationsByCollection } from './types.js'
5
4
6
5
import { traverseRichText } from './traverseRichText.js'
7
6
8
- export const traverseFields = < T > ( args : {
7
+ export const traverseFields = < T extends Record < string , any > > ( args : {
9
8
externallyUpdatedRelationship ?: DocumentEvent
10
- fieldSchema : ReturnType < typeof fieldSchemaToJSON >
9
+ fieldSchema : FieldSchemaJSON
11
10
incomingData : T
12
11
localeChanged : boolean
13
12
populationsByCollection : PopulationsByCollection
14
- result : T
13
+ result : Record < string , any >
15
14
} ) : void => {
16
15
const {
17
16
externallyUpdatedRelationship,
@@ -48,7 +47,7 @@ export const traverseFields = <T>(args: {
48
47
49
48
traverseFields ( {
50
49
externallyUpdatedRelationship,
51
- fieldSchema : fieldSchema . fields ,
50
+ fieldSchema : fieldSchema . fields ! ,
52
51
incomingData : incomingRow ,
53
52
localeChanged,
54
53
populationsByCollection,
@@ -64,7 +63,7 @@ export const traverseFields = <T>(args: {
64
63
case 'blocks' :
65
64
if ( Array . isArray ( incomingData [ fieldName ] ) ) {
66
65
result [ fieldName ] = incomingData [ fieldName ] . map ( ( incomingBlock , i ) => {
67
- const incomingBlockJSON = fieldSchema . blocks [ incomingBlock . blockType ]
66
+ const incomingBlockJSON = fieldSchema . blocks ?. [ incomingBlock . blockType ]
68
67
69
68
if ( ! result [ fieldName ] ) {
70
69
result [ fieldName ] = [ ]
@@ -82,7 +81,7 @@ export const traverseFields = <T>(args: {
82
81
83
82
traverseFields ( {
84
83
externallyUpdatedRelationship,
85
- fieldSchema : incomingBlockJSON . fields ,
84
+ fieldSchema : incomingBlockJSON ! . fields ! ,
86
85
incomingData : incomingBlock ,
87
86
localeChanged,
88
87
populationsByCollection,
@@ -106,7 +105,7 @@ export const traverseFields = <T>(args: {
106
105
107
106
traverseFields ( {
108
107
externallyUpdatedRelationship,
109
- fieldSchema : fieldSchema . fields ,
108
+ fieldSchema : fieldSchema . fields ! ,
110
109
incomingData : incomingData [ fieldName ] || { } ,
111
110
localeChanged,
112
111
populationsByCollection,
@@ -166,11 +165,11 @@ export const traverseFields = <T>(args: {
166
165
incomingRelation === externallyUpdatedRelationship ?. id
167
166
168
167
if ( hasChanged || hasUpdated || localeChanged ) {
169
- if ( ! populationsByCollection [ fieldSchema . relationTo ] ) {
170
- populationsByCollection [ fieldSchema . relationTo ] = [ ]
168
+ if ( ! populationsByCollection [ fieldSchema . relationTo ! ] ) {
169
+ populationsByCollection [ fieldSchema . relationTo ! ] = [ ]
171
170
}
172
171
173
- populationsByCollection [ fieldSchema . relationTo ] . push ( {
172
+ populationsByCollection [ fieldSchema . relationTo ! ] ? .push ( {
174
173
id : incomingRelation ,
175
174
accessor : i ,
176
175
ref : result [ fieldName ] ,
@@ -265,11 +264,11 @@ export const traverseFields = <T>(args: {
265
264
// if the new value is not empty, populate it
266
265
// otherwise set the value to null
267
266
if ( newID ) {
268
- if ( ! populationsByCollection [ fieldSchema . relationTo ] ) {
269
- populationsByCollection [ fieldSchema . relationTo ] = [ ]
267
+ if ( ! populationsByCollection [ fieldSchema . relationTo ! ] ) {
268
+ populationsByCollection [ fieldSchema . relationTo ! ] = [ ]
270
269
}
271
270
272
- populationsByCollection [ fieldSchema . relationTo ] . push ( {
271
+ populationsByCollection [ fieldSchema . relationTo ! ] ? .push ( {
273
272
id : newID ,
274
273
accessor : fieldName ,
275
274
ref : result as Record < string , unknown > ,
0 commit comments