pouchdb: .query() bug when updating documents
Issue
.query()
function is not returning what it is supposed to. If I do allDocs()
it works as designed. I’ve destroyed the database and done this over and over again with the same result.
Info
- Environment: react-native
- Platform: iOS
- Adapter: asyncstorage
- Server: pouchDB / cloudant
Reproduce
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:
- I sync both databases together. So now I have the most recent versions of this document on my device.
- 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}
-
I update template: person on cloud. And then I update it again. So 2 revisions have gone by without syncing to my device.
-
I sync with my device.
-
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}
- 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.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 26 (3 by maintainers)
Hi David, take a look in the abstract-mapreduce section. That probably would be a decent place to start.
From: David Welling notifications@github.com Sent: Thursday, December 27, 2018 4:36 PM To: pouchdb/pouchdb Cc: Subscribed Subject: Re: [pouchdb/pouchdb] .query() bug when updating documents (#7293)
@daleharveyhttps://github.com/daleharvey Hi Dale, I’d like to try to submit a PR for this issue if I can figure it out. I realize it isn’t a problem with pouchdb so much as react native, but if I can find a way to make it work without disrupting the pouchdb code, that might be a boon for both communities. I’m having trouble just tracking down where indexes are updated when a put occurs. If you can just give me a push in the right direction, I would be super grateful. It seems unlikely to be in the adapter as I’ve been using the SQL lite adapter while others appear to have been using the asyncstorage adapter.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/pouchdb/pouchdb/issues/7293#issuecomment-450161567, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAK9AmY6V-bzDYEdAhGmXvLekycjwmcjks5u9NrwgaJpZM4UMo6I.
Also experiencing something similar to this.
What’s weird is that it happily allows me to query the database (with indices etc.) after my initialisation steps (which include calls to
db.put({ ... })
); but when I make changes afterward then I only get the doc that I just made changes to.The docs are definitely still in the database because
db.allDocs()
still works as intended.EDIT: When making multiple changes arising from the change feed, all of those documents changed can be seen by the query.
I published
pouchdb-adapter-react-native-sqlite@2.0.0
which depends onpouchdb-adapter-websql-core@7.0.0
. I confirmed it resolved the issue on RN@0.59. Thanks @DaveWelling!@bgold0 hm, according to docs they are equivalent https://pouchdb.com/api.html#query_index
I’ve just rebuided RN from scratch using your example syntax - now it works. For how long - that’s the question… So you had no similar issues with find, right?
UPD: no way, it stopped again after couple of reloadings - just as it did before( UPD2: I found the reason - .find stops getting results after replication from server even unchanged docs. I’m on the latest PDB 7.0. That’s even worse than with .query( UPD3: the same happens for “pouchdb-find”: “6.4.3” + “pouchdb-react-native”: “6.4.1”. Does it work for you?
I turns out this IS a problem in BOTH adapters (sqllite (which uses the old pouchdb-adapter-websql-core) and asyncstoreage). Essentially, what is happening is that neither adapter supports the ‘keys’ option in allDocs. This means that when this lookup criteria is passed in, it is ignored and all records are returned.
As a result, abstract-mapreduce will not work correctly when updating the index. Specifically, it fails at line 494 of getKeyValueDocs – returning all docs instead of those with the keys passed in. In my case, I was able to find a different adapter for SqlLite that works.
If you come to this thread with the same problem, I’ve created a PR in the pouchdb-adapters-rn repository to fix a few minor problems which will prevent you using it. If the PR isn’t merged for whatever reason, you can just use my fork.
Any updates about this issue? Also facing the same problem using with react native.