-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
41 lines (37 loc) · 930 Bytes
/
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
var View = require('kappa-view')
module.exports = createIndex
function createIndex (ldb, opts) {
return View(ldb, opts, function (db) {
return {
map: function (msgs, next) {
var ops = []
if (!msgs.length) return next()
let pending = msgs.length
msgs.forEach(async function (msg) {
var res = await opts.map(msg)
if (res && Array.isArray(res)) {
res = res.map(function (r) {
return {
type: 'put',
key: r[0],
value: r[1]
}
})
ops.push.apply(ops, res)
}
pending--
done()
})
function done () {
if (pending === 0) {
ldb.batch(ops, next)
}
}
},
api: opts.api,
indexed: function (msgs) {
if (opts.indexed) opts.indexed(msgs)
}
}
})
}