Skip to content

How do I update an relation? #215

Answered by kettanaito
tarjei asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, @tarjei.

Here's how you can update a manyOf relation by pushing a new entity to the relational value:

const db = factory({
  post: {
    id: primaryKey(String),
    images: manyOf('image')
  },
  image: {
    id: primaryKey(String),
    url: String
  }
})

rest.post('/api/images', (req, res, ctx) => {
  // Create a new "image" entity.
  const newImage = db.image.create({
    url: req.body.url
  })

  // Update a certain post(s) by adding a new image
  // to the existing "post.images" relationship.
  const nextPost = db.post.update({
    where: { id: { equals: 'some-post' } },
    data: {
      images(prevImages) {
        // Grab the previous value of "post.images"
        // and add …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@tarjei
Comment options

Answer selected by tarjei
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants