Skip to content

Commit 4baa37e

Browse files
author
Glynn Bird
committed
simplify Nano a bit. Removed some old code and made "relax" a simpler function. At the same time, i have made the ...AsStream functions "async" as they were a bit odd in that regard, returning synchronously.
1 parent eded337 commit 4baa37e

18 files changed

+357
-651
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ const dblist = await nano.db.list()
347347
Lists all the CouchDB databases as a stream:
348348

349349
```js
350-
nano.db.listAsStream()
350+
(await nano.db.listAsStream())
351351
.on('error', (e) => console.error('error', e))
352352
.pipe(process.stdout)
353353
```
@@ -418,7 +418,7 @@ const c = await nano.db.changes('alice')
418418
Same as `nano.db.changes` but returns a stream.
419419

420420
```js
421-
nano.db.changes('alice').pipe(process.stdout)
421+
( await nano.db.changes('alice')).pipe(process.stdout)
422422
```
423423

424424
### nano.db.info()
@@ -602,7 +602,7 @@ const doclist = await alice.list({include_docs: true})
602602
List all the docs in the database as a stream.
603603

604604
```js
605-
alice.listAsStream()
605+
(await alice.listAsStream())
606606
.on('error', (e) => console.error('error', e))
607607
.pipe(process.stdout)
608608
```
@@ -858,12 +858,12 @@ Fetch documents from a partition as a stream:
858858

859859
```js
860860
// fetch document id/revs from a partition
861-
nano.db.partitionedListAsStream('canidae')
861+
(await nano.db.partitionedListAsStream('canidae'))
862862
.on('error', (e) => console.error('error', e))
863863
.pipe(process.stdout)
864864

865865
// add document bodies but limit size of response
866-
nano.db.partitionedListAsStream('canidae', { include_docs: true, limit: 5 })
866+
(await nano.db.partitionedListAsStream('canidae', { include_docs: true, limit: 5 }))
867867
.on('error', (e) => console.error('error', e))
868868
.pipe(process.stdout)
869869
```
@@ -883,7 +883,7 @@ Query documents from a partition by supplying a Mango selector as a stream:
883883

884884
```js
885885
// find document whose name is 'wolf' in the 'canidae' partition
886-
db.partitionedFindAsStream('canidae', { 'selector' : { 'name': 'Wolf' }})
886+
(await db.partitionedFindAsStream('canidae', { 'selector' : { 'name': 'Wolf' }}))
887887
.on('error', (e) => console.error('error', e))
888888
.pipe(process.stdout)
889889
```
@@ -908,7 +908,7 @@ Search documents from a partition by supplying a Lucene query as a stream:
908908
const params = {
909909
q: 'name:\'Wolf\''
910910
}
911-
db.partitionedSearchAsStream('canidae', 'search-ddoc', 'search-index', params)
911+
(await db.partitionedSearchAsStream('canidae', 'search-ddoc', 'search-index', params))
912912
.on('error', (e) => console.error('error', e))
913913
.pipe(process.stdout)
914914
// { total_rows: ... , bookmark: ..., rows: [ ...] }
@@ -938,7 +938,7 @@ const params = {
938938
endkey: 'b',
939939
limit: 1
940940
}
941-
db.partitionedViewAsStream('canidae', 'view-ddoc', 'view-name', params)
941+
(await db.partitionedViewAsStream('canidae', 'view-ddoc', 'view-name', params))
942942
.on('error', (e) => console.error('error', e))
943943
.pipe(process.stdout)
944944
// { rows: [ { key: ... , value: [Object] } ] }
@@ -994,7 +994,7 @@ fs.readFile('rabbit.png', (err, data) => {
994994
### db.attachment.insertAsStream(docname, attname, att, contenttype, [params])
995995

996996
As of Nano 9.x, the function `db.attachment.insertAsStream` is now deprecated. Now simply pass
997-
a readable stream to `db.attachment.insert` as the third paramseter.
997+
a readable stream to `db.attachment.insert` as the third parameter.
998998

999999
### db.attachment.get(docname, attname, [params])
10001000

@@ -1012,7 +1012,7 @@ fs.writeFile('rabbit.png', body)
10121012

10131013
```js
10141014
import fs from 'node:fs'
1015-
alice.attachment.getAsStream('rabbit', 'rabbit.png')
1015+
(await alice.attachment.getAsStream('rabbit', 'rabbit.png'))
10161016
.on('error', e => console.error)
10171017
.pipe(fs.createWriteStream('rabbit.png'))
10181018
```
@@ -1062,7 +1062,7 @@ const body = alice.view('characters', 'happy_ones', { include_docs: true })
10621062
Same as `db.view` but returns a stream:
10631063

10641064
```js
1065-
alice.viewAsStream('characters', 'happy_ones', {reduce: false})
1065+
(await alice.viewAsStream('characters', 'happy_ones', {reduce: false}))
10661066
.on('error', (e) => console.error('error', e))
10671067
.pipe(process.stdout)
10681068
```
@@ -1080,7 +1080,7 @@ const body = await alice.viewWithList('characters', 'happy_ones', 'my_list')
10801080
Calls a list function fed by the given view from the specified design document as a stream.
10811081

10821082
```js
1083-
alice.viewWithListAsStream('characters', 'happy_ones', 'my_list')
1083+
(await alice.viewWithListAsStream('characters', 'happy_ones', 'my_list'))
10841084
.on('error', (e) => console.error('error', e))
10851085
.pipe(process.stdout)
10861086
```
@@ -1143,7 +1143,7 @@ Check out the tests for a fully functioning example.
11431143
Calls a view of the specified design with optional query string additions `params`. Returns stream.
11441144

11451145
```js
1146-
alice.search('characters', 'happy_ones', { q: 'cat' }).pipe(process.stdout)
1146+
(await alice.search('characters', 'happy_ones', { q: 'cat' })).pipe(process.stdout)
11471147
```
11481148

11491149
### db.find(selector)
@@ -1177,7 +1177,7 @@ const q = {
11771177
fields: [ "name", "age", "tags", "url" ],
11781178
limit:50
11791179
}
1180-
alice.findAsStream(q)
1180+
(await alice.findAsStream(q))
11811181
.on('error', (e) => console.error('error', e))
11821182
.pipe(process.stdout)
11831183
```
@@ -1260,7 +1260,7 @@ import fs from 'node:fs'
12601260
import Nano from 'nano'
12611261
const nano = Nano('http://127.0.0.1:5984/')
12621262
const alice = nano.use('alice')
1263-
alice.attachment.getAsStream('rabbit', 'picture.png')
1263+
(await alice.attachment.getAsStream('rabbit', 'picture.png'))
12641264
.on('error', (e) => console.error('error', e))
12651265
.pipe(fs.createWriteStream('/tmp/rabbit.png'))
12661266
```

lib/changesreader.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,21 @@ export default class ChangesReader {
278278
}
279279
const lin = liner()
280280
const cp = changeProcessor(self.ee, self.batchSize)
281-
self.request(req)
282-
.on(EVENT_ERROR, (e) => {
283-
self.ee.emit(EVENT_ERROR, e)
284-
})
285-
.pipe(lin)
286-
.pipe(cp)
287-
.on('finish', (lastSeq) => {
288-
// the 'end' event was triggering before the last data event
289-
setTimeout(() => {
290-
self.ee.emit('end', cp.lastSeq)
291-
}, 10)
292-
})
281+
setImmediate(async () => {
282+
const s = await self.request(req)
283+
s.on(EVENT_ERROR, (e) => {
284+
self.ee.emit(EVENT_ERROR, e)
285+
})
286+
.pipe(lin)
287+
.pipe(cp)
288+
.on('finish', (lastSeq) => {
289+
// the 'end' event was triggering before the last data event
290+
setTimeout(() => {
291+
self.ee.emit('end', cp.lastSeq)
292+
}, 10)
293+
})
294+
})
295+
293296

294297
return self.ee
295298
}

lib/nano.d.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,10 @@ declare namespace nano {
392392
list(params: DocumentListParams): Promise<DocumentListResponse<D>>;
393393
/** List document from this database as a stream.
394394
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/bulk-api.html#get--db-_all_docs} */
395-
listAsStream(): NodeJS.ReadStream;
395+
listAsStream(): Promise<NodeJS.ReadStream>;
396396
/** List document from this database as a stream with options.
397397
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/bulk-api.html#get--db-_all_docs} */
398-
listAsStream(params: DocumentListParams): NodeJS.ReadStream;
398+
listAsStream(params: DocumentListParams): Promise<NodeJS.ReadStream>;
399399
/** Fetch a list of documents by _id.
400400
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/bulk-api.html#post--db-_all_docs} */
401401
fetch(docnames: BulkFetchDocsWrapper): Promise<DocumentFetchResponse<D>>;
@@ -479,7 +479,7 @@ declare namespace nano {
479479
designname: string,
480480
searchname: string,
481481
params: DocumentSearchParams
482-
): NodeJS.ReadStream;
482+
): Promise<NodeJS.ReadStream>;
483483
/** Low-level wrapper that executes a view from a Design Document.
484484
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/views.html#get--db-_design-ddoc-_view-view} */
485485
baseView<V>(
@@ -506,14 +506,14 @@ declare namespace nano {
506506
viewAsStream<V>(
507507
designname: string,
508508
viewname: string
509-
): NodeJS.ReadStream;
509+
): Promise<NodeJS.ReadStream>;
510510
/** Executes a view from a Design Document, with options as a stream
511511
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/views.html#get--db-_design-ddoc-_view-view} */
512512
viewAsStream<V>(
513513
designname: string,
514514
viewname: string,
515515
params: DocumentViewParams
516-
): NodeJS.ReadStream;
516+
): Promise<NodeJS.ReadStream>;
517517
/** Applies a list function to a view.
518518
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name} */
519519
viewWithList(
@@ -535,15 +535,15 @@ declare namespace nano {
535535
designname: string,
536536
viewname: string,
537537
listname: string
538-
): NodeJS.ReadStream;
538+
): Promise<NodeJS.ReadStream>;
539539
/** Applies a list function to a view with options as a stream.
540540
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name} */
541541
viewWithListAsStream(
542542
designname: string,
543543
viewname: string,
544544
listname: string,
545545
params: DocumentViewParams
546-
): NodeJS.ReadStream;
546+
): Promise<NodeJS.ReadStream>;
547547
/** Run Mango query.
548548
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/find.html#db-find} */
549549
find(query: MangoQuery): Promise <MangoResponse<D>>;
@@ -557,13 +557,13 @@ declare namespace nano {
557557
partitionedList(partitionKey: string, params?: DocumentFetchParams): Promise<DocumentListResponse<D>>;
558558
/** List documents in a single partition in this database as a stream.
559559
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-all-docs} */
560-
partitionedListAsStream(partitionKey: string, params?: DocumentFetchParams): NodeJS.ReadStream;
560+
partitionedListAsStream(partitionKey: string, params?: DocumentFetchParams): Promise<NodeJS.ReadStream>;
561561
/** Run Mango query a single partition in this database.
562562
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-id-find} */
563563
partitionedFind(partitionKey: string, query: MangoQuery): Promise <MangoResponse<D>>;
564564
/** Run Mango query a single partition in this database, as a stream.
565565
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-id-find} */
566-
partitionedFindAsStream(partitionKey: string, query: MangoQuery): NodeJS.ReadStream;
566+
partitionedFindAsStream(partitionKey: string, query: MangoQuery): Promise<NodeJS.ReadStream>;
567567
/** Run a full-text search in a single partition in this database. */
568568
partitionedSearch<V>(
569569
partitionKey: string,
@@ -577,7 +577,7 @@ declare namespace nano {
577577
designname: string,
578578
searchname: string,
579579
params: DocumentSearchParams
580-
): NodeJS.ReadStream;
580+
): Promise<NodeJS.ReadStream>;
581581
/** Executes the specified view function in a single partition from the specified design document.
582582
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-design-design-doc-view-view-name} */
583583
partitionedView<V>(
@@ -593,7 +593,7 @@ declare namespace nano {
593593
designname: string,
594594
viewname: string,
595595
params: DocumentViewParams
596-
): NodeJS.ReadStream;
596+
): Promise<NodeJS.ReadStream>;
597597
}
598598

599599
/** attachment data */
@@ -643,7 +643,7 @@ declare namespace nano {
643643
get(docname: string, attname: string): Promise<Buffer>;
644644
/** Get an attachment as a stream.
645645
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/document/attachments.html#get--db-docid-attname} */
646-
getAsStream(docname: string, attname: string): NodeJS.ReadStream;
646+
getAsStream(docname: string, attname: string): Promise<NodeJS.ReadStream>;
647647
/** Get an attachment with options.
648648
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/document/attachments.html#get--db-docid-attname} */
649649
get(

0 commit comments

Comments
 (0)