Saturday, 2 June 2018

pouchDB query() bug when updating documents

Let's say I have these three documents:

{ "_id": "11111", "type": "template", "name": "person" }
{ "_id": "22222", "type": "template", "name": "place" }
{ "_id": "33333", "type": "template", "name": "thing" }

I have a cloud database and then I have a device with pouchDB syncing from that database.

These are the steps that I do:

  1. I sync both databases together. So now I have the most recent versions of this document on my device.
  2. I run the below query and I get back all three templates like so:

Code

var template_obj = {};

return device_db.query('filters/templates')
.then((templates) => {
    for (let t of templates.rows) templates_obj[t.id] = true;
    return templates_obj;
});

filters/templates

function (doc) {
  if(doc.type == "template")
    emit(doc._id);
}

return

{ "11111": true, "22222": true, "33333": true }

  1. I update template: person on cloud. And then I update it again. So 2 revisions have gone by without syncing to my device.

  2. I sync with my device.

  3. Now when I run the same query and I only get back the document I edited. Which is weird because I haven't touched any of the other documents. The same view returns the expected results on the cloud but not on the device.

return

{"11111": true}

  1. If I do the following code however, all templates come back as normal and the same _rev from the cloud show up on the device. Meaning the sync was successful and view is getting confused.

new code

return device_db.allDocs({conflicts: true})
.then((data) => {
    for (let d of data.rows) {
        if(d.doc.type == "template") {
            templates_obj[d.doc._id] = true;
        }
    }
    return templates_obj;
 }).catch((err) => {
    console.log(JSON.stringify(err));
})

I'm starting to believe this is a bug because if I destroy my database and do these steps again, I can reproduce this issue.



from pouchDB query() bug when updating documents

No comments:

Post a Comment