perf(plugin-nested-docs): remove extra find call (#12224)
Reduce query by combining find and update into one local api call.
This commit is contained in:
@@ -22,7 +22,6 @@ type ResaveArgs = {
|
||||
|
||||
const resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs) => {
|
||||
const parentSlug = pluginConfig?.parentFieldSlug || 'parent'
|
||||
const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs'
|
||||
|
||||
if (draft) {
|
||||
// If the parent is a draft, don't resave children
|
||||
|
||||
@@ -8,47 +8,39 @@ import type { Breadcrumb, NestedDocsPluginConfig } from '../types.js'
|
||||
export const resaveSelfAfterCreate =
|
||||
(pluginConfig: NestedDocsPluginConfig, collection: CollectionConfig): CollectionAfterChangeHook =>
|
||||
async ({ doc, operation, req }) => {
|
||||
if (operation !== 'create') {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const { locale, payload } = req
|
||||
const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs'
|
||||
const breadcrumbs = doc[breadcrumbSlug] as unknown as Breadcrumb[]
|
||||
|
||||
if (operation === 'create') {
|
||||
const originalDocWithDepth0 = await payload.findByID({
|
||||
const updateAsDraft =
|
||||
typeof collection.versions === 'object' &&
|
||||
collection.versions.drafts &&
|
||||
doc._status !== 'published'
|
||||
|
||||
try {
|
||||
await payload.update({
|
||||
id: doc.id,
|
||||
collection: collection.slug,
|
||||
data: {
|
||||
[breadcrumbSlug]:
|
||||
breadcrumbs?.map((crumb, i) => ({
|
||||
...crumb,
|
||||
doc: breadcrumbs.length === i + 1 ? doc.id : crumb.doc,
|
||||
})) || [],
|
||||
},
|
||||
depth: 0,
|
||||
draft: updateAsDraft,
|
||||
locale,
|
||||
req,
|
||||
})
|
||||
|
||||
const updateAsDraft =
|
||||
typeof collection.versions === 'object' &&
|
||||
collection.versions.drafts &&
|
||||
doc._status !== 'published'
|
||||
|
||||
try {
|
||||
await payload.update({
|
||||
id: doc.id,
|
||||
collection: collection.slug,
|
||||
data: {
|
||||
...originalDocWithDepth0,
|
||||
[breadcrumbSlug]:
|
||||
breadcrumbs?.map((crumb, i) => ({
|
||||
...crumb,
|
||||
doc: breadcrumbs.length === i + 1 ? doc.id : crumb.doc,
|
||||
})) || [],
|
||||
},
|
||||
depth: 0,
|
||||
draft: updateAsDraft,
|
||||
locale,
|
||||
req,
|
||||
})
|
||||
} catch (err: unknown) {
|
||||
payload.logger.error(
|
||||
`Nested Docs plugin has had an error while adding breadcrumbs during document creation.`,
|
||||
)
|
||||
payload.logger.error(err)
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
payload.logger.error(
|
||||
`Nested Docs plugin has had an error while adding breadcrumbs during document creation.`,
|
||||
)
|
||||
payload.logger.error(err)
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user