@payloadcms/plugin-nested-docs 3.20.0-canary.8eef7db → 3.20.0-canary.9f2bca1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resaveChildren.d.ts","sourceRoot":"","sources":["../../src/hooks/resaveChildren.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,gBAAgB,EAIjB,MAAM,SAAS,CAAA;AAIhB,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"resaveChildren.d.ts","sourceRoot":"","sources":["../../src/hooks/resaveChildren.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,gBAAgB,EAIjB,MAAM,SAAS,CAAA;AAIhB,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AA4EzD,eAAO,MAAM,cAAc,iBACV,sBAAsB,cAAc,gBAAgB,KAAG,yBAqBrE,CAAA"}
|
|
@@ -2,78 +2,46 @@ import { APIError } from 'payload';
|
|
|
2
2
|
import { populateBreadcrumbs } from '../utilities/populateBreadcrumbs.js';
|
|
3
3
|
const resave = async ({ collection, doc, draft, pluginConfig, req })=>{
|
|
4
4
|
const parentSlug = pluginConfig?.parentFieldSlug || 'parent';
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
req,
|
|
17
|
-
where: {
|
|
18
|
-
[parentSlug]: {
|
|
19
|
-
equals: doc.id
|
|
20
|
-
}
|
|
5
|
+
const parentDocIsPublished = doc._status === 'published';
|
|
6
|
+
const children = await req.payload.find({
|
|
7
|
+
collection: collection.slug,
|
|
8
|
+
depth: 0,
|
|
9
|
+
draft: true,
|
|
10
|
+
limit: 0,
|
|
11
|
+
locale: req.locale,
|
|
12
|
+
req,
|
|
13
|
+
where: {
|
|
14
|
+
[parentSlug]: {
|
|
15
|
+
equals: doc.id
|
|
21
16
|
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
req,
|
|
31
|
-
where: {
|
|
32
|
-
[parentSlug]: {
|
|
33
|
-
equals: doc.id
|
|
34
|
-
}
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs';
|
|
20
|
+
try {
|
|
21
|
+
for (const child of children.docs){
|
|
22
|
+
const childIsPublished = typeof collection.versions === 'object' && collection.versions.drafts && child._status === 'published';
|
|
23
|
+
if (!parentDocIsPublished && childIsPublished) {
|
|
24
|
+
continue;
|
|
35
25
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (a.updatedAt !== b.updatedAt) {
|
|
48
|
-
return a.updatedAt > b.updatedAt ? 1 : -1;
|
|
49
|
-
}
|
|
50
|
-
return a._status === 'published' ? 1 : -1;
|
|
26
|
+
await req.payload.update({
|
|
27
|
+
id: child.id,
|
|
28
|
+
collection: collection.slug,
|
|
29
|
+
data: {
|
|
30
|
+
...child,
|
|
31
|
+
[breadcrumbSlug]: await populateBreadcrumbs(req, pluginConfig, collection, child)
|
|
32
|
+
},
|
|
33
|
+
depth: 0,
|
|
34
|
+
draft: !childIsPublished,
|
|
35
|
+
locale: req.locale,
|
|
36
|
+
req
|
|
51
37
|
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
collection: collection.slug,
|
|
60
|
-
data: {
|
|
61
|
-
...child,
|
|
62
|
-
[breadcrumbSlug]: await populateBreadcrumbs(req, pluginConfig, collection, child)
|
|
63
|
-
},
|
|
64
|
-
depth: 0,
|
|
65
|
-
draft: isDraft,
|
|
66
|
-
locale: req.locale,
|
|
67
|
-
req
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
} catch (err) {
|
|
71
|
-
req.payload.logger.error(`Nested Docs plugin encountered an error while re-saving a child document.`);
|
|
72
|
-
req.payload.logger.error(err);
|
|
73
|
-
if (err?.name === 'ValidationError' && err?.data?.errors?.length) {
|
|
74
|
-
throw new APIError('Could not publish or save changes: One or more children are invalid.', 400);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
38
|
+
}
|
|
39
|
+
} catch (err) {
|
|
40
|
+
req.payload.logger.error(`Nested Docs plugin has had an error while re-saving a child document${draft ? ' as draft' : ' as published'}.`);
|
|
41
|
+
req.payload.logger.error(err);
|
|
42
|
+
// Use type assertion until we can use instanceof reliably with our Error types
|
|
43
|
+
if (err?.name === 'ValidationError' && err?.data?.errors?.length) {
|
|
44
|
+
throw new APIError('Could not publish or save changes: One or more children are invalid.', 400);
|
|
77
45
|
}
|
|
78
46
|
}
|
|
79
47
|
};
|
|
@@ -81,10 +49,19 @@ export const resaveChildren = (pluginConfig, collection)=>async ({ doc, req })=>
|
|
|
81
49
|
await resave({
|
|
82
50
|
collection,
|
|
83
51
|
doc,
|
|
84
|
-
draft:
|
|
52
|
+
draft: true,
|
|
85
53
|
pluginConfig,
|
|
86
54
|
req
|
|
87
55
|
});
|
|
56
|
+
if (doc._status === 'published') {
|
|
57
|
+
await resave({
|
|
58
|
+
collection,
|
|
59
|
+
doc,
|
|
60
|
+
draft: false,
|
|
61
|
+
pluginConfig,
|
|
62
|
+
req
|
|
63
|
+
});
|
|
64
|
+
}
|
|
88
65
|
return undefined;
|
|
89
66
|
};
|
|
90
67
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/resaveChildren.ts"],"sourcesContent":["import type {\n CollectionAfterChangeHook,\n CollectionConfig,\n JsonObject,\n PayloadRequest,\n ValidationError,\n} from 'payload'\n\nimport { APIError } from 'payload'\n\nimport type { NestedDocsPluginConfig } from '../types.js'\n\nimport { populateBreadcrumbs } from '../utilities/populateBreadcrumbs.js'\n\ntype ResaveArgs = {\n collection: CollectionConfig\n doc: JsonObject\n draft: boolean\n pluginConfig: NestedDocsPluginConfig\n req: PayloadRequest\n}\n\nconst resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs) => {\n const parentSlug = pluginConfig?.parentFieldSlug || 'parent'\n const
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/resaveChildren.ts"],"sourcesContent":["import type {\n CollectionAfterChangeHook,\n CollectionConfig,\n JsonObject,\n PayloadRequest,\n ValidationError,\n} from 'payload'\n\nimport { APIError } from 'payload'\n\nimport type { NestedDocsPluginConfig } from '../types.js'\n\nimport { populateBreadcrumbs } from '../utilities/populateBreadcrumbs.js'\n\ntype ResaveArgs = {\n collection: CollectionConfig\n doc: JsonObject\n draft: boolean\n pluginConfig: NestedDocsPluginConfig\n req: PayloadRequest\n}\n\nconst resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs) => {\n const parentSlug = pluginConfig?.parentFieldSlug || 'parent'\n const parentDocIsPublished = doc._status === 'published'\n const children = await req.payload.find({\n collection: collection.slug,\n depth: 0,\n draft: true,\n limit: 0,\n locale: req.locale,\n req,\n where: {\n [parentSlug]: {\n equals: doc.id,\n },\n },\n })\n\n const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs'\n\n try {\n for (const child of children.docs) {\n const childIsPublished =\n typeof collection.versions === 'object' &&\n collection.versions.drafts &&\n child._status === 'published'\n\n if (!parentDocIsPublished && childIsPublished) {\n continue\n }\n\n await req.payload.update({\n id: child.id,\n collection: collection.slug,\n data: {\n ...child,\n [breadcrumbSlug]: await populateBreadcrumbs(req, pluginConfig, collection, child),\n },\n depth: 0,\n draft: !childIsPublished,\n locale: req.locale,\n req,\n })\n }\n } catch (err: unknown) {\n req.payload.logger.error(\n `Nested Docs plugin has had an error while re-saving a child document${\n draft ? ' as draft' : ' as published'\n }.`,\n )\n req.payload.logger.error(err)\n\n // Use type assertion until we can use instanceof reliably with our Error types\n if (\n (err as ValidationError)?.name === 'ValidationError' &&\n (err as ValidationError)?.data?.errors?.length\n ) {\n throw new APIError(\n 'Could not publish or save changes: One or more children are invalid.',\n 400,\n )\n }\n }\n}\n\nexport const resaveChildren =\n (pluginConfig: NestedDocsPluginConfig, collection: CollectionConfig): CollectionAfterChangeHook =>\n async ({ doc, req }) => {\n await resave({\n collection,\n doc,\n draft: true,\n pluginConfig,\n req,\n })\n\n if (doc._status === 'published') {\n await resave({\n collection,\n doc,\n draft: false,\n pluginConfig,\n req,\n })\n }\n\n return undefined\n }\n"],"names":["APIError","populateBreadcrumbs","resave","collection","doc","draft","pluginConfig","req","parentSlug","parentFieldSlug","parentDocIsPublished","_status","children","payload","find","slug","depth","limit","locale","where","equals","id","breadcrumbSlug","breadcrumbsFieldSlug","child","docs","childIsPublished","versions","drafts","update","data","err","logger","error","name","errors","length","resaveChildren","undefined"],"mappings":"AAQA,SAASA,QAAQ,QAAQ,UAAS;AAIlC,SAASC,mBAAmB,QAAQ,sCAAqC;AAUzE,MAAMC,SAAS,OAAO,EAAEC,UAAU,EAAEC,GAAG,EAAEC,KAAK,EAAEC,YAAY,EAAEC,GAAG,EAAc;IAC7E,MAAMC,aAAaF,cAAcG,mBAAmB;IACpD,MAAMC,uBAAuBN,IAAIO,OAAO,KAAK;IAC7C,MAAMC,WAAW,MAAML,IAAIM,OAAO,CAACC,IAAI,CAAC;QACtCX,YAAYA,WAAWY,IAAI;QAC3BC,OAAO;QACPX,OAAO;QACPY,OAAO;QACPC,QAAQX,IAAIW,MAAM;QAClBX;QACAY,OAAO;YACL,CAACX,WAAW,EAAE;gBACZY,QAAQhB,IAAIiB,EAAE;YA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
{
|
|
2
2
|
"name": "@payloadcms/plugin-nested-docs",
|
|
3
|
-
"version": "3.20.0-canary.
|
|
3
|
+
"version": "3.20.0-canary.9f2bca1",
|
|
4
4
|
"description": "The official Nested Docs plugin for Payload",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -37,10 +37,10 @@
|
|
|
37
37
|
],
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@payloadcms/eslint-config": "3.9.0",
|
|
40
|
-
"payload": "3.20.0-canary.
|
|
40
|
+
"payload": "3.20.0-canary.9f2bca1"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"payload": "3.20.0-canary.
|
|
43
|
+
"payload": "3.20.0-canary.9f2bca1"
|
|
44
44
|
},
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"registry": "https://registry.npmjs.org/"
|