Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Update other collections with hook

default discord avatar
buzzworthy7last year
3

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


}

  • discord user avatar
    denolfe
    last year

    You could do this with a field hook or a collection hook. They can be used to update

    other

    collections, not just the current 👍



    https://payloadcms.com/docs/hooks/collections#afterchange
  • default discord avatar
    justono10 months ago

    Hello. This is a pretty old thread I have a similar desire as the vote things described by Will. My case is when an Asset collection item is updated I want to create a AssetHistory record, stamping the Asset's data at a point in time throughout it's life. The afterChange hook is what I need on the Asset Collection and I understand how to create the afterChange trigger. BUT my question in regard to this subject is do you just straight up make axios/fetch calls to the graphql or REST api to add new records to other collections or is there a fancy payload package or utility to do this?



    Apllogies I'm just new to payload. I recently had a demo by your team at our office and I'm building an internal tool with Payload atm to check it out!. I think though I would likely use the

    Local API

    now reading the docs more

    https://payloadcms.com/docs/local-api/overview

    my main goal is just follow best practice for payload dev. I feel like this is the right way to go about updating a different collection from a afterChange hook?

  • default discord avatar
    brianjm10 months ago

    You are correct.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team..