基本上,我正在查询视图,请求返回update_seq
值。然后,我在后续的_changes
供稿请求中使用此返回的序列号。我的期望(可能是错误的?)是返回的更改应该为空;因为我只是查询了该视图,并要求从该视图开始进行更改。
通常可以正常工作,除非在不更改文档内容的情况下更新文档(即仅更改文档的_rev
字段)。现在,我可以如上所述查询视图和更改,但是它将始终给出指向此更改文档的更改结果。也许下面的代码可以更好地解释...
const request = require("request-promise-native");
const _throw = e => { throw e };
// Given an 'empty' couchdb database 'test' with the following design document:
//
// _id: "_design/test",
// language: "javascript",
// views: {
// test: {
// map: function(doc) {
// if (doc.message) emit(null, doc.message);
// }
// }
// }
const db = "http://localhost:5984/test";
const get = id =>
request.get({
url: `${db}/${id}`,
json: true,
})
const post = body =>
request.post({
url: `${db}`,
json: true,
body
});
// Insert doc, or update existing doc if it already exists ...
const update = doc =>
get(doc._id)
.then(({ _rev }) => post({ ...doc, _rev }))
.catch(e => e.statusCode === 404
? post(doc)
: _throw(e))
// Query the view, make sure to get the update_seq value ...
const messages = () => request
.get({
url: `${db}/_design/test/_view/test`,
qs: { update_seq: true },
json: true
})
// Query the _changes feed for any changes since the supplied seq number
const changes = (since = "now") =>
request
.get({
url: `${db}/_changes`,
qs: { filter: "_view", view: "test/test", since },
json: true
})
// Just update the document, query the view, and then pass the returned seq to the changes feed ...
const check = async message => {
await update({ _id: "test", message });
const msgs = await messages();
console.log("messages:", msgs.rows);
const chgs = await changes(msgs.update_seq);
console.log("changes:", chgs.results, "\n\n");
}
// I would expect the changes results to always be empty, since I am tracking the
// changes since the seq number returned from the view (and there are no changes
// to the database inbetween). However, this is only true for when the message field
// of the document changes. Updating the doc without changing this field (i.e. just
// triggering a _rev change) causes the changes result to be always non-empty
const run_checks = async () => {
await check("1");
await check("1");
await check("2");
await check("2");
await check("2");
}
run_checks();
这将产生以下结果:
$ node changes.js
messages: [ { id: 'test', key: null, value: '1' } ]
changes: []
messages: [ { id: 'test', key: null, value: '1' } ]
changes: [ { seq:
'55-g1AAAAH3eJzLYWBg4MhgTmEQTM4vTc5ISXIwNDLXMwBCwxygFFMiQ5L8____szKYExlygQLs5mlmqZamZikMnKV5KalpmXmpKXi0JykAySR7kAmJjPjUOYDUxaPalJpkYpZkTKxNCSAT6lFMSEqxSDM1NSXShDwWIMnQAKSAhsxHmJKaZGFoQLSPIaYsgJiyH2FKskmyhbm5CUmmHICYch9kihnER6ZpxuYgt2DqI2jaA4hpwJhgyAIAz9mJKQ',
id: 'test',
changes: [ [Object] ] } ]
messages: [ { id: 'test', key: null, value: '2' } ]
changes: []
messages: [ { id: 'test', key: null, value: '2' } ]
changes: [ { seq:
'57-g1AAAAH3eJzLYWBg4MhgTmEQTM4vTc5ISXIwNDLXMwBCwxygFFMiQ5L8____szKYExlygQLs5mlmqZamZikMnKV5KalpmXmpKXi0JykAySR7kAmJjPjUOYDUxaPalJpkYpZkTKxNCSAT6lFMSEqxSDM1NSXShDwWIMnQAKSAhsxHmJKaZGFoQLSPIaYsgJiyH2FKskmyhbm5CUmmHICYch9kigXER6ZpxuYgt2DqI2jaA4hpwJhgyAIA0HWJKw',
id: 'test',
changes: [ [Object] ] } ]
messages: [ { id: 'test', key: null, value: '2' } ]
changes: [ { seq:
'58-g1AAAAIzeJyV0EsOgjAQBuBRTNSlJ9ATGKAtbVdyE6WvIEFcsdab6E30JnoTLNYESAyBNJkmbebLP5MDwCL1FKzkuZSpEnEQ0q1vT5Dbr2kCYl1VVZZ6CZzsw5yaSHMSKViWhdLmWGjV0y42tordT5h8hZAxpHDyr6dPimtp382iBY4EGprlUAuXjiAUM4SQgUIxsxWu9rLIrVG0YIE_eCdOuTvl0SgSS0YpHqU8nfKqFe4mIgbROsuo7Trt7bTWfphUiPOwnSn7AHWel-8',
id: 'test',
changes: [ [Object] ] } ]
仅当视图中的实际值发生更改时,更改结果才为空...我缺少什么?