-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
37 lines (29 loc) · 931 Bytes
/
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
const levelup = require('levelup')
const randomAccessKeyValue = require('random-access-key-value')
const hypercore = require('hypercore')
const tmp = require('tmp')
const realmdown = require('.')
var db = levelup(realmdown({ path: `${tmp.dirSync().name}/db` }))
function run (cb) {
console.log('example using levelup \n')
db.put('foo', 'bar', function (err) {
if (err) throw err
db.get('foo', function (err, value) {
if (err) throw err
console.log(value.toString()) // 'bar'
cb()
})
})
}
function runHypercore () {
console.log('\nexample using hypercore\n')
const storage = filename => randomAccessKeyValue(db, filename)
const feed = hypercore(storage, { valueEncoding: 'utf8' })
feed.append('hello')
feed.append('world', function (err) {
if (err) throw err
feed.get(0, console.log) // prints hello
feed.get(1, console.log) // prints world
})
}
run(runHypercore)