couch-view-by-keys
A command line tool to query CouchDB views, focused on easing queries with complex JSON keys (which are a pain to do with curl
)
Summary
Install
npm install -g couch-view-by-keys
How-To
General
url="http://username:password@localhost:5984/db-name/_design/design-doc-name/_view/view-name"
couch-view-by-keys "$url" keyA keyB keyC
couch-view-by-keys "$url" '["a", "complex", "key"]' '["another", "complex", "key"]'
cat keys | xargs couch-view-by-keys "$url"
Or to fetch many documents
url="http://username:password@localhost:5984/_all_docs"
couch-view-by-keys "$url" docIdA docIdB docIdC
cat ids | xargs couch-view-by-keys "$url"
Get rows
couch-view-by-keys "$url"
Get docs
couch-view-by-keys --docs "$url"
Get values
couch-view-by-keys --values "$url"
Get only view rows id and key
By default, view rows are returned with their document, but this can be disabled by setting include_docs=false
couch-view-by-keys "${url}?include_docs=false"
NB: reduce=false
is also set by default, as reduce=true
is incompatible with include_docs=true
Limit
couch-view-by-keys "${url}?limit=10"
Skip
couch-view-by-keys "${url}?skip=10"
Output format
newline-delimited JSON
That's the default output format
couch-view-by-keys "$url" keyA keyB keyC
set the indentation to 0 to drop newlines
couch-view-by-keys "$url" keyA keyB keyC --json 0
JSON
couch-view-by-keys "$url" keyA keyB keyC --json
couch-view-by-keys "$url" keyA keyB keyC --json 2
couch-view-by-keys "$url" keyA keyB keyC --json 4
Tips
use single quotes in JSON keys
Some times you might need to use variable interpolation, which, in bash, requires to use double quotes. Unfortunately, JSON keys being expected to be valid JSON, they also require doubles quotes. You would thus normally endup with some horrible escaping of the kind:
couch-view-by-keys "$url" "[\"$1\",\"a\"]" "[\"$2\",\"b\"]" "[\"$3\",\"c\"]"
Horrified by so much anticipated pain, you might just stop there, give up on computing and start drinking. But fear no more! You can just use single quotes instead:
couch-view-by-keys "$url" "['$1','a']" "['$2','b']" "['$3','c']"
couch-view-by-keys "$url" "{ 'a': '$1'}"