2
votes

I'm working on syncing a PouchDB database (with Angular) with a CouchDB database.

When the replication is in progress, the code is issuing a POST request to do a bulk update to http://127.0.0.1:5984/testdb/_bulk_docs.

I have a validation rule on database to reject unauthorized writes, and it generates a forbidden error. Therefore, the server is responding with a JSON response as [{"id":"0951db944e729c981ad3964c22002d55","rev":"8-ccdcb52743cae43c5870113f09f2e25a","error":"forbidden","reason":"Not Authorized"}]

According to the docs (at the end of the page), the above response should generate a 417 Expectation Failed status code. However, it currently generates a 201 Created status code.

Because of the incorrect response code, the client (PouchDB) shows as all records synced, but the updates are not written to the server (CouchDB).

Is there a config option to change this status code?

Fore reference, my validate_doc_update function is as following.

function(newDoc, oldDoc, userCtx){ 
    if (!userCtx) throw({forbidden: 'Need a user to update'});

    if((userCtx.roles.indexOf('_admin') == -1) && (userCtx.roles.indexOf('backend:manager') == -1)){
           throw({forbidden: "Not Authorized"});
    }
}
1

1 Answers

1
votes

The 417:expectation failed status code only works when the all_or_nothing parameter is set to true. By default this parameter is false.

The default bulk update transaction mode in couchdb is non atomic which guarantees that only some of the documents will be saved. If the document is not saved the api returns an error object like you got along with a list of documents that were in fact successfully saved. So 201 seems to be the correct response.

Then you've got to walk through the response to find which documents failed and manually update them.

In case of all_or_nothing mode however a success will be returned only if all the documents have been updated.

While syncing you can also use the _replicate endpoint that has many other features that bulk update does not have.