-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
view.js
118 lines (107 loc) · 2.82 KB
/
view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
const url = require('url');
const fetch = require('node-fetch');
const utils = require('./utils.js');
const config = require('./config.js');
/**
* View constructor. Create an instance of a view associated with a couchdb view in the npm registry.
*
* ```js
* const view = new View('dependedUpon');
* ```
*
* @param {String} `name` Name of couchdb view to use.
* @returns {Object} instance of `View`
* @name View
* @api public
*/
class View {
constructor(name) {
this.name = name;
this.config = utils.clone(config);
this.config.pathname += '/_view/' + this.name;
}
/**
* Query the couchdb view with the provided parameters.
*
* ```js
* let results = await view.query({
* group_level: 2,
* startkey: JSON.stringify(['micromatch']),
* endkey: JSON.stringify(['micromatch', {}])
* });
* ```
* @param {Object} `params` URL query parameters to pass along to the couchdb view.
* @return {Promise} Results of the query when promise is resolved.
* @name .query
* @api public
*/
async query(params = {}) {
const response = await fetch(this.url(params));
if (!response.ok) {
throw new Error(response.statusText);
}
return new Promise((resolve, reject) => {
let items = [];
let header = {};
response.body
.pipe(utils.JSONStream.parse('rows.*'))
.on('header', (data) => {
header = data;
if (header.error) {
reject(new Error(header.reason || header.error));
}
})
.on('data', (data) => {
items.push(data);
})
.once('error', reject)
.once('end', () => {
resolve(items);
});
});
}
/**
* Query the couchdb view with the provided parameters and return a stream of results.
*
* ```js
* view.stream({
* group_level: 2,
* startkey: JSON.stringify(['micromatch']),
* endkey: JSON.stringify(['micromatch', {}])
* })
* .on('data', (data) => {
* console.log(data);
* });
* ```
* @param {Object} `params` URL query parameters to pass along to the couchdb view.
* @return {Stream} Streaming results of the query.
* @name .stream
* @api public
*/
stream(params = {}) {
const stream = utils.JSONStream.parse('rows.*');
fetch(this.url(params)).then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
response.body.pipe(stream);
}).catch(e => stream.emit('error', e));
return stream;
}
/**
* Build a formatted url with the provided parameters.
*
* @param {Object} `query` URL query parameters.
* @return {String} formatted url string
* @name .url
* @api public
*/
url(query = {}) {
return url.format({ ...this.config, query });
}
}
/**
* Exposes `View`
*/
module.exports = View;