-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeystore.js
38 lines (33 loc) · 849 Bytes
/
keystore.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
import { createRandomId } from './utils.js'
export const create = (ram, initialValue, userId) => {
const keystore = {}
for (const key in initialValue) {
setChildByKey(ram, keystore, key, initialValue[key], userId)
}
return keystore
}
export const setChildByKey = (
ram,
keystore,
key,
value,
userId,
version = 0
) => {
const saveAddress = keystore[key] ?? createRandomId()
keystore[key] = saveAddress
return ram.set(value, userId, version, saveAddress)
}
export const getChildByKey = (ram, keystore, key) => {
return ram.get(keystore[key]) ?? null
}
export const getValues = (ram, keystore) => {
const values = {}
for (const key in keystore) {
values[key] = getChildByKey(ram, keystore, key)
}
return values
}
export const getChildAddressByKey = (keystore, key) => {
return keystore[key] ?? null
}