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.

Is there a shorthand way of updating a relationship/array field with an `update` call?

default discord avatar
tinoutilast year
3

Assuming the following

messages

field in my

threads

collection:


{
    name: 'messages',
    type: 'relationship',
    relationTo: 'messages',
    hasMany: true,
    required: true,
    min: 1,
},


Is there a shorthand and/or better way of doing this?



const thread = await payload.findByID({
  collection: 'threads',
  id: message.thread.id,
  depth: 0,
});
const update = await payload.update({
  collection: 'threads',
  id: message.thread.id,
  data: {
    messages: [ ...thread.messages, message.id ],
  }
});


And similarly for removing an item, or is fetching the doc first to get the field value and do the addition/removal myself the only way?

  • That’s currently the only way to do it. Get the full array, mutate it, then update doc

  • default discord avatar
    tinoutilast year

    Gotcha, wanted to make sure I wasn't missing a hidden method to update it directly. Thanks Jarrod! 🙏

  • default discord avatar
    superwafflepuffslast year

    Any possibility of us getting access to something that scales better? I'm trying to use this for a memberships section.

    Organizations { members: Relationship<Users> }

    and in order to make access handling easier I also want to store an array of memberships on the user object

    Users { memberships: Relationship<Organizations> }

    Would be great if we could get access to all the power of mongo $push, $pull, etc...

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..