-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (44 loc) · 1.21 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
var sublevel = require('level-sublevel')
var monot = require('monot')
var map = require('map-stream')
var deleteRange = require('level-delete-range')
var Timestamp = monot()
module.exports = airplanedb
function airplanedb (db) {
if (db.sync) return db
db = sublevel(db)
db.methods = db.methods || {}
db.methods = {sync: {type: 'readable'}}
var changelog = db.sublevel('changelog')
db.pre(addChange)
function addChange (ch, add) {
add({
key: ch.key + '!' + new Timestamp().toISOString() + '!' + ch.type,
value: ch.value || '',
type: 'put',
prefix: changelog
})
}
changelog.post(cleanup)
function cleanup (ch) {
if (ch.type === 'del') return
var start = ch.key.slice(0, -28) + '0000-00-00T00:00:00.000Z' + ch.key.slice(-4)
var end = ch.key.slice(0, -5) + 'A' + ch.key.slice(-4)
deleteRange(changelog, {
start: start,
end: end
})
}
db.sync = sync
function sync (from, range) {
from = from || '0000-00-00T00:00:00.000Z'
return changelog.createReadStream(range)
.pipe(map(filter))
function filter (item, cb) {
var ts = item.key.slice(-28, -4)
if (ts >= from) return cb(null, item)
cb()
}
}
return db
}