fix: syncWithSearch args and return type

This commit is contained in:
Jacob Fletcher
2022-02-20 02:08:41 -05:00
parent 80da94c3e0
commit 41d80a959c
4 changed files with 21 additions and 13 deletions

View File

@@ -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'
}),
})
```

View File

@@ -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,

View File

@@ -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: {

View File

@@ -11,15 +11,18 @@ export type DocToSync = {
}
export type BeforeSync = (args: {
doc: DocToSync
originalDoc: {
[key: string]: any
}
searchDoc: DocToSync
payload: Payload
}) => DocToSync;
}) => DocToSync | Promise<DocToSync>;
export type SearchConfig = {
searchOverrides?: Partial<CollectionConfig>
collections?: string[]
defaultPriorities?: {
[collection: string]: number | ((doc: DocToSync) => number)
[collection: string]: number | ((doc: any) => number | Promise<number>)
}
beforeSync?: BeforeSync
syncOnlyPublished?: boolean