-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (113 loc) · 4.4 KB
/
index.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
const nano = require('nano');
const find = require('lodash.find');
const omit = require('lodash.omit');
const snakeCase = require('lodash.snakecase');
const mapKeys = require('lodash.mapkeys');
const rowsReader = require('./lib/rowsReader');
const iteratorCaller = require('./lib/iteratorCaller');
const iteratorCallerBulk = require('./lib/iteratorCallerBulk');
const allowedQueryOptions = [
'limit', 'skip', 'stale', 'descending', 'startkey', 'startkey_docid',
'endkey', 'endkey_docid', 'include_docs', 'inclusive_end',
];
function getCouchDb(couchdbAddr, options) {
const couchdb = typeof couchdbAddr === 'string' ? nano(couchdbAddr, options.nano) : couchdbAddr;
if (!couchdb.config.db) {
throw new Error('No database is selected, did you pass a database in the couchdb address?');
}
return couchdb;
}
function getQueryFn(couchdb, view) {
if (!view) {
return (options) => {
return new Promise((resolve, reject) => {
couchdb.list(options, (err, response) => {
/* istanbul ignore if */
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
};
}
const viewSplit = view.split('/');
const designName = viewSplit[0];
const viewName = viewSplit[1];
return (options) => {
return new Promise((resolve, reject) => {
couchdb.view(designName, viewName, options, (err, response) => {
/* istanbul ignore if */
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
};
}
function getQueryOptions(options, extra) {
const queryOptions = mapKeys(omit(options, extra), (value, key) => snakeCase(key));
const invalidQueryOption = find(Object.keys(queryOptions), (queryOption) => allowedQueryOptions.indexOf(queryOption) === -1);
if (invalidQueryOption) {
throw new Error(`Query option \`${invalidQueryOption}\` is not allowed`);
}
queryOptions.reduce = false;
return queryOptions;
}
// ------------------------------------------------
function couchdbIterator(couchdbAddr, view, iterator, options) {
if (typeof view === 'function') {
options = iterator;
iterator = view;
view = null;
}
options = Object.assign({ concurrency: 50 }, options);
options.limit = options.limit || options.concurrency * 10;
return new Promise((resolve, reject) => {
const couchdb = getCouchDb(couchdbAddr, options);
const queryFn = getQueryFn(couchdb, view);
const queryOptions = getQueryOptions(options, ['nano', 'concurrency']);
// Start the iteration!
const rowsReaderStream = rowsReader(queryFn, queryOptions);
const iteratorCallerStream = iteratorCaller(iterator, options.concurrency);
rowsReaderStream
.on('error', reject)
.pipe(iteratorCallerStream)
.on('error', reject)
.on('end', () => resolve(iteratorCallerStream.getCount()));
iteratorCallerStream.on('readable', () => {
while (iteratorCallerStream.read() !== null) { /* do nothing */ }
});
});
}
function couchdbIteratorBulk(couchdbAddr, view, iterator, options) {
if (typeof view === 'function') {
options = iterator;
iterator = view;
view = null;
}
options = Object.assign({ bulkSize: 50 }, options);
options.limit = options.limit || options.bulkSize * 10;
return new Promise((resolve, reject) => {
const couchdb = getCouchDb(couchdbAddr, options);
const queryFn = getQueryFn(couchdb, view);
const queryOptions = getQueryOptions(options, ['nano', 'bulkSize']);
// Start the iteration!
const rowsReaderStream = rowsReader(queryFn, queryOptions);
const iteratorCallerStream = iteratorCallerBulk(iterator, options.bulkSize);
rowsReaderStream
.on('error', reject)
.pipe(iteratorCallerStream)
.on('error', reject)
.on('end', () => resolve(iteratorCallerStream.getCount()));
iteratorCallerStream.on('readable', () => {
while (iteratorCallerStream.read() !== null) { /* do nothing */ }
});
});
}
module.exports = couchdbIterator;
module.exports.bulk = couchdbIteratorBulk;