iriscouch / follow

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

add ability to specify custom query parameters for dynamic filter functions #3

Closed max-mapper closed 12 years ago

max-mapper commented 12 years ago

so that you can have dynamic filters as demonstrated here: https://github.com/apache/couchdb/blob/trunk/share/www/script/test/changes.js#L232

follow({db: db, include_docs: true, filter: "ddoc/by_value", query_params: {k: "type", v: "template"}}, function(error, change) {
  console.log("template!", change)
})

with ddoc/by_value being defined as such:

ddoc.filters = {
  by_value: function(doc, req) {
    if (!req.query.k || !req.query.v || !doc[req.query.k]) return false;
    return doc[req.query.k] === req.query.v;
  }
}
max-mapper commented 12 years ago

also the API I proposed is copied from filtered replication in which you specify a query_params object in your replication JSON. because of how conflated the options are inside of follow right now (options object getting whitelisted into self, etc) I figured this is a totally sane API given the circumstances

jhs commented 12 years ago

I pushed a modification of your patch (strangely it still gave you credit for the edit, haha). I hope you like it. It uses query_params like you but it also sends them over HTTP if the filter runs server-side. (My own usage is to fetch the function right out of the ddoc, eval it, and choose which side to run it later.)

An example of how it feels is in the all-but useless CLI interface. https://github.com/iriscouch/follow/blob/master/cli.js#L46-50