Search code examples
ravendbravendb-http

Can I POST multiple patch requests to RavenDB's /bulk_docs HTTP API endpoint?


I'm in the process of writing a node wrapper for RavenDB.

I'm using version 3 but as there are no HTTP docs for it, I've been relying on the 2.0 and 2.5 docs.

In regards to single document operations, I've used this doc page successfully for PUTs, DELETEs and multiple PATCHs to individual documents.

Similarly, I've used this doc page successfully for multiple PUTs and DELETEs of several documents in one HTTP call but the docs are a bit vague in regards to PATCHing mutliple documents in one call.

Under the "Batching Requests" heading, it clearly states it's possible:

Request batching in RavenDB is handled using the '/bulk_docs' endpoint, which accepts an array of operations to execute. The format for the operations is:

method - PUT, PATCH or DELETE.

...

For PUTs, I POST to /bulk_docs:

[
  {
    Method: 'PUT',
    Key: 'users/1',
    Document: { username: 'dummy' }
    Metadata: { 'Raven-Entity-Type': 'Users' }
  },
  ...
]

For DELETEs, I POST to /bulk_docs:

[
  {
    Method: 'DELETE',
    Key: 'users/1'
  },
  ...
]

For PATCHs, I've tried POSTing the following without any luck:

[
  {
    Method: 'PATCH',
    Key: 'users/1',
    Document: {
      Type: 'Set',
      Name:'username',
      Value: 'new-username'
    }
  },
  ...
]

and

[
  {
    Method: 'PATCH',
    Key: 'users/1',
    Type: 'Set',
    Name:'username',
    Value: 'new-username'
  },
  ...
]

All I'm getting back is 500 - Internal Server Error and without any examples of PATCHing multiple documents on that docs page I'm kind of stuck...

Any help would be appreciated :)


Solution

  • The structure for PATCH is :

    [
      {
        Method: 'PATCH',
        Key: 'users/1',
        Patches: [{
    Type: 'Set',
            Name:'username',
            Value: 'new-username'
    }]
      },
      ...
    ]
    

    The full structure can be see here: https://github.com/ayende/ravendb/blob/master/Raven.Abstractions/Commands/PatchCommandData.cs#L72