Skip to content

Commit 386e3e3

Browse files
glynnbirdYCsxwei123BigsonLvrochalmorandini
authored
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 <steven.yc.tang@gmail.com> Co-authored-by: Leon <sxwei123@users.noreply.github.com> Co-authored-by: Luiz Victor Linhares Rocha <luizvictorlrocha@gmail.com> Co-authored-by: Luca Morandini <lmorandini@ieee.org>
1 parent c7751ae commit 386e3e3

File tree

4 files changed

+92
-14
lines changed

4 files changed

+92
-14
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ See [Migration Guide for switching from Nano 6.x to 7.x](migration_6_to_7.md).
8787
- [Views and design functions](#views-and-design-functions)
8888
- [db.view(designname, viewname, [params], [callback])](#dbviewdesignname-viewname-params-callback)
8989
- [db.viewAsStream(designname, viewname, [params])](#dbviewasstreamdesignname-viewname-params)
90+
- [db.viewWithList(designname, viewname, listname, [params])](#dbviewwithlistdesignname-viewname-params)
91+
- [db.viewWithListAsStream(designname__viewname, listname, [params])](#dbviewwithlistasstreamdesignname-viewname-params)
9092
- [db.show(designname, showname, doc_id, [params], [callback])](#dbshowdesignname-showname-doc_id-params-callback)
9193
- [db.atomic(designname, updatename, docname, [body], [callback])](#dbatomicdesignname-updatename-docname-body-callback)
9294
- [db.search(designname, viewname, params, [callback])](#dbsearchdesignname-searchname-params-callback)
@@ -1077,6 +1079,16 @@ alice.viewWithList('characters', 'happy_ones', 'my_list').then((body) => {
10771079
});
10781080
```
10791081

1082+
### db.viewWithListAsStream(designname, viewname, listname, [params], [callback])
1083+
1084+
Calls a list function fed by the given view from the specified design document as a stream.
1085+
1086+
```js
1087+
alice.viewWithListAsStream('characters', 'happy_ones', 'my_list').then((body) => {
1088+
console.log(body);
1089+
});
1090+
```
1091+
10801092
### db.show(designname, showname, doc_id, [params], [callback])
10811093

10821094
Calls a show function from the specified design for the document specified by doc_id with

lib/nano.d.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ declare namespace nano {
3333
db: DatabaseScope;
3434
use<D>(db: string): DocumentScope<D>;
3535
scope<D>(db: string): DocumentScope<D>;
36-
request: Promise<any>;
37-
relax: Promise<any>;
38-
dinosaur: Promise<any>;
36+
request(opts: RequestOptions | string, callback?: Callback<any>): Promise<any>;
37+
relax(opts: RequestOptions | string, callback?: Callback<any>): Promise<any>;
38+
dinosaur(opts: RequestOptions | string, callback?: Callback<any>): Promise<any>;
3939
// http://docs.couchdb.org/en/latest/api/server/authn.html#cookie-authentication
4040
auth(username: string, userpass: string, callback?: Callback<DatabaseAuthResponse>): Promise<DatabaseAuthResponse>;
4141
// http://docs.couchdb.org/en/latest/api/server/authn.html#get--_session
@@ -54,10 +54,11 @@ declare namespace nano {
5454

5555
interface FollowEmitter extends EventEmitter {
5656
follow(): void;
57+
stop(): void;
5758
}
5859

5960
interface UUIDObject {
60-
uuids: string[]
61+
uuids: string[];
6162
}
6263

6364
// https://docs.couchdb.org/en/stable/api/server/common.html#api-server-root
@@ -320,6 +321,21 @@ declare namespace nano {
320321
params: DocumentViewParams,
321322
callback?: Callback<any>
322323
): Promise<any>;
324+
// http://docs.couchdb.org/en/latest/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name
325+
viewWithListAsStream(
326+
designname: string,
327+
viewname: string,
328+
listname: string,
329+
callback?: Callback<any>
330+
): Promise<any>;
331+
// http://docs.couchdb.org/en/latest/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name
332+
viewWithListAsStream(
333+
designname: string,
334+
viewname: string,
335+
listname: string,
336+
params: DocumentViewParams,
337+
callback?: Callback<any>
338+
): Promise<any>;
323339
// http://docs.couchdb.org/en/latest/api/database/find.html#db-find
324340
find(query: MangoQuery, callback?: Callback<MangoResponse<D>>): Promise <MangoResponse<D>>;
325341
server: ServerScope;
@@ -425,11 +441,6 @@ declare namespace nano {
425441
db: string;
426442
}
427443

428-
type RequestFunction = (
429-
options?: RequestOptions | string,
430-
callback?: Callback<any>
431-
) => void;
432-
433444
interface RequestOptions {
434445
db?: string;
435446
method?: string;
@@ -513,9 +524,9 @@ declare namespace nano {
513524

514525
type DocumentInfer<D> = (doc: D & Document) => void
515526
interface View<D> {
516-
map?:DocumentInfer<D>;
527+
map?: string | DocumentInfer<D>;
517528
reduce?: string | DocumentInfer<D>
518-
529+
519530
}
520531

521532
interface ViewDocument<D> extends IdentifiedDocument {
@@ -819,6 +830,7 @@ declare namespace nano {
819830
value: {
820831
rev: string;
821832
};
833+
error?: string;
822834
}
823835

824836
interface DocumentResponseRow<D> extends DocumentResponseRowMeta {
@@ -1177,7 +1189,7 @@ declare namespace nano {
11771189
sort?: string | string[];
11781190

11791191
// Do not wait for the index to finish building to return results.
1180-
stale?: boolean
1192+
stale?: boolean;
11811193
}
11821194

11831195
// http://docs.couchdb.org/en/latest/api/ddoc/views.html#get--db-_design-ddoc-_view-view
@@ -1292,11 +1304,11 @@ declare namespace nano {
12921304
// http://docs.couchdb.org/en/latest/api/database/find.html#selector-syntax
12931305
type MangoValue = number | string | Date | boolean | object | null;
12941306
type MangoOperator = '$lt' | '$lte' | '$eq' | '$ne' | '$gte' | '$gt' |
1295-
'$exists' | '$type' |
1307+
'$exists' | '$type' |
12961308
'$in' | '$nin' | '$size' | '$mod' | '$regex' |
12971309
'$or' | '$and' | '$nor' | '$not' | '$all' | '$allMatch' | '$elemMatch';
12981310
type MangoSelector = {
1299-
[K in MangoOperator | string]: MangoSelector | MangoValue | MangoValue[];
1311+
[K in MangoOperator | string]: MangoSelector| MangoSelector[] | MangoValue | MangoValue[];
13001312
}
13011313

13021314
// http://docs.couchdb.org/en/latest/api/database/find.html#sort-syntax

lib/nano.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,12 @@ module.exports = exports = function dbScope (cfg) {
795795
}, qs, callback)
796796
}
797797

798+
function viewWithListAsStream (ddoc, viewName, listName, qs, callback) {
799+
return view(ddoc, listName + '/' + viewName, {
800+
type: 'list', stream: true
801+
}, qs, callback)
802+
}
803+
798804
// http://docs.couchdb.org/en/latest/api/database/bulk-api.html#post--db-_bulksDoc
799805
function bulksDoc (docs, qs0, callback0) {
800806
const { opts, callback } = getCallback(qs0, callback0)
@@ -1130,6 +1136,7 @@ module.exports = exports = function dbScope (cfg) {
11301136
findAsStream: findAsStream,
11311137
createIndex: createIndex,
11321138
viewWithList: viewWithList,
1139+
viewWithListAsStream: viewWithListAsStream,
11331140
server: serverScope,
11341141
replication: {
11351142
enable: function (target, opts, cb) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
2+
// use this file except in compliance with the License. You may obtain a copy of
3+
// the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
9+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
// License for the specific language governing permissions and limitations under
11+
// the License.
12+
13+
const Nano = require('..')
14+
const COUCH_URL = 'http://localhost:5984'
15+
const nano = Nano(COUCH_URL)
16+
const nock = require('nock')
17+
18+
afterEach(() => {
19+
nock.cleanAll()
20+
})
21+
22+
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 () => {
23+
// mocks
24+
const response = {
25+
rows: [
26+
{ key: null, value: 23515 }
27+
]
28+
}
29+
const scope = nock(COUCH_URL)
30+
.get('/db/_design/ddoc/_list/listname/viewname')
31+
.reply(200, response)
32+
33+
return new Promise((resolve, reject) => {
34+
const db = nano.db.use('db')
35+
const s = db.viewWithListAsStream('ddoc', 'viewname', 'listname')
36+
expect(typeof s).toBe('object')
37+
let buffer = ''
38+
s.on('data', (chunk) => {
39+
buffer += chunk.toString()
40+
})
41+
s.on('end', () => {
42+
expect(buffer).toBe(JSON.stringify(response))
43+
expect(scope.isDone()).toBe(true)
44+
resolve()
45+
})
46+
})
47+
})

0 commit comments

Comments
 (0)