Alternative ways to update multiple objects


#1

I am trying to find a workaround for implementation of updating multiple objects in one request. I am aware of issue github issue #795 and also read comment here. I am trying to implement what krainboltgreene said.

I have an object errand in database where it has a field dirty default as false. I need a request to bulk update dirty field for many rows of object errand

In single update:

PATCH /errands/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": {
  "type": "errands",
  "id": "1",
  "attributes": {
    "dirty": true
  }
}

For multiple objects update I am planning to do something as:

POST /errand_dirty_list/0/relationships/errands HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": [
    { "type": "errands", "id": "123" },
    { "type": "errands", "id": "124" }
  ]
}

Or may be do something like this:

POST /errand_dirty_list HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": {
  "type": "errand_dirty_list",
  "attributes": {
    "dirty_errands": [123, 124, 125]
  }
}

For both workaround cases, I need to response a fake resource identifier object id because my database do not have the object errand_dirty_list.

My question is is this way of workaround to implement multiple object update as single request still valid as JSONAPI specification? If not, is there any other way that still work and compliance with current JSONAPI specification?


Delete multiple resources using filtered collection
#2

Here’s a thought, why not just manage it yourself as an async resource?

EDIT: Ok, actually read the comment and I think that is exactly his suggestion. Maybe without the async part.

The way I would approach this is to create a resource which represents a bulk operation, maybe not even on the errand resource. I wouldn’t concern myself with what the operation is, the resource representation should give that information.

/bulktransactions/.....

That way your payload could be something like this:

{
  "data": {
    "type": "bulktransaction",
    "attributes": {
      "resources": [{"type":"errands"}],
      "operations": [
        {"id":123,"method":"PATCH", "payload":1},
        {"id":124,"method":"PATCH", "payload":1},
        {"id":125,"method":"PATCH","payload":1}
      ],
      "payload":[
        {"id":1,
          "value":{
            "data": {
              "type": "errands",
              "attributes": {
                "dirty": true
              }
            }
          }
        }
      ]
    }
  }
}

You could return a location header for the bulk transaction resource itself, and get all the info and updates you needed from there.


POST collection of entities
#3

Look like what you recommended is more alike of the recommended way. Thank you!