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 14:30:27 UTC

[couchdb-nano] branch main updated: master --> main (#232)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 386e3e3  master --> main (#232)
386e3e3 is described below

commit 386e3e307f7012aa1d4653e6e2a4c6ddc7432a22
Author: Glynn Bird <gl...@gmail.com>
AuthorDate: Mon Nov 2 14:30:15 2020 +0000

    master --> main (#232)
    
    * fix(nano.d.ts): add opts and callback for relax (#225)
    
    * fix(nano.d.ts): add opts and callback for nano.request
    
    * fix(nano.d.ts): add opts and callback for relax and dinosaur
    
    * Add FollowEmitter.stop() to typescript declaration file (#230)
    
    * Patch typescript declaration file (#231)
    
    * Add string to View.map type (#220)
    
    addresses #219
    
    * - Addded viewWithListAsStream function (#227)
    
    - Added test case for viewWithListAsStream
    - Added documntation for viewWithListm and viewWithListAsStream
    
    Co-authored-by: Steven Tang <st...@gmail.com>
    Co-authored-by: Leon <sx...@users.noreply.github.com>
    Co-authored-by: Luiz Victor Linhares Rocha <lu...@gmail.com>
    Co-authored-by: Luca Morandini <lm...@ieee.org>
---
 README.md                                | 12 ++++++++
 lib/nano.d.ts                            | 40 +++++++++++++++++----------
 lib/nano.js                              |  7 +++++
 test/design.viewWithListAsStream.test.js | 47 ++++++++++++++++++++++++++++++++
 4 files changed, 92 insertions(+), 14 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 8f778bb..7713bf6 100644
--- a/lib/nano.d.ts
+++ b/lib/nano.d.ts
@@ -33,9 +33,9 @@ declare namespace nano {
     db: DatabaseScope;
     use<D>(db: string): DocumentScope<D>;
     scope<D>(db: string): DocumentScope<D>;
-    request: Promise<any>;
-    relax: Promise<any>;
-    dinosaur: Promise<any>;
+    request(opts: RequestOptions | string, callback?: Callback<any>): Promise<any>;
+    relax(opts: RequestOptions | string, callback?: Callback<any>): Promise<any>;
+    dinosaur(opts: RequestOptions | string, callback?: Callback<any>): Promise<any>;
     // http://docs.couchdb.org/en/latest/api/server/authn.html#cookie-authentication
     auth(username: string, userpass: string, callback?: Callback<DatabaseAuthResponse>): Promise<DatabaseAuthResponse>;
     // http://docs.couchdb.org/en/latest/api/server/authn.html#get--_session
@@ -54,10 +54,11 @@ declare namespace nano {
 
   interface FollowEmitter extends EventEmitter {
     follow(): void;
+    stop(): void;
   }
 
   interface UUIDObject {
-    uuids: string[]
+    uuids: string[];
   }
 
   // https://docs.couchdb.org/en/stable/api/server/common.html#api-server-root
@@ -320,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;
@@ -425,11 +441,6 @@ declare namespace nano {
     db: string;
   }
 
-  type RequestFunction = (
-    options?: RequestOptions | string,
-    callback?: Callback<any>
-  ) => void;
-
   interface RequestOptions {
     db?: string;
     method?: string;
@@ -513,9 +524,9 @@ declare namespace nano {
 
   type DocumentInfer<D> = (doc: D & Document) => void
   interface View<D> {
-    map?:DocumentInfer<D>;
+    map?: string | DocumentInfer<D>;
     reduce?: string | DocumentInfer<D>
-    
+
   }
 
   interface ViewDocument<D> extends IdentifiedDocument {
@@ -819,6 +830,7 @@ declare namespace nano {
     value: {
       rev: string;
     };
+    error?: string;
   }
 
   interface DocumentResponseRow<D> extends DocumentResponseRowMeta {
@@ -1177,7 +1189,7 @@ declare namespace nano {
     sort?: string | string[];
 
     // Do not wait for the index to finish building to return results.
-    stale?: boolean
+    stale?: boolean;
   }
 
   // http://docs.couchdb.org/en/latest/api/ddoc/views.html#get--db-_design-ddoc-_view-view
@@ -1292,11 +1304,11 @@ 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 = {
-    [K in MangoOperator | string]: MangoSelector | MangoValue | MangoValue[];
+    [K in MangoOperator | string]: MangoSelector| MangoSelector[] | MangoValue | MangoValue[];
   }
 
   // http://docs.couchdb.org/en/latest/api/database/find.html#sort-syntax
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()
+    })
+  })
+})