apache / apache/couchdb-nano

Fields param is missing for changesreader.get

Open
#263 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
689
Forks
167
Avg merge
1h
Merged PRs (30d)
1

Description

## Expected Behavior

[The CouchDB documentation](https://docs.couchdb.org/en/stable/api/database/changes.html#selector) 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](https://docs.couchdb.org/en/stable/api/database/find.html#filtering-fields) 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](https://github.com/apache/couchdb-nano/blob/c84d0a43b13edc888a2a339f7fb057ad66d3d3fe/lib/changesreader.js#L168)
```
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](https://github.com/npm/registry/blob/master/docs/REPLICATE-API.md)

## Your Environment
* Version used: `"nano": "^9.0.3",`

Contributor guide

Open the contributing guide

Research direction

Start in lib/changesreader.js around line 168 and trace how changesreader.get builds the request when selector is provided. Verify the documented fields option and the proposed body-parameter behavior against CouchDB, then confirm that a fields list is sent for filtered changes requests without changing existing selector behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.