This is an automated email from the ASF dual-hosted git repository.

glynnbird pushed a commit to branch issue148
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git

commit 7c906c986c17561f21228b2b179e6d024edf3f80
Author: Glynn Bird <[email protected]>
AuthorDate: Fri Mar 15 09:10:28 2019 +0000

    fix formatting of drilldown parameter for issue #148
---
 lib/nano.js | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/lib/nano.js b/lib/nano.js
index 8169b0d..6fa6784 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -275,6 +275,10 @@ module.exports = exports = function dbScope (cfg) {
       req.body = querystring.stringify(opts.form).toString('utf8')
     }
 
+    // ask request to render query string arrays as repeated values e.g.
+    // ?drilldown=["author","Dickens"]&drilldown=["publisher","Penguin"]
+    req.qsStringifyOptions = { arrayFormat: 'repeat' }
+
     log(req)
 
     if (opts.stream) {
@@ -621,7 +625,7 @@ module.exports = exports = function dbScope (cfg) {
 
       // Several search parameters must be JSON-encoded; but since this is an
       // object API, several parameters need JSON endoding.
-      const paramsToEncode = ['counts', 'drilldown', 'group_sort', 'ranges', 
'sort']
+      const paramsToEncode = ['counts', 'group_sort', 'ranges', 'sort']
       paramsToEncode.forEach(function (param) {
         if (param in qs1) {
           if (typeof qs1[param] !== 'string') {
@@ -637,6 +641,25 @@ module.exports = exports = function dbScope (cfg) {
         }
       })
 
+      // the drilldown parameter needs special treatment. It can either be:
+      // - a single array of strings e.g. ['author', 'Dickens']
+      // - an array of arrays e.g. 
[['author','Dickens']['publisher','Penguin']]
+      // The former should be JSON.stringified the latter should be an array of
+      // JSON.stringified arrays(!).
+      if (qs1['drilldown']) {
+        // if this is an array of arrays
+        if (Array.isArray(qs1['drilldown']) &&
+            qs1['drilldown'].length > 0 &&
+            Array.isArray(qs1['drilldown'][0])) {
+          // JSON stringify each element of the array
+          qs1['drilldown'] = qs1['drilldown'].map(function (val) {
+            return JSON.stringify(val)
+          })
+        } else if (Array.isArray(qs1['drilldown'])) {
+          qs1['drilldown'] = JSON.stringify(qs1['drilldown'])
+        }
+      }
+
       if (qs1 && qs1.keys) {
         const body = {keys: qs1.keys}
         delete qs1.keys

Reply via email to