-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
65 lines (54 loc) · 1.97 KB
/
example.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
65
const BeeDiffStream = require('.')
const Hyperbee = require('hyperbee')
const ram = require('random-access-memory')
const Autobase = require('autobase')
const Corestore = require('corestore')
async function main () {
// Setting up a simple hyperbee view for an autobase
const opts = {
apply: async (batch, view) => {
try {
for (const { value } of batch) {
if (value.add) await view.put(...value.add, { update: false })
else if (value.del) await view.del(value.del, { update: false })
}
} catch (e) {
console.error(e)
}
},
open: linStore => new Hyperbee(linStore.get('abee'), {
extension: false,
keyEncoding: 'utf-8',
valueEncoding: 'utf-8'
}),
valueEncoding: 'json' // the apply function will receive batches of jsons
}
const store = new Corestore(ram)
const base = new Autobase(store, null, opts)
await base.append({ add: ['e1', 'entry1'] })
await base.append({ add: ['e2', 'entry2'] })
const oldSnap = base.view.snapshot()
await base.append({ add: ['e3', 'A'] })
await base.append({ add: ['e3', 'lot'] })
await base.append({ add: ['e3', 'of'] })
await base.append({ add: ['e3', 'changes'] })
await base.append({ add: ['e3', 'to'] })
await base.append({ add: ['e3', 'entry3'] })
await base.append({ del: 'e1' })
await base.append({ add: ['e2', 'new entry 2'] })
await base.append({ del: 'e2' })
await base.append({ add: ['e1', 'Something-else'] })
const newSnap = base.view.snapshot()
const diffStream = new BeeDiffStream(oldSnap, newSnap)
for await (const { left: added, right: removed } of diffStream) {
if (added && !removed) console.log('- Set', added.key, 'to', added.value)
if (!added && removed) console.log('- Removed', removed.key)
if (added && removed) console.log('- Updated', added.key, 'from', removed.value, 'to', added.value)
}
/*
- Updated e1 from entry1 to Something-else
- Removed e2
- Added e3 entry3
*/
}
main()