-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcouch-iterator.js
106 lines (87 loc) · 2.72 KB
/
couch-iterator.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
'use strict'
var util = require('util')
var querystring = require('querystring')
var debug = require('debug')('couchdown')
var AbstractIterator = require('abstract-leveldown').AbstractIterator
var unwrapValue = require('./unwrap-value')
function CouchIterator (db, options) {
AbstractIterator.call(this, db)
var self = this
var viewOpts = {
endkey: options.end || options.gte || options.gt,
startkey: options.start || options.lte || options.lt,
skip: options.start,
descending: options.reverse || false,
limit: options.limit === -1 ? undefined : options.limit,
include_end: !!options.gte || undefined,
include_docs: options.values || false
}
if (options.lt) {
options.skip = options.skip ? options.skip + 1 : 1
}
this.includeValues = options.values
this.includeKeys = options.keys
this.keyEncoding = options.keyEncoding || db.keyEncoding
this.valueEncoding = options.valueEncoding || db.valueEncoding
this.wrapJSON = db.wrapJSON
this.opts = Object.keys(viewOpts).reduce(function (a, key) {
if (typeof viewOpts[key] !== 'undefined') {
a[key] = typeof viewOpts[key] === 'string' ? '"' + viewOpts[key] + '"' : viewOpts[key]
}
return a
}, {})
this.key = '_all_docs?' + querystring.stringify(this.opts)
this._queryComplete = false
this.queryCallbacks = []
this._curr = 0
this._initialError
db._request(this.key, 'GET', null, true, function (err, body) {
var runCallbacks = function () {
self._queryComplete = true
self.queryCallbacks.forEach(function (cb) {
if (typeof cb === 'function') {
cb()
}
})
}
if (err) {
debug('Errored out on _all_docs call', err)
self._initialError = err
return runCallbacks()
}
debug('Total rows received', body.total_rows)
self._rows = body.rows
runCallbacks()
})
}
util.inherits(CouchIterator, AbstractIterator)
CouchIterator.prototype._next = function (cb) {
var self = this
var queryComplete = function () {
if (self._initialError) {
return cb(self._initialError)
}
if (self._curr === self._rows.length || self._curr < 0) {
return cb()
}
var row = self._rows[self._curr]
var key = row.key.toString(self.keyEncoding)
self._curr++
unwrapValue(row.doc, self.valueEncoding, self.wrapJSON, function (err, body) {
// levelup really wants to parse the JSON itself. Which is a pain.
if (self.valueEncoding === 'json') {
body = JSON.stringify(body)
}
cb(err, key, body)
})
}
if (!this._queryComplete) {
self.queryCallbacks.push(queryComplete)
} else {
queryComplete()
}
}
CouchIterator.prototype._end = function (cb) {
return cb()
}
module.exports = CouchIterator