fix(plugin-nested-docs): cannot update more than 10 child docs (#10737)
`limit` was defaulting to 10 in the call to find children that need to be updated, now limit is 0 to get all children docs.
This commit is contained in:
@@ -27,6 +27,7 @@ const resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs)
|
||||
collection: collection.slug,
|
||||
depth: 0,
|
||||
draft: true,
|
||||
limit: 0,
|
||||
locale: req.locale,
|
||||
req,
|
||||
where: {
|
||||
@@ -39,16 +40,14 @@ const resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs)
|
||||
const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs'
|
||||
|
||||
try {
|
||||
await children.docs.reduce(async (priorSave, child) => {
|
||||
await priorSave
|
||||
|
||||
for (const child of children.docs) {
|
||||
const childIsPublished =
|
||||
typeof collection.versions === 'object' &&
|
||||
collection.versions.drafts &&
|
||||
child._status === 'published'
|
||||
|
||||
if (!parentDocIsPublished && childIsPublished) {
|
||||
return
|
||||
continue
|
||||
}
|
||||
|
||||
await req.payload.update({
|
||||
@@ -63,7 +62,7 @@ const resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs)
|
||||
locale: req.locale,
|
||||
req,
|
||||
})
|
||||
}, Promise.resolve())
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
req.payload.logger.error(
|
||||
`Nested Docs plugin has had an error while re-saving a child document${
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { CollectionConfig } from 'payload'
|
||||
import type { CollectionConfig, PayloadRequest } from 'payload'
|
||||
|
||||
import type { NestedDocsPluginConfig } from '../types.js'
|
||||
|
||||
export const getParents = async (
|
||||
req: any,
|
||||
req: PayloadRequest,
|
||||
pluginConfig: NestedDocsPluginConfig,
|
||||
collection: CollectionConfig,
|
||||
doc: Record<string, unknown>,
|
||||
|
||||
@@ -4,6 +4,7 @@ import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
import { initPayloadInt } from '../helpers/initPayloadInt.js'
|
||||
import { Page } from './payload-types.js'
|
||||
|
||||
let payload: Payload
|
||||
|
||||
@@ -52,6 +53,62 @@ describe('@payloadcms/plugin-nested-docs', () => {
|
||||
'/parent-page/child-page/grandchild-page',
|
||||
)
|
||||
})
|
||||
|
||||
it('should update more than 10 (default limit) breadcrumbs', async () => {
|
||||
// create a parent doc
|
||||
const parentDoc = await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
title: '11 children',
|
||||
slug: '11-children',
|
||||
},
|
||||
})
|
||||
|
||||
// create 11 children docs
|
||||
for (let i = 0; i < 11; i++) {
|
||||
await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
title: `Child ${i + 1}`,
|
||||
slug: `child-${i + 1}`,
|
||||
parent: parentDoc.id,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// update parent doc
|
||||
await payload.update({
|
||||
collection: 'pages',
|
||||
id: parentDoc.id,
|
||||
data: {
|
||||
title: '11 children updated',
|
||||
slug: '11-children-updated',
|
||||
},
|
||||
})
|
||||
|
||||
// read children docs
|
||||
const { docs } = await payload.find({
|
||||
collection: 'pages',
|
||||
limit: 0,
|
||||
draft: true,
|
||||
where: {
|
||||
parent: {
|
||||
equals: parentDoc.id,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const firstUpdatedChildBreadcrumbs = docs[0]?.breadcrumbs as Page['breadcrumbs']
|
||||
const lastUpdatedChildBreadcrumbs = docs[10]?.breadcrumbs as Page['breadcrumbs']
|
||||
|
||||
expect(firstUpdatedChildBreadcrumbs).toHaveLength(2)
|
||||
// @ts-ignore
|
||||
expect(firstUpdatedChildBreadcrumbs[0].url).toStrictEqual('/11-children-updated')
|
||||
|
||||
expect(firstUpdatedChildBreadcrumbs).toBeDefined()
|
||||
// @ts-ignore
|
||||
expect(lastUpdatedChildBreadcrumbs[0].url).toStrictEqual('/11-children-updated')
|
||||
})
|
||||
})
|
||||
|
||||
describe('overrides', () => {
|
||||
|
||||
Reference in New Issue
Block a user