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

default discord avatar
Tinouti
last month
2

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?

  • discord user avatar
    Jarrod
    Payload Team
    last month

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

  • default discord avatar
    Tinouti
    last month

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

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.