-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds recursion to richText field to populate relationship and u…
…pload nested fields
- Loading branch information
1 parent
e76f7c8
commit 42af22c
Showing
4 changed files
with
262 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable @typescript-eslint/no-use-before-define */ | ||
import { Collection } from '../../collections/config/types'; | ||
import { Payload } from '../..'; | ||
import { RichTextField, Field } from '../config/types'; | ||
import { PayloadRequest } from '../../express/types'; | ||
|
||
type Arguments = { | ||
data: unknown | ||
overrideAccess?: boolean | ||
depth: number | ||
currentDepth?: number | ||
payload: Payload | ||
field: RichTextField | ||
req: PayloadRequest | ||
showHiddenFields: boolean | ||
} | ||
|
||
export const populate = async ({ | ||
id, | ||
collection, | ||
data, | ||
overrideAccess, | ||
depth, | ||
currentDepth, | ||
payload, | ||
req, | ||
showHiddenFields, | ||
}: Omit<Arguments, 'field'> & { | ||
id: string, | ||
field: Field | ||
collection: Collection | ||
}): Promise<void> => { | ||
let dataRef = data as Record<string, unknown>; | ||
|
||
const doc = await payload.operations.collections.findByID({ | ||
req: { | ||
...req, | ||
payloadAPI: 'local', | ||
}, | ||
collection, | ||
id, | ||
currentDepth: currentDepth + 1, | ||
overrideAccess, | ||
disableErrors: true, | ||
depth, | ||
showHiddenFields, | ||
}); | ||
|
||
if (doc) { | ||
dataRef = doc; | ||
} else { | ||
dataRef = null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* eslint-disable @typescript-eslint/no-use-before-define */ | ||
import { Payload } from '../..'; | ||
import { Field, fieldHasSubFields, fieldIsArrayType, fieldAffectsData } from '../config/types'; | ||
import { PayloadRequest } from '../../express/types'; | ||
import { populate } from './populate'; | ||
import { recurseRichText } from './relationshipPromise'; | ||
|
||
type NestedRichTextFieldsArgs = { | ||
promises: Promise<void>[] | ||
data: unknown | ||
fields: Field[] | ||
req: PayloadRequest | ||
payload: Payload | ||
overrideAccess: boolean | ||
depth: number | ||
currentDepth?: number | ||
showHiddenFields: boolean | ||
} | ||
|
||
export const recurseNestedFields = ({ | ||
promises, | ||
data, | ||
fields, | ||
req, | ||
payload, | ||
overrideAccess = false, | ||
depth, | ||
currentDepth = 0, | ||
showHiddenFields, | ||
}: NestedRichTextFieldsArgs): void => { | ||
fields.forEach((field) => { | ||
if (field.type === 'relationship' || field.type === 'upload') { | ||
if (field.type === 'relationship') { | ||
if (field.hasMany && Array.isArray(data[field.name])) { | ||
if (Array.isArray(field.relationTo)) { | ||
data[field.name].forEach(({ relationTo, value }, i) => { | ||
const collection = payload.collections[relationTo]; | ||
if (collection) { | ||
promises.push(populate({ | ||
id: value, | ||
field, | ||
collection, | ||
data: data[field.name][i], | ||
overrideAccess, | ||
depth, | ||
currentDepth, | ||
payload, | ||
req, | ||
showHiddenFields, | ||
})); | ||
} | ||
}); | ||
} else { | ||
data[field.name].forEach((id, i) => { | ||
const collection = payload.collections[field.relationTo as string]; | ||
if (collection) { | ||
promises.push(populate({ | ||
id, | ||
field, | ||
collection, | ||
data: data[field.name][i], | ||
overrideAccess, | ||
depth, | ||
currentDepth, | ||
payload, | ||
req, | ||
showHiddenFields, | ||
})); | ||
} | ||
}); | ||
} | ||
} else if (Array.isArray(field.relationTo) && data[field.name]?.value && data[field.name]?.relationTo) { | ||
const collection = payload.collections[data[field.name].relationTo]; | ||
promises.push(populate({ | ||
id: data[field.name].value, | ||
field, | ||
collection, | ||
data: data[field.name].value, | ||
overrideAccess, | ||
depth, | ||
currentDepth, | ||
payload, | ||
req, | ||
showHiddenFields, | ||
})); | ||
} | ||
} else if (typeof data[field.name] !== undefined) { | ||
const collection = payload.collections[field.relationTo]; | ||
promises.push(populate({ | ||
id: data[field.name], | ||
field, | ||
collection, | ||
data: data[field.name], | ||
overrideAccess, | ||
depth, | ||
currentDepth, | ||
payload, | ||
req, | ||
showHiddenFields, | ||
})); | ||
} | ||
} else if (fieldHasSubFields(field) && !fieldIsArrayType(field)) { | ||
if (fieldAffectsData(field) && typeof data[field.name] === 'object') { | ||
recurseNestedFields({ | ||
promises, | ||
data: data[field.name], | ||
fields: field.fields, | ||
req, | ||
payload, | ||
overrideAccess, | ||
depth, | ||
currentDepth, | ||
showHiddenFields, | ||
}); | ||
} else { | ||
recurseNestedFields({ | ||
promises, | ||
data, | ||
fields: field.fields, | ||
req, | ||
payload, | ||
overrideAccess, | ||
depth, | ||
currentDepth, | ||
showHiddenFields, | ||
}); | ||
} | ||
} else if (Array.isArray(data[field.name])) { | ||
if (field.type === 'blocks') { | ||
data[field.name].forEach((row, i) => { | ||
const block = field.blocks.find(({ slug }) => slug === row?.blockType); | ||
if (block) { | ||
recurseNestedFields({ | ||
promises, | ||
data: data[field.name][i], | ||
fields: block.fields, | ||
req, | ||
payload, | ||
overrideAccess, | ||
depth, | ||
currentDepth, | ||
showHiddenFields, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
if (field.type === 'array') { | ||
data[field.name].forEach((_, i) => { | ||
recurseNestedFields({ | ||
promises, | ||
data: data[field.name][i], | ||
fields: field.fields, | ||
req, | ||
payload, | ||
overrideAccess, | ||
depth, | ||
currentDepth, | ||
showHiddenFields, | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
if (field.type === 'richText' && Array.isArray(data[field.name])) { | ||
data[field.name].forEach((node) => { | ||
if (Array.isArray(node.children)) { | ||
recurseRichText({ | ||
req, | ||
children: node.children, | ||
payload, | ||
overrideAccess, | ||
depth, | ||
currentDepth, | ||
field, | ||
promises, | ||
showHiddenFields, | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters