-
Notifications
You must be signed in to change notification settings - Fork 7
/
20171031T182007_pvar_index.js
60 lines (54 loc) · 1.42 KB
/
20171031T182007_pvar_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
58
59
60
var _ = require('lodash')
var ktypes = require('krl-stdlib/types')
var dbRange = require('../dbRange')
module.exports = {
up: function (ldb, callback) {
var dbOps = []
var onKV = function (data) {
var keyPrefix = data.key
var val = data.value
// NOTE: not sharing code with DB.js b/c migrations should be immutable
// i.e. produce the same result regardless of previous codebase states
var indexType = ktypes.typeOf(val)
var rootValue = { type: indexType }
switch (indexType) {
case 'Null':
rootValue.value = null
break
case 'Function':
case 'Action':
rootValue.type = 'String'
rootValue.value = ktypes.toString(val)
break
case 'Map':
case 'Array':
_.each(val, function (v, k) {
dbOps.push({
type: 'put',
key: keyPrefix.concat(['value', k]),
value: v
})
})
break
default:
rootValue.value = val
}
dbOps.push({
type: 'put',
key: keyPrefix,
value: rootValue
})
}
dbRange(ldb, {
prefix: ['entvars']
}, onKV, function (err) {
if (err) return callback(err)
dbRange(ldb, {
prefix: ['appvars']
}, onKV, function (err) {
if (err) return callback(err)
ldb.batch(dbOps, callback)
})
})
}
}