You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by gl...@apache.org on 2020/11/02 09:21:09 UTC

[couchdb-nano] branch master updated: - Addded viewWithListAsStream function (#227)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new e28e8ad  - Addded viewWithListAsStream function (#227)
e28e8ad is described below

commit e28e8ad4fc130a029bf5a3a5cf67aa3f6f6aa107
Author: Luca Morandini <lm...@ieee.org>
AuthorDate: Mon Nov 2 20:20:18 2020 +1100

    - Addded viewWithListAsStream function (#227)
    
    - Added test case for viewWithListAsStream
    - Added documntation for viewWithListm and viewWithListAsStream
---
 README.md                                | 12 ++++++++
 lib/nano.d.ts                            | 19 +++++++++++--
 lib/nano.js                              |  7 +++++
 test/design.viewWithListAsStream.test.js | 47 ++++++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 541b00c..c10b2ad 100644
--- a/README.md
+++ b/README.md
@@ -87,6 +87,8 @@ See [Migration Guide for switching from Nano 6.x to 7.x](migration_6_to_7.md).
 - [Views and design functions](#views-and-design-functions)
   - [db.view(designname, viewname, [params], [callback])](#dbviewdesignname-viewname-params-callback)
   - [db.viewAsStream(designname, viewname, [params])](#dbviewasstreamdesignname-viewname-params)
+  - [db.viewWithList(designname, viewname, listname, [params])](#dbviewwithlistdesignname-viewname-params)
+  - [db.viewWithListAsStream(designname__viewname, listname, [params])](#dbviewwithlistasstreamdesignname-viewname-params)
   - [db.show(designname, showname, doc_id, [params], [callback])](#dbshowdesignname-showname-doc_id-params-callback)
   - [db.atomic(designname, updatename, docname, [body], [callback])](#dbatomicdesignname-updatename-docname-body-callback)
   - [db.search(designname, viewname, params, [callback])](#dbsearchdesignname-searchname-params-callback)
@@ -1077,6 +1079,16 @@ alice.viewWithList('characters', 'happy_ones', 'my_list').then((body) => {
 });
 ```
 
+### db.viewWithListAsStream(designname, viewname, listname, [params], [callback])
+
+Calls a list function fed by the given view from the specified design document as a stream.
+
+```js
+alice.viewWithListAsStream('characters', 'happy_ones', 'my_list').then((body) => {
+  console.log(body);
+});
+```
+
 ### db.show(designname, showname, doc_id, [params], [callback])
 
 Calls a show function from the specified design for the document specified by doc_id with
diff --git a/lib/nano.d.ts b/lib/nano.d.ts
index 1c7119d..7713bf6 100644
--- a/lib/nano.d.ts
+++ b/lib/nano.d.ts
@@ -321,6 +321,21 @@ declare namespace nano {
       params: DocumentViewParams,
       callback?: Callback<any>
     ): Promise<any>;
+    // http://docs.couchdb.org/en/latest/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name
+    viewWithListAsStream(
+        designname: string,
+        viewname: string,
+        listname: string,
+        callback?: Callback<any>
+    ): Promise<any>;
+    // http://docs.couchdb.org/en/latest/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name
+    viewWithListAsStream(
+        designname: string,
+        viewname: string,
+        listname: string,
+        params: DocumentViewParams,
+        callback?: Callback<any>
+    ): Promise<any>;
     // http://docs.couchdb.org/en/latest/api/database/find.html#db-find
     find(query: MangoQuery, callback?: Callback<MangoResponse<D>>): Promise <MangoResponse<D>>;
     server: ServerScope;
@@ -511,7 +526,7 @@ declare namespace nano {
   interface View<D> {
     map?: string | DocumentInfer<D>;
     reduce?: string | DocumentInfer<D>
-    
+
   }
 
   interface ViewDocument<D> extends IdentifiedDocument {
@@ -1289,7 +1304,7 @@ declare namespace nano {
   // http://docs.couchdb.org/en/latest/api/database/find.html#selector-syntax
   type MangoValue = number | string | Date | boolean | object | null;
   type MangoOperator = '$lt' | '$lte' | '$eq' | '$ne' | '$gte' | '$gt' |
-                    '$exists' | '$type' | 
+                    '$exists' | '$type' |
                     '$in' | '$nin' | '$size' | '$mod' | '$regex' |
                     '$or' | '$and' | '$nor' | '$not' | '$all' | '$allMatch' | '$elemMatch';
   type MangoSelector = {
diff --git a/lib/nano.js b/lib/nano.js
index 977b925..56edd02 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -795,6 +795,12 @@ module.exports = exports = function dbScope (cfg) {
       }, qs, callback)
     }
 
+    function viewWithListAsStream (ddoc, viewName, listName, qs, callback) {
+      return view(ddoc, listName + '/' + viewName, {
+        type: 'list', stream: true
+      }, qs, callback)
+    }
+
     // http://docs.couchdb.org/en/latest/api/database/bulk-api.html#post--db-_bulksDoc
     function bulksDoc (docs, qs0, callback0) {
       const { opts, callback } = getCallback(qs0, callback0)
@@ -1130,6 +1136,7 @@ module.exports = exports = function dbScope (cfg) {
       findAsStream: findAsStream,
       createIndex: createIndex,
       viewWithList: viewWithList,
+      viewWithListAsStream: viewWithListAsStream,
       server: serverScope,
       replication: {
         enable: function (target, opts, cb) {
diff --git a/test/design.viewWithListAsStream.test.js b/test/design.viewWithListAsStream.test.js
new file mode 100644
index 0000000..927301e
--- /dev/null
+++ b/test/design.viewWithListAsStream.test.js
@@ -0,0 +1,47 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const nano = Nano(COUCH_URL)
+const nock = require('nock')
+
+afterEach(() => {
+  nock.cleanAll()
+})
+
+test('should be able to access a MapReduce view with a list as a stream - GET /db/_design/ddoc/_list/listname/viewname - db.viewWithListAsStream', async () => {
+  // mocks
+  const response = {
+    rows: [
+      { key: null, value: 23515 }
+    ]
+  }
+  const scope = nock(COUCH_URL)
+    .get('/db/_design/ddoc/_list/listname/viewname')
+    .reply(200, response)
+
+  return new Promise((resolve, reject) => {
+    const db = nano.db.use('db')
+    const s = db.viewWithListAsStream('ddoc', 'viewname', 'listname')
+    expect(typeof s).toBe('object')
+    let buffer = ''
+    s.on('data', (chunk) => {
+      buffer += chunk.toString()
+    })
+    s.on('end', () => {
+      expect(buffer).toBe(JSON.stringify(response))
+      expect(scope.isDone()).toBe(true)
+      resolve()
+    })
+  })
+})