-
Notifications
You must be signed in to change notification settings - Fork 2
/
ddocs.js
72 lines (59 loc) · 1.63 KB
/
ddocs.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
/**
* couch-daemon: High-level os daemon API for CouchDB
*
* ddocs - Create a stream of design docs.
*
* Licensed under the MIT license.
* https://github.com/jo/couch-daemon
* © 2014 Johannes J. Schmidt, null2 GmbH, Berlin
*
*/
var _ = require('highland');
var nano = require('nano');
module.exports = function(url, options) {
options = options || {};
var couch = nano(url);
function getDdocs(db, done) {
couch.use(db).list({
startkey: '_design',
endkey: '_design0',
include_docs: true
}, done);
}
return _.through(function(source) {
var sourceEnded = false;
var target = _(function(push, done) {
// on db create, or update
// - pause stream
// - fetch all ddocs and emit them row by row
// - emit db event
// - resume stream
source
.on('data', function(data) {
if (data && data.stream === 'dbs' && data.db_name && (data.type === 'created' || data.type === 'updated')) {
source.pause();
getDdocs(data.db_name, function(err, resp) {
if (!err) {
resp.rows.forEach(function(row) {
var d = _.extend({ stream: 'ddocs', db_name: data.db_name }, row);
push(null, d);
});
}
push(null, data);
source.resume();
if (sourceEnded) {
push(null, _.nil);
}
});
} else {
push(null, data);
}
})
.on('error', push);
});
source.on('end', function() {
sourceEnded = true;
});
return target;
});
};