I have a collection called 'Votes' where a user will vote on another location called 'Locations'. Each Vote will have a locationId and UserId. Would it be possible to update a total tally of votes in the related location collection after a vote document is created using a Hook? I'm not qutie sure if they're only intended for updating document in the same collection or not.
I'm thinking Sibling Data is what I need to use here:
import type { FieldHook } from 'payload/types';
// Field hook type is a generic that takes three arguments:
// 1: The document type
// 2: The value type
// 3: The sibling data type
type ExampleFieldHook = FieldHook<ExampleDocumentType, string, SiblingDataType>;
const exampleFieldHook: ExampleFieldHook = (args) => {
const {
value, // Typed as
string
as shown above
data, // Typed as a Partial of your ExampleDocumentType
siblingData, // Typed as a Partial of SiblingDataType
originalDoc, // Typed as ExampleDocumentType
operation,
req,
} = args;
// Do something here...
return value; // should return a string as typed above, undefined, or null
}
You could do this with a field hook or a collection hook. They can be used to update
othercollections, not just the current 👍
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.