0
votes

I would like to have a local to the browser DB which updates itself from a remote source. Am attempting to use pouchdb / cloudant to solve this. The remote DB is used by many clients so should be read only and not require authentication. In cloudant my permissions config for relevant db (e.g. "remoteDB") is as follows:

{"cloudant":{"admin_user":["_writer","_admin","_replicator","_reader"],"nobody":["_reader"],"apiKey":["_writer","_admin","_replicator","_reader"]}}

In my setup code I have the following:

var localDB = new PouchDB('local');
var remoteDB = new PouchDB('https://account.cloudant.com/remoteDB');

localDB.replicate.from(remoteDB, {live: false}).on('change', function (change) {
  console.log("change", change);
}).on('error', function (err) {
  console.log("err-replicate", err);
});

I am getting a 401 unauthorised error when loading a page which includes this. The request that causes this is a PUT request for:

https://account.cloudant.com/remoteDB/_local/JqXQso2dZEjdZsU1e_2Qxw==

The header params are:

  • _id: _local/JqXQso2dZEjdZsU1e_2Qxw==
  • last_seq: "0"

If I have the nobody user in cloudant for this DB have writer permission the error goes away. Any ideas what is wrong here?

Is this the right way to have oneway replication from a remote source?

1
Seems like the fix could be to add _replicator permission to nobody user - Bronte Palmerston

1 Answers

1
votes

The error you are seeing is PouchDB attempting to write "checkpoint" documents to the the remote Cloudant database. The checkpoints mark where the client got to in the revision timeline, making future replications more efficient as they can resume from where they left off.

You correctly surmise that the way to solve this for a read-only replication is grant "_reader" & "_replicator" permissions to the user (or to everyone). The _replicator permission allows only local documents to be written, not the core documents themselves. This is enough to allow checkpoint documents to be saved and for PouchDB to proceed without any errors.