Updating multiple objects in single request


#1

About updating objects, the spec says:

The PATCH request MUST include a single resource object as primary data.

I’m personally against updating multiple objects in a single request, but there are times when you want to to accomplish this. For example, a multi-select UI may allow a user to set the same attribute on multiple records.

Has there been a discussion about batch updates anywhere?


#2

#3

ah, awesome. thanks! :smile:


#4

My team came up with the idea to support included in POST and PATCH requests.

For instance, when updating a child that has an effect on one of the parent’s values.

In our case, a change to one of the children’s values changes values on the parent’s attributes at the top-level as well as values within another set of the parent’s children, a sibling array.

We chose to include the parent’s changes in the request.

PATCH

{
  "data":{
    "id":"1",
    "type":"expense",
    "attributes":{
      "amount":25
    }
  },
  "included":[
    {
      "id":"43",
      "type":"report",
      "attributes":{
        "amount":100,
        "allocations":[
          {
            "percent":60,
            "amount":60
          },
          {
            "percent":40,
            "amount":40
          }
        ]
      }
    }
  ]
}

In this case, the change to the expense resource’s amount changes the amount of the report resource, which also changes the amount on the report resource’s allocations objects, which get adjusted according to their percentages.

*Interestingly, I realized after doing this, that as long as you provide a type and id in the objects provided in the included array, you could provide the server with any resource change, regardless of an existing relationship as long as the server knows how to handle the request.