fix(plugin-nested-docs): sync write transaction errors (#4084)

This commit is contained in:
Dan Ribbens
2023-11-10 10:16:31 -05:00
committed by GitHub
parent 348a70cc33
commit 47efd3b92e
4 changed files with 39 additions and 33 deletions

View File

@@ -6,38 +6,38 @@ import populateBreadcrumbs from '../utilities/populateBreadcrumbs'
const resaveChildren =
(pluginConfig: PluginConfig, collection: CollectionConfig): CollectionAfterChangeHook =>
({ req: { payload, locale }, req, doc }) => {
async ({ doc, req: { locale, payload }, req }) => {
const resaveChildrenAsync = async (): Promise<void> => {
const children = await payload.find({
req,
collection: collection.slug,
depth: 0,
locale,
req,
where: {
parent: {
equals: doc.id,
},
},
depth: 0,
locale,
})
try {
children.docs.forEach((child: any) => {
children.docs.forEach(async (child: any) => {
const updateAsDraft =
typeof collection.versions === 'object' &&
collection.versions.drafts &&
child._status !== 'published'
payload.update({
req,
await payload.update({
id: child.id,
collection: collection.slug,
draft: updateAsDraft,
data: {
...child,
breadcrumbs: populateBreadcrumbs(req, pluginConfig, collection, child),
},
depth: 0,
draft: updateAsDraft,
locale,
req,
})
})
} catch (err: unknown) {
@@ -48,8 +48,7 @@ const resaveChildren =
}
}
// Non-blocking
resaveChildrenAsync()
await resaveChildrenAsync()
return undefined
}

View File

@@ -11,16 +11,16 @@ interface DocWithBreadcrumbs {
const resaveSelfAfterCreate =
(collection: CollectionConfig): CollectionAfterChangeHook =>
async ({ req, doc, operation }) => {
const { payload, locale } = req
async ({ doc, operation, req }) => {
const { locale, payload } = req
const { breadcrumbs = [] } = doc as DocWithBreadcrumbs
if (operation === 'create') {
const originalDocWithDepth0 = await payload.findByID({
req,
id: doc.id,
collection: collection.slug,
depth: 0,
id: doc.id,
req,
})
const updateAsDraft =
@@ -30,12 +30,8 @@ const resaveSelfAfterCreate =
try {
await payload.update({
req,
collection: collection.slug,
id: doc.id,
locale,
depth: 0,
draft: updateAsDraft,
collection: collection.slug,
data: {
...originalDocWithDepth0,
breadcrumbs:
@@ -44,6 +40,10 @@ const resaveSelfAfterCreate =
doc: breadcrumbs.length === i + 1 ? doc.id : crumb.doc,
})) || [],
},
depth: 0,
draft: updateAsDraft,
locale,
req,
})
} catch (err: unknown) {
payload.logger.error(

View File

@@ -8,17 +8,10 @@ import { seed } from './seed'
export default buildConfigWithDefaults({
collections: [Pages, Users],
localization: {
locales: ['en', 'es', 'de'],
defaultLocale: 'en',
fallback: true,
locales: ['en', 'es', 'de'],
},
plugins: [
nestedDocs({
collections: ['pages'],
generateLabel: (_, doc) => doc.title as string,
generateURL: (docs) => docs.reduce((url, doc) => `${url}/${doc.slug}`, ''),
}),
],
onInit: async (payload) => {
await payload.create({
collection: 'users',
@@ -30,4 +23,11 @@ export default buildConfigWithDefaults({
await seed(payload)
},
plugins: [
nestedDocs({
collections: ['pages'],
generateLabel: (_, doc) => doc.title as string,
generateURL: (docs) => docs.reduce((url, doc) => `${url}/${doc.slug}`, ''),
}),
],
})

View File

@@ -1,7 +1,9 @@
import type { Payload } from '../../../packages/payload/src'
import type { PayloadRequest } from '../../../packages/payload/src/express/types'
export const seed = async (payload: Payload): Promise<boolean> => {
payload.logger.info('Seeding data...')
const req = {} as PayloadRequest
try {
await payload.create({
@@ -10,40 +12,45 @@ export const seed = async (payload: Payload): Promise<boolean> => {
email: 'demo@payloadcms.com',
password: 'demo',
},
req,
})
const { id: parentID } = await payload.create({
collection: 'pages',
data: {
title: 'Parent page',
slug: 'parent-page',
title: 'Parent page',
},
req,
})
const { id: childID } = await payload.create({
collection: 'pages',
data: {
title: 'Child page',
slug: 'child-page',
parent: parentID,
slug: 'child-page',
title: 'Child page',
},
req,
})
await payload.create({
collection: 'pages',
data: {
title: 'Grandchild page',
slug: 'grandchild-page',
parent: childID,
slug: 'grandchild-page',
title: 'Grandchild page',
},
req,
})
await payload.create({
collection: 'pages',
data: {
title: 'Sister page',
slug: 'sister-page',
title: 'Sister page',
},
req,
})
return true
} catch (err) {