You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a query occur during a routine that removes a document, the query can result in strange results. Run the code below anywhere with PouchDB. If you adjust the setTimeout delay so that 'end remove' happens in the middle of the query (after 'start query' and before 'end query'), the query result is weird.
I encountered this when designing a db whose internal logic does occasional document removal while still serving external users with querying functionality. So quite often the remove can happen mid-query.
var db = window.__testDb = new PouchDB('test')
db.destroy().then(function(){
var ddoc = {
_id: '_design/type',
views: {
type: {
map: function mapFun(doc) {
if (doc.type) {
emit(doc.type);
}
}.toString()
}
}
}
var doc1 = {
_id: '001',
item: 'Good Kill',
type: 'Movie',
pts: '2015-07-21T07:00:00.000Z'
}
var doc2 = {
_id: '002',
item: 'The Dark Knight',
type: 'Movie',
pts: '2015-07-21T08:00:00.000Z'
}
var revToRemove;
db = new PouchDB('test')
db.groupBy = function(type){
return new Promise(function(resolve, reject){
var ddocName = "groupby_" + type + "_sortedby_lastupdated"
var mapFunction = "function(doc){ if (doc.type === '" + type + "') { emit(doc.pts) } }"
var ddoc = {
_id: '_design/' + ddocName,
views: {}
};
ddoc.views[ddocName] = {
map: mapFunction.toString()
};
db.put(ddoc).then(function(){
resolve(ddocName)
}).catch(function(){
// design doc already exist
resolve(ddocName)
})
})
}
db.put(doc1).then(function(res){
revToRemove = res.rev
return db.put(ddoc)
}).then(function(){
return db.groupBy('Movie');
}).then(function(ddocName){
console.time('put')
console.time('query')
setTimeout(function(){
console.log('start put')
db.put(doc2).then(function(){
console.log('end put')
console.log('start remove')
db.remove(doc1._id, revToRemove).then(function(){
console.log('end remove')
console.timeEnd('put')
})
})
}, 30) // problem extremely likely to happen if timeout set between 20-50ms ('put' time and 'query' time within 20ms of each other)
console.log('start query')
return db.query(ddocName, {
startkey: '2015-07-21T09:00:00.000Z',
include_docs: true,
limit: 5,
descending: true
})
}).then(function(res){
console.log('end query')
console.timeEnd('query')
console.log('res:', res)
console.table(res.rows)
}).catch(function(err){
console.log('err:', err)
})
})
The text was updated successfully, but these errors were encountered:
Yeah, there is a race condition when using include_docs:true with query(). In fact, CouchDB has this same problem. The only solution, sadly, is to emit() your documents and avoid using include_docs:true.
If a query occur during a routine that removes a document, the query can result in strange results. Run the code below anywhere with PouchDB. If you adjust the setTimeout delay so that 'end remove' happens in the middle of the query (after 'start query' and before 'end query'), the query result is weird.
I encountered this when designing a db whose internal logic does occasional document removal while still serving external users with querying functionality. So quite often the remove can happen mid-query.
The text was updated successfully, but these errors were encountered: