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.

afterChange "Create New Entry" in Collection

default discord avatar
kittabitlast year
1 1

Good Afternoon Everyone,

I might just be missing something in the documentation, but I'm running into an issue; I have let's say 10 different collections, but when something is added to one of these collections, I want to create a new entry into one called "ActivityFeed" as an example.

After some digging around myself, I attempted to use ChatGPT and Bard (even after feeding it Payload documentation - it wasn't great to say the least); ChatGPT tried to suggest something like the following:

  1. Import "ActivityFeed" Collection:
    import ActivityFeed from './ActivityFeed';

  2. Add function that could be used in hook for collection:

const afterChange: CollectionAfterChangeHook = async ({ doc, previousDoc, operation, req }) => {
  if (operation === 'create' || operation === 'update') {
    const activity = {
      timestamp: new Date(),
      type: operation === 'create' ? 'added' : 'updated',
      relatedItem: doc._id, // Use the document's _id as the related item
      user: doc.author, // Use the author from the document as the user
    };

    // Create a slug for the activity feed entry based on the related item's title
    const relatedItemTitle = doc.title || previousDoc.title;
    const slug = createSlug(relatedItemTitle);

    // Save the activity feed entry
    const activityFeedModel = req.models[ActivityFeed.slug];
    const activityFeed = await Payload.findOne(ActivityFeed.slug, {}, activityFeedModel);
    await Payload.create(ActivityFeed.slug, { ...activity, slug }, activityFeedModel, activityFeed);
  }

  return doc;
};

...or this...

const afterChange: CollectionAfterChangeHook = async ({
  doc,
  previousDoc,
  operation,
  req,
  model,
}) => {
  if (operation === 'create' || operation === 'update') {
    const activity = {
      timestamp: new Date(),
      type: operation === 'create' ? 'added' : 'updated',
      relatedItem: doc._id, // Use the document's _id as the related item
      user: doc.author, // Use the author from the document as the user
    };

    // Create a slug for the activity feed entry based on the related item's title
    const relatedItemTitle = doc.title || previousDoc.title;
    const slug = createSlug(relatedItemTitle);

    // Save the activity feed entry
    await Payload.adapters.default.create(
      ActivityFeed.slug,
      { ...activity, slug },
      model
    );
  }

  return doc;
};

I know this is all wrong, and I know I'm overlooking something - so if anyone could give me any guidance or just point me in the right direction, I'd REALLY appreciate it as I'm feeling a bit stuck at the moment. I know I'm just missing something along the lines of payload.create, but everything I try is just erroring out or making TypeScript very angry haha.

Thanks in advance y'all, I really appreciate any help!!!

Cheers,
-Nick

  • Selected Answer
    discord user avatar
    denolfe
    last year

    I would definitely recommend going to the docs before using anything like ChatGPT.

    What you're looking for is the Local API's create function. Here's some psuedo-code for you to work from:

    // Inside hook
    await req.payload.create({
      collection: "activity", // Activity collection slug
      data: {
          timestamp: new Date(),
          type: operation === 'create' ? 'added' : 'updated',
          relatedItem: doc.id, // Relation field to current collection 
          user: doc.author,  // Relation field to user collection
        },
    });
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..