Skip to content

Commit 3501d47

Browse files
authored
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.
1 parent 4aaef5e commit 3501d47

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

packages/plugin-nested-docs/src/hooks/resaveChildren.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs)
2727
collection: collection.slug,
2828
depth: 0,
2929
draft: true,
30+
limit: 0,
3031
locale: req.locale,
3132
req,
3233
where: {
@@ -39,16 +40,14 @@ const resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs)
3940
const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs'
4041

4142
try {
42-
await children.docs.reduce(async (priorSave, child) => {
43-
await priorSave
44-
43+
for (const child of children.docs) {
4544
const childIsPublished =
4645
typeof collection.versions === 'object' &&
4746
collection.versions.drafts &&
4847
child._status === 'published'
4948

5049
if (!parentDocIsPublished && childIsPublished) {
51-
return
50+
continue
5251
}
5352

5453
await req.payload.update({
@@ -63,7 +62,7 @@ const resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs)
6362
locale: req.locale,
6463
req,
6564
})
66-
}, Promise.resolve())
65+
}
6766
} catch (err: unknown) {
6867
req.payload.logger.error(
6968
`Nested Docs plugin has had an error while re-saving a child document${

packages/plugin-nested-docs/src/utilities/getParents.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { CollectionConfig } from 'payload'
1+
import type { CollectionConfig, PayloadRequest } from 'payload'
22

33
import type { NestedDocsPluginConfig } from '../types.js'
44

55
export const getParents = async (
6-
req: any,
6+
req: PayloadRequest,
77
pluginConfig: NestedDocsPluginConfig,
88
collection: CollectionConfig,
99
doc: Record<string, unknown>,

test/plugin-nested-docs/int.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'path'
44
import { fileURLToPath } from 'url'
55

66
import { initPayloadInt } from '../helpers/initPayloadInt.js'
7+
import { Page } from './payload-types.js'
78

89
let payload: Payload
910

@@ -52,6 +53,62 @@ describe('@payloadcms/plugin-nested-docs', () => {
5253
'/parent-page/child-page/grandchild-page',
5354
)
5455
})
56+
57+
it('should update more than 10 (default limit) breadcrumbs', async () => {
58+
// create a parent doc
59+
const parentDoc = await payload.create({
60+
collection: 'pages',
61+
data: {
62+
title: '11 children',
63+
slug: '11-children',
64+
},
65+
})
66+
67+
// create 11 children docs
68+
for (let i = 0; i < 11; i++) {
69+
await payload.create({
70+
collection: 'pages',
71+
data: {
72+
title: `Child ${i + 1}`,
73+
slug: `child-${i + 1}`,
74+
parent: parentDoc.id,
75+
},
76+
})
77+
}
78+
79+
// update parent doc
80+
await payload.update({
81+
collection: 'pages',
82+
id: parentDoc.id,
83+
data: {
84+
title: '11 children updated',
85+
slug: '11-children-updated',
86+
},
87+
})
88+
89+
// read children docs
90+
const { docs } = await payload.find({
91+
collection: 'pages',
92+
limit: 0,
93+
draft: true,
94+
where: {
95+
parent: {
96+
equals: parentDoc.id,
97+
},
98+
},
99+
})
100+
101+
const firstUpdatedChildBreadcrumbs = docs[0]?.breadcrumbs as Page['breadcrumbs']
102+
const lastUpdatedChildBreadcrumbs = docs[10]?.breadcrumbs as Page['breadcrumbs']
103+
104+
expect(firstUpdatedChildBreadcrumbs).toHaveLength(2)
105+
// @ts-ignore
106+
expect(firstUpdatedChildBreadcrumbs[0].url).toStrictEqual('/11-children-updated')
107+
108+
expect(firstUpdatedChildBreadcrumbs).toBeDefined()
109+
// @ts-ignore
110+
expect(lastUpdatedChildBreadcrumbs[0].url).toStrictEqual('/11-children-updated')
111+
})
55112
})
56113

57114
describe('overrides', () => {

0 commit comments

Comments
 (0)