From 41d80a959cd9bba01cebfd3afb8d9c153e78bc10 Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Sun, 20 Feb 2022 02:08:41 -0500 Subject: [PATCH] fix: syncWithSearch args and return type --- packages/plugin-search/README.md | 8 ++++---- packages/plugin-search/demo/src/payload.config.ts | 6 +++--- .../plugin-search/src/Search/hooks/syncWithSearch.ts | 11 ++++++++--- packages/plugin-search/src/types.ts | 9 ++++++--- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/plugin-search/README.md b/packages/plugin-search/README.md index 90ffcb1999..8df72b3553 100644 --- a/packages/plugin-search/README.md +++ b/packages/plugin-search/README.md @@ -68,7 +68,7 @@ export default config; searchPlugin({ ... defaultPriorities: { - pages: (doc) => doc.title.startsWith('Hello, world!') ? 1 : 10, + pages: ({ doc }) => doc.title.startsWith('Hello, world!') ? 1 : 10, posts: 20 } }) @@ -95,10 +95,10 @@ export default config; ```js searchPlugin({ ... - beforeSync: ({ doc }) => ({ - ...doc, + beforeSync: ({ originalDoc, searchDoc }) => ({ + ...searchDoc, // Modify your docs in any way here, this can be async - excerpt: doc?.excerpt || 'This is a fallback excerpt' + excerpt: originalDoc?.excerpt || 'This is a fallback excerpt' }), }) ``` diff --git a/packages/plugin-search/demo/src/payload.config.ts b/packages/plugin-search/demo/src/payload.config.ts index e7ba914ff3..7180d8f4c8 100644 --- a/packages/plugin-search/demo/src/payload.config.ts +++ b/packages/plugin-search/demo/src/payload.config.ts @@ -51,9 +51,9 @@ export default buildConfig({ ] }, syncOnlyPublished: false, - beforeSync: ({ doc }) => ({ - ...doc, - excerpt: doc?.excerpt || 'This is a fallback excerpt' + beforeSync: ({ originalDoc, searchDoc }) => ({ + ...searchDoc, + excerpt: originalDoc?.excerpt || 'This is a fallback excerpt' }), defaultPriorities: { pages: 10, diff --git a/packages/plugin-search/src/Search/hooks/syncWithSearch.ts b/packages/plugin-search/src/Search/hooks/syncWithSearch.ts index f326f4836c..9c76d6399f 100644 --- a/packages/plugin-search/src/Search/hooks/syncWithSearch.ts +++ b/packages/plugin-search/src/Search/hooks/syncWithSearch.ts @@ -34,7 +34,8 @@ const syncWithSearch: SyncWithSearch = async (args) => { if (typeof beforeSync === 'function') { dataToSave = await beforeSync({ - doc: dataToSave, + originalDoc: doc, + searchDoc: dataToSave, payload }); } @@ -46,7 +47,12 @@ const syncWithSearch: SyncWithSearch = async (args) => { } = defaultPriorities; if (typeof priority === 'function') { - defaultPriority = await priority(dataToSave); + try { + defaultPriority = await priority(doc); + } catch (err) { + payload.logger.error(err); + payload.logger.error(`Error gathering default priority for search documents related to ${collection}`); + } } else { defaultPriority = priority; } @@ -134,7 +140,6 @@ const syncWithSearch: SyncWithSearch = async (args) => { } } else if (doSync) { try { - console.log('create'); payload.create({ collection: 'search', data: { diff --git a/packages/plugin-search/src/types.ts b/packages/plugin-search/src/types.ts index 5d94ed3f4b..6cc0bd8797 100644 --- a/packages/plugin-search/src/types.ts +++ b/packages/plugin-search/src/types.ts @@ -11,15 +11,18 @@ export type DocToSync = { } export type BeforeSync = (args: { - doc: DocToSync + originalDoc: { + [key: string]: any + } + searchDoc: DocToSync payload: Payload -}) => DocToSync; +}) => DocToSync | Promise; export type SearchConfig = { searchOverrides?: Partial collections?: string[] defaultPriorities?: { - [collection: string]: number | ((doc: DocToSync) => number) + [collection: string]: number | ((doc: any) => number | Promise) } beforeSync?: BeforeSync syncOnlyPublished?: boolean