Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 986
bryan Avatar asked May 29 '18 14:05

bryan


People also ask

Why does PouchDB require so much effort?

The answer is: because _rev s are what makes sync work so well. PouchDB asks for a little upfront effort with managing document revisions, so that later on, sync is a breeze. In fact, you are probably already familiar with a system that forces you to go through a similar dance.

What is a document in PouchDB?

What's a document? PouchDB is a NoSQL database, meaning that you store unstructured documents rather than explicitly specifying a schema with rows, tables, and all that jazz. A document might look like this:

How does document revision work between CouchDB and PouchDB?

PouchDB and CouchDB's document revision structure is very similar to Git's. In fact, each document's revision history is stored as a tree (exactly like Git), which allows you to handle conflicts when any two databases get out of sync.

What is Git in PouchDB?

This system is called Git. PouchDB and CouchDB's document revision structure is very similar to Git's. In fact, each document's revision history is stored as a tree (exactly like Git), which allows you to handle conflicts when any two databases get out of sync.


1 Answers

After realizing you are using React Native, I think this actually has to do with PouchDB in React Native, and it's indeed a bug. There are several reports of that behavior:

  • https://github.com/pouchdb/pouchdb/issues/7219
  • https://github.com/pouchdb/pouchdb/issues/7188
  • https://github.com/pouchdb/pouchdb/issues/7293
like image 56
Bernhard Gschwantner Avatar answered Oct 13 '22 17:10

Bernhard Gschwantner