iriscouch / follow

Very stable, very reliable, NodeJS CouchDB _changes follower
Apache License 2.0
393 stars 82 forks source link

Original data #23

Open indexzero opened 11 years ago

indexzero commented 11 years ago

When querying a view for changes it is often useful to get the full original set of view data before handling changes.

Would be nice if follow could handle this.

jhs commented 11 years ago

Sorry, I'm not clear. Can you give some URL or http examples of what you mean? Thanks.

indexzero commented 11 years ago

Sure thing. Say that you want to watch for changes on app/location:

follow({
    db: "https://example.iriscouch.com/boogie",
    filter: "app/location", 
    include_docs:true
  }, function (error, change) {
  if (!error) {
    console.log("Change " + change.seq + " has " + Object.keys(change.doc).length + " fields");
  }
})

Now iirc the callback function (error, change) will only get called when an update is made to app/location, but when you make the follow request there are existing documents in the view which you want to know about. With this implementation you would need to do something like:

//
// Get all documents in the view now
//
request("https://example.iriscouch.com/boogie/_design/app/_view/location", function (err, res, body) {
  //
  // Build list of existing documents in the view
  //
});

//
// Listen for changes
//
follow({
    db: "https://example.iriscouch.com/boogie",
    filter: "app/location", 
    include_docs:true
  }, function (error, change) {
  if (!error) {
    console.log("Change " + change.seq + " has " + Object.keys(change.doc).length + " fields");
  }

  //
  // Continuously update list with changes from the view.
  //
})