/
3-diffs.js
55 lines (47 loc) · 2.08 KB
/
3-diffs.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
const chalk = require('chalk')
const hypercore = require('hypercore')
const ram = require('random-access-memory')
const Hyperbee = require('hyperbee')
start()
async function start () {
const db = new Hyperbee(hypercore(ram), {
keyEncoding: 'utf-8',
valueEncoding: 'utf-8'
})
await db.ready()
// Let's insert a bunch of KV-pairs of the form 'a' -> 'a' etc.
const keys = 'abcdefghijkl'
for (const char of keys) {
await db.put(char, char)
console.log(`Version after inserting ${char}: ${db.version}`)
}
// The createDiffStream method allows us to observe differences between versions of the Hyperbee.
// Let's see what's changed between the latest version, and version 9.
console.log(chalk.green('\nDiff between the latest version, and version 9:\n'))
for await (const { left, right } of db.createDiffStream(9)) {
// Since we've only inserted values, `right` will always be null.
console.log(`left -> ${left.key}, right -> ${right}`)
}
// Before modifying the database, let's record the current database version.
const oldVersion = db.version
// Now let's delete keys 'k' and 'l', insert key 'm', and modify 'a':
await db.del('k')
await db.del('l')
await db.put('m', 'm')
await db.put('a', 'new a')
console.log(chalk.green('\nDiff after modifications:\n'))
for await (const { left, right } of db.createDiffStream(oldVersion)) {
// For keys 'k' and 'l', `right` is set because it's a deletion.
// For 'm', `left` is set because it's a new insertion.
// For 'a', both `left` and `right` are set because it's a modification
console.log((`left -> ${left && left.key}, right -> ${right && right.key}`))
}
// We can also check out a database snapshot for an old version
// With the snapshot, we can re-create the original diff output.
const snapshot = db.checkout(oldVersion)
console.log(chalk.green('\nSnapshot diff to version 9:\n'))
for await (const { left, right } of snapshot.createDiffStream(9)) {
// Since we've only inserted values, `right` will always be null.
console.log(`left -> ${left.key}, right -> ${right}`)
}
}