apache / couchdb-nano

Nano: The official Apache CouchDB library for Node.js
https://www.npmjs.com/package/nano
Apache License 2.0
651 stars 165 forks source link

Fields param is missing for changesreader.get #263

Open hexnickk opened 3 years ago

hexnickk commented 3 years ago

Expected Behavior

The CouchDB documentation describes that it is possible to provide _selector for _chages handler

POST /recipes/_changes?filter=_selector HTTP/1.1
Content-Type: application/json
Host: localhost:5984

{
    "selector": { "_id": { "$regex": "^_design/" } }
}

The documentation also describes that selector can contain a list of specified fields to filter.

{
    "selector": { "Actor_name": "Robert De Niro" },
    "fields": ["Actor_name", "Movie_year", "_id", "_rev"]
}

Current Behavior

Right now it is only possible to provide the selector parameter, but not the fields parameter. Link

          if (self.selector) {
            req.qs.filter = '_selector'
            req.body.selector = self.selector
          }

Possible Solution

Locally I've fixed it just by adding another parameter. It can be something like this:

          if (self.selector || self.fields) {
            req.qs.filter = '_selector'
            req.body.selector = self.selector
            req.body.fields = self.fields
          }

It also may worth adding another custom body param similar to qs, which adds additional body params.

          Object.assign(req.qs, opts.qs)
          // New
          Object.assign(req.body, opts.body)

Steps to Reproduce (for bugs)

It should be straightforward to reproduce, but I can add exact steps if needed.

Context

I was trying to fetch few fields from https://replicate.npmjs.com, but I didn't need all extra fields as versions, because it noticeably slows the script down and increases network consumption.

More context

Your Environment