I am listening to the changes from all the databases on my CouchDB instance using nano in my node.js application to follow the _global_changes
database.
const globalChanges = nano.use('_global_changes');
const feed = globalChanges.follow({ since: "now", include_docs: true });
feed.on('change', function (change) {
// Here's the change that just happened
console.log(change);
});
feed.follow();
This works great in returning a reference to every change as it occurs, but I am at a loss as to how I can access the actual document and the details of the change from this point.
Every change that comes out of the feed looks like this:
{
seq: '84-g1AAAAJjeJyd0UsKwjAQBuDY-tq6EJd6AklS-3Blb6KZTIdSqq5c6030JnoTvUlN0m6EIrQEJjDh_2AyJWNsmvvIZvp80TlCKmS85uaI0jx5isGyqqoi99XiaBoTzYmQoC3wh4GVqbBrpLmTBIQoxaarlFpp30gjJyUipAi2XaWDla4_02WSVIyyo3Qamspu5jLY3WpjpwHXQkW8l_aotafVBk6jhHgg-2mvWntbzXMaZjFxxF7ap9bcv_n1BhJFKlBtueILoc2geA',
id: 'updated:DATABASE-WHERE-CHANGE-HAPPENED',
changes: [ { rev: '22-8a4f097580a344b3d70e5adc17971cb4' } ],
doc: {
_id: 'updated:DATABASE-WHERE-CHANGE-HAPPENED',
_rev: '22-8a4f097580a344b3d70e5adc17971cb4'
}
}
From this point, I can't figure out how I can query DATABASE-WHERE-CHANGE-HAPPENED
to get the specific document that was changed/deleted/added, and find out exactly what happened to it. I can't find a document in DATABASE-WHERE-CHANGE_HAPPENED
that matches change._rev
or change.seq
...
Any help would be greatly appreciated.
(I am trying to listen for whenever a certain type of attachment is added, and then do something with that attachment.)
MY SOLUTION
As Hypnic Jerk pointed out in the comment below, there doesn't seem to be any way for me to do this. Instead, whenever a change is made, the only information I get from the Global changes feed is that a change was made in a particular database. So then, I need to query the whole database in question to find if there's some piece of information I am interested in.
In my use case this was easy because I am looking for a certain type of file that might be added as an attachment, and then converting it to a different file type. But for other use cases this could be very difficult or impossible. It would be really helpful if CouchDB could show more information about the changes made in the global _global_changes
feed.
const globalChanges = nano.use('_global_changes');
const feed = globalChanges.follow({ since: "now", include_docs: true });
feed.on('change', function (change) {
// STEP 1 - See if the database mentioned in change.doc._id
// is one that I am interested in
// STEP 2 - If so, extract the database name from change.doc._id
// STEP 3 - Query the database with this name for any docs
// with attachments
// const db = nano.use(databaseName);
// const docsWithAttachments = await db.find({
// selector: { _attachments: { "$exists": true }}
// } ,{ include_docs: true });
// STEP 4 - Go through all the attachments for each document to see if
// there are any files of the type that I am interested in.
// STEP 5 - Do something with those files.
});
feed.follow();
So yes, this causes a lot of extra querying and it works OK for my use case, but for other use cases this may not be as feasible. Sadly, the only information that the _GLOBAL_CHANGES
feed seems to give is to tell you which database was modified, unless I am missing something?