-
Notifications
You must be signed in to change notification settings - Fork 0
/
epvStorePersistentLevelup.js
64 lines (62 loc) · 1.83 KB
/
epvStorePersistentLevelup.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
var level = require("level-browserify")
const { set, unset } = require("./kkvHelpers")
const epvStore = require("./epvStore")
const create = require("lodash/create")
const warnOnErrorCb = err => {
if (err) console.error(err)
}
module.exports = dbName => {
const data = new Map()
const db = level(dbName, { valueEncoding: "json" })
let loadCount = 0
return Promise.resolve()
.then(
() =>
new Promise((resolve, reject) => {
//auto-load
db
.createReadStream()
.on("data", ({ key, value }) => {
const [k1, k2] = JSON.parse(key)
set(data, k1, k2, value)
loadCount++
})
.on("error", reject)
.on("close", reject)
.on("end", resolve)
})
)
.then(() => {
console.log(`${loadCount} entries from ${dbName} store loaded`)
const store = epvStore(data)
const patchAndSave = patch => {
// call memory store patch
store.patch(patch)
// and then persist it
const batch = []
Object.keys(patch).forEach(k1 => {
const subPatch = patch[k1]
const subKeys = Object.keys(subPatch)
subKeys.forEach(k2 => {
const key = JSON.stringify([k1, k2])
const value = subPatch[k2]
if (value == null) {
batch.push({ type: "del", key })
} else {
batch.push({ type: "put", key, value })
}
})
})
return new Promise((resolve, reject) => {
db.batch(batch, err => (err ? reject(err) : resolve()))
})
}
return create(store, {
patch: patchAndSave,
clearAllData: () =>
new Promise((resolve, reject) => {
db.db.idb.clear(resolve, reject)
}),
})
})
}