Skip to content

Commit 470ae5e

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 0867abd commit 470ae5e

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
@@ -395,10 +395,10 @@ declare namespace nano {
395395
list(params: DocumentListParams): Promise<DocumentListResponse<D>>;
396396
/** List document from this database as a stream.
397397
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/bulk-api.html#get--db-_all_docs} */
398-
listAsStream(): NodeJS.ReadStream;
398+
listAsStream(): Promise<NodeJS.ReadStream>;
399399
/** List document from this database as a stream with options.
400400
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/bulk-api.html#get--db-_all_docs} */
401-
listAsStream(params: DocumentListParams): NodeJS.ReadStream;
401+
listAsStream(params: DocumentListParams): Promise<NodeJS.ReadStream>;
402402
/** Fetch a list of documents by _id.
403403
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/bulk-api.html#post--db-_all_docs} */
404404
fetch(docnames: BulkFetchDocsWrapper): Promise<DocumentFetchResponse<D>>;
@@ -482,7 +482,7 @@ declare namespace nano {
482482
designname: string,
483483
searchname: string,
484484
params: DocumentSearchParams
485-
): NodeJS.ReadStream;
485+
): Promise<NodeJS.ReadStream>;
486486
/** Low-level wrapper that executes a view from a Design Document.
487487
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/views.html#get--db-_design-ddoc-_view-view} */
488488
baseView<V>(
@@ -509,14 +509,14 @@ declare namespace nano {
509509
viewAsStream<V>(
510510
designname: string,
511511
viewname: string
512-
): NodeJS.ReadStream;
512+
): Promise<NodeJS.ReadStream>;
513513
/** Executes a view from a Design Document, with options as a stream
514514
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/views.html#get--db-_design-ddoc-_view-view} */
515515
viewAsStream<V>(
516516
designname: string,
517517
viewname: string,
518518
params: DocumentViewParams
519-
): NodeJS.ReadStream;
519+
): Promise<NodeJS.ReadStream>;
520520
/** Applies a list function to a view.
521521
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name} */
522522
viewWithList(
@@ -538,15 +538,15 @@ declare namespace nano {
538538
designname: string,
539539
viewname: string,
540540
listname: string
541-
): NodeJS.ReadStream;
541+
): Promise<NodeJS.ReadStream>;
542542
/** Applies a list function to a view with options as a stream.
543543
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name} */
544544
viewWithListAsStream(
545545
designname: string,
546546
viewname: string,
547547
listname: string,
548548
params: DocumentViewParams
549-
): NodeJS.ReadStream;
549+
): Promise<NodeJS.ReadStream>;
550550
/** Run Mango query.
551551
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/find.html#db-find} */
552552
find(query: MangoQuery): Promise <MangoResponse<D>>;
@@ -563,13 +563,13 @@ declare namespace nano {
563563
partitionedList(partitionKey: string, params?: DocumentFetchParams): Promise<DocumentListResponse<D>>;
564564
/** List documents in 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-all-docs} */
566-
partitionedListAsStream(partitionKey: string, params?: DocumentFetchParams): NodeJS.ReadStream;
566+
partitionedListAsStream(partitionKey: string, params?: DocumentFetchParams): Promise<NodeJS.ReadStream>;
567567
/** Run Mango query a single partition in this database.
568568
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-id-find} */
569569
partitionedFind(partitionKey: string, query: MangoQuery): Promise <MangoResponse<D>>;
570570
/** Run Mango query a single partition in this database, as a stream.
571571
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-id-find} */
572-
partitionedFindAsStream(partitionKey: string, query: MangoQuery): NodeJS.ReadStream;
572+
partitionedFindAsStream(partitionKey: string, query: MangoQuery): Promise<NodeJS.ReadStream>;
573573
/** Run a full-text search in a single partition in this database. */
574574
partitionedSearch<V>(
575575
partitionKey: string,
@@ -583,7 +583,7 @@ declare namespace nano {
583583
designname: string,
584584
searchname: string,
585585
params: DocumentSearchParams
586-
): NodeJS.ReadStream;
586+
): Promise<NodeJS.ReadStream>;
587587
/** Executes the specified view function in a single partition from the specified design document.
588588
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-design-design-doc-view-view-name} */
589589
partitionedView<V>(
@@ -599,7 +599,7 @@ declare namespace nano {
599599
designname: string,
600600
viewname: string,
601601
params: DocumentViewParams
602-
): NodeJS.ReadStream;
602+
): Promise<NodeJS.ReadStream>;
603603
}
604604

605605
/** attachment data */
@@ -649,7 +649,7 @@ declare namespace nano {
649649
get(docname: string, attname: string): Promise<Buffer>;
650650
/** Get an attachment as a stream.
651651
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/document/attachments.html#get--db-docid-attname} */
652-
getAsStream(docname: string, attname: string): NodeJS.ReadStream;
652+
getAsStream(docname: string, attname: string): Promise<NodeJS.ReadStream>;
653653
/** Get an attachment with options.
654654
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/document/attachments.html#get--db-docid-attname} */
655655
get(

0 commit comments

Comments
 (0)