@payloadcms/plugin-nested-docs 3.36.0-canary.9 → 3.36.0
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.
- package/dist/hooks/resaveChildren.d.ts.map +1 -1
- package/dist/hooks/resaveChildren.js +0 -1
- package/dist/hooks/resaveChildren.js.map +1 -1
- package/dist/hooks/resaveSelfAfterCreate.d.ts.map +1 -1
- package/dist/hooks/resaveSelfAfterCreate.js +17 -24
- package/dist/hooks/resaveSelfAfterCreate.js.map +1 -1
- package/package.json +3 -3
|
@@ -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;AAqGzD,eAAO,MAAM,cAAc,iBACV,sBAAsB,cAAc,gBAAgB,KAAG,yBAWrE,CAAA"}
|
|
@@ -2,7 +2,6 @@ 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 breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs';
|
|
6
5
|
if (draft) {
|
|
7
6
|
// If the parent is a draft, don't resave children
|
|
8
7
|
return;
|
|
@@ -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
|
|
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\n if (draft) {\n // If the parent is a draft, don't resave children\n return\n } else {\n const initialDraftChildren = 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 draftChildren = initialDraftChildren.docs.filter((child) => child._status === 'draft')\n\n const publishedChildren = await req.payload.find({\n collection: collection.slug,\n depth: 0,\n draft: false,\n limit: 0,\n locale: req.locale,\n req,\n where: {\n [parentSlug]: {\n equals: doc.id,\n },\n },\n })\n\n const childrenById = [...draftChildren, ...publishedChildren.docs].reduce<\n Record<string, JsonObject[]>\n >((acc, child) => {\n acc[child.id] = acc[child.id] || []\n acc[child.id]!.push(child)\n return acc\n }, {})\n\n const sortedChildren = Object.values(childrenById).flatMap((group: JsonObject[]) => {\n return group.sort((a, b) => {\n if (a.updatedAt !== b.updatedAt) {\n return a.updatedAt > b.updatedAt ? 1 : -1\n }\n return a._status === 'published' ? 1 : -1\n })\n })\n\n if (sortedChildren) {\n try {\n for (const child of sortedChildren) {\n const isDraft = child._status !== 'published'\n\n await req.payload.update({\n id: child.id,\n collection: collection.slug,\n data: populateBreadcrumbs(req, pluginConfig, collection, child),\n depth: 0,\n draft: isDraft,\n locale: req.locale,\n req,\n })\n }\n } catch (err: unknown) {\n req.payload.logger.error(\n `Nested Docs plugin encountered an error while re-saving a child document.`,\n )\n req.payload.logger.error(err)\n\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 }\n}\n\nexport const resaveChildren =\n (pluginConfig: NestedDocsPluginConfig, collection: CollectionConfig): CollectionAfterChangeHook =>\n async ({ doc, req }) => {\n await resave({\n collection,\n doc,\n draft: doc._status === 'published' ? false : true,\n pluginConfig,\n req,\n })\n\n return undefined\n }\n"],"names":["APIError","populateBreadcrumbs","resave","collection","doc","draft","pluginConfig","req","parentSlug","parentFieldSlug","initialDraftChildren","payload","find","slug","depth","limit","locale","where","equals","id","draftChildren","docs","filter","child","_status","publishedChildren","childrenById","reduce","acc","push","sortedChildren","Object","values","flatMap","group","sort","a","b","updatedAt","isDraft","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,cAAc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AClB,IAAI;gBACF,KAAK,MAAMP,SAASO,eAAgB;oBAClC,MAAMS,UAAUhB,MAAMC,OAAO,KAAK;oBAElC,MAAMjB,IAAII,OAAO,CAAC6B,MAAM,CAAC;wBACvBrB,IAAII,MAAMJ,EAAE;wBACZhB,YAAYA,WAAWU,IAAI;wBAC3B4B,MAAMxC,oBAAoBM,KAAKD,cAAcH,YAAYoB;wBACzDT,OAAO;wBACPT,OAAOkC;wBACPvB,QAAQT,IAAIS,MAAM;wBAClBT;oBACF;gBACF;YACF,EAAE,OAAOmC,KAAc;gBACrBnC,IAAII,OAAO,CAACgC,MAAM,CAACC,KAAK,CACtB,CAAC,yEAAyE,CAAC;gBAE7ErC,IAAII,OAAO,CAACgC,MAAM,CAACC,KAAK,CAACF;gBAEzB,IACE,AAACA,KAAyBG,SAAS,qBAClCH,KAAyBD,MAAMK,QAAQC,QACxC;oBACA,MAAM,IAAI/C,SACR,wEACA;gBAEJ;YACF;QACF;IACF;AACF;AAEA,OAAO,MAAMgD,iBACX,CAAC1C,cAAsCH,aACvC,OAAO,EAAEC,GAAG,EAAEG,GAAG,EAAE;QACjB,MAAML,OAAO;YACXC;YACAC;YACAC,OAAOD,IAAIoB,OAAO,KAAK,cAAc,QAAQ;YAC7ClB;YACAC;QACF;QAEA,OAAO0C;IACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resaveSelfAfterCreate.d.ts","sourceRoot":"","sources":["../../src/hooks/resaveSelfAfterCreate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE1E,OAAO,KAAK,EAAc,sBAAsB,EAAE,MAAM,aAAa,CAAA;AAKrE,eAAO,MAAM,qBAAqB,iBACjB,sBAAsB,cAAc,gBAAgB,KAAG,
|
|
1
|
+
{"version":3,"file":"resaveSelfAfterCreate.d.ts","sourceRoot":"","sources":["../../src/hooks/resaveSelfAfterCreate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE1E,OAAO,KAAK,EAAc,sBAAsB,EAAE,MAAM,aAAa,CAAA;AAKrE,eAAO,MAAM,qBAAqB,iBACjB,sBAAsB,cAAc,gBAAgB,KAAG,yBAqCrE,CAAA"}
|
|
@@ -1,39 +1,32 @@
|
|
|
1
1
|
// This hook automatically re-saves a document after it is created
|
|
2
2
|
// so that we can build its breadcrumbs with the newly created document's ID.
|
|
3
3
|
export const resaveSelfAfterCreate = (pluginConfig, collection)=>async ({ doc, operation, req })=>{
|
|
4
|
+
if (operation !== 'create') {
|
|
5
|
+
return undefined;
|
|
6
|
+
}
|
|
4
7
|
const { locale, payload } = req;
|
|
5
8
|
const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs';
|
|
6
9
|
const breadcrumbs = doc[breadcrumbSlug];
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
const updateAsDraft = typeof collection.versions === 'object' && collection.versions.drafts && doc._status !== 'published';
|
|
11
|
+
try {
|
|
12
|
+
await payload.update({
|
|
9
13
|
id: doc.id,
|
|
10
14
|
collection: collection.slug,
|
|
15
|
+
data: {
|
|
16
|
+
[breadcrumbSlug]: breadcrumbs?.map((crumb, i)=>({
|
|
17
|
+
...crumb,
|
|
18
|
+
doc: breadcrumbs.length === i + 1 ? doc.id : crumb.doc
|
|
19
|
+
})) || []
|
|
20
|
+
},
|
|
11
21
|
depth: 0,
|
|
22
|
+
draft: updateAsDraft,
|
|
23
|
+
locale,
|
|
12
24
|
req
|
|
13
25
|
});
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
id: doc.id,
|
|
18
|
-
collection: collection.slug,
|
|
19
|
-
data: {
|
|
20
|
-
...originalDocWithDepth0,
|
|
21
|
-
[breadcrumbSlug]: breadcrumbs?.map((crumb, i)=>({
|
|
22
|
-
...crumb,
|
|
23
|
-
doc: breadcrumbs.length === i + 1 ? doc.id : crumb.doc
|
|
24
|
-
})) || []
|
|
25
|
-
},
|
|
26
|
-
depth: 0,
|
|
27
|
-
draft: updateAsDraft,
|
|
28
|
-
locale,
|
|
29
|
-
req
|
|
30
|
-
});
|
|
31
|
-
} catch (err) {
|
|
32
|
-
payload.logger.error(`Nested Docs plugin has had an error while adding breadcrumbs during document creation.`);
|
|
33
|
-
payload.logger.error(err);
|
|
34
|
-
}
|
|
26
|
+
} catch (err) {
|
|
27
|
+
payload.logger.error(`Nested Docs plugin has had an error while adding breadcrumbs during document creation.`);
|
|
28
|
+
payload.logger.error(err);
|
|
35
29
|
}
|
|
36
|
-
return undefined;
|
|
37
30
|
};
|
|
38
31
|
|
|
39
32
|
//# sourceMappingURL=resaveSelfAfterCreate.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/resaveSelfAfterCreate.ts"],"sourcesContent":["import type { CollectionAfterChangeHook, CollectionConfig } from 'payload'\n\nimport type { Breadcrumb, NestedDocsPluginConfig } from '../types.js'\n\n// This hook automatically re-saves a document after it is created\n// so that we can build its breadcrumbs with the newly created document's ID.\n\nexport const resaveSelfAfterCreate =\n (pluginConfig: NestedDocsPluginConfig, collection: CollectionConfig): CollectionAfterChangeHook =>\n async ({ doc, operation, req }) => {\n const { locale, payload } = req\n const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs'\n const breadcrumbs = doc[breadcrumbSlug] as unknown as Breadcrumb[]\n\n
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/resaveSelfAfterCreate.ts"],"sourcesContent":["import type { CollectionAfterChangeHook, CollectionConfig } from 'payload'\n\nimport type { Breadcrumb, NestedDocsPluginConfig } from '../types.js'\n\n// This hook automatically re-saves a document after it is created\n// so that we can build its breadcrumbs with the newly created document's ID.\n\nexport const resaveSelfAfterCreate =\n (pluginConfig: NestedDocsPluginConfig, collection: CollectionConfig): CollectionAfterChangeHook =>\n async ({ doc, operation, req }) => {\n if (operation !== 'create') {\n return undefined\n }\n\n const { locale, payload } = req\n const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs'\n const breadcrumbs = doc[breadcrumbSlug] as unknown as Breadcrumb[]\n\n const updateAsDraft =\n typeof collection.versions === 'object' &&\n collection.versions.drafts &&\n doc._status !== 'published'\n\n try {\n await payload.update({\n id: doc.id,\n collection: collection.slug,\n data: {\n [breadcrumbSlug]:\n breadcrumbs?.map((crumb, i) => ({\n ...crumb,\n doc: breadcrumbs.length === i + 1 ? doc.id : crumb.doc,\n })) || [],\n },\n depth: 0,\n draft: updateAsDraft,\n locale,\n req,\n })\n } catch (err: unknown) {\n payload.logger.error(\n `Nested Docs plugin has had an error while adding breadcrumbs during document creation.`,\n )\n payload.logger.error(err)\n }\n }\n"],"names":["resaveSelfAfterCreate","pluginConfig","collection","doc","operation","req","undefined","locale","payload","breadcrumbSlug","breadcrumbsFieldSlug","breadcrumbs","updateAsDraft","versions","drafts","_status","update","id","slug","data","map","crumb","i","length","depth","draft","err","logger","error"],"mappings":"AAIA,kEAAkE;AAClE,6EAA6E;AAE7E,OAAO,MAAMA,wBACX,CAACC,cAAsCC,aACvC,OAAO,EAAEC,GAAG,EAAEC,SAAS,EAAEC,GAAG,EAAE;QAC5B,IAAID,cAAc,UAAU;YAC1B,OAAOE;QACT;QAEA,MAAM,EAAEC,MAAM,EAAEC,OAAO,EAAE,GAAGH;QAC5B,MAAMI,iBAAiBR,aAAaS,oBAAoB,IAAI;QAC5D,MAAMC,cAAcR,GAAG,CAACM,eAAe;QAEvC,MAAMG,gBACJ,OAAOV,WAAWW,QAAQ,KAAK,YAC/BX,WAAWW,QAAQ,CAACC,MAAM,IAC1BX,IAAIY,OAAO,KAAK;QAElB,IAAI;YACF,MAAMP,QAAQQ,MAAM,CAAC;gBACnBC,IAAId,IAAIc,EAAE;gBACVf,YAAYA,WAAWgB,IAAI;gBAC3BC,MAAM;oBACJ,CAACV,eAAe,EACdE,aAAaS,IAAI,CAACC,OAAOC,IAAO,CAAA;4BAC9B,GAAGD,KAAK;4BACRlB,KAAKQ,YAAYY,MAAM,KAAKD,IAAI,IAAInB,IAAIc,EAAE,GAAGI,MAAMlB,GAAG;wBACxD,CAAA,MAAO,EAAE;gBACb;gBACAqB,OAAO;gBACPC,OAAOb;gBACPL;gBACAF;YACF;QACF,EAAE,OAAOqB,KAAc;YACrBlB,QAAQmB,MAAM,CAACC,KAAK,CAClB,CAAC,sFAAsF,CAAC;YAE1FpB,QAAQmB,MAAM,CAACC,KAAK,CAACF;QACvB;IACF,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/plugin-nested-docs",
|
|
3
|
-
"version": "3.36.0
|
|
3
|
+
"version": "3.36.0",
|
|
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.28.0",
|
|
40
|
-
"payload": "3.36.0
|
|
40
|
+
"payload": "3.36.0"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"payload": "3.36.0
|
|
43
|
+
"payload": "3.36.0"
|
|
44
44
|
},
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"registry": "https://registry.npmjs.org/"
|