p2p key/value store over a hyperlog using a multi-value register conflict strategy
var hyperkv = require('hyperkv')
var hyperlog = require('hyperlog')
var sub = require('subleveldown')
var level = require('level')
var db = level('/tmp/kv.db')
var kv = hyperkv({
log: hyperlog(sub(db, 'log'), { valueEncoding: 'json' }),
db: sub(db, 'kv')
})
var key = process.argv[2]
var value = process.argv[3]
kv.put(key, value, function (err, node) {
if (err) console.error(err)
else console.log(node.key)
})
$ node example/put.js greeting hello
1f1b819cd3f7d379914037b473a855b7867f71c76126e379c91cbb31df2a859b
$ node example/put.js greeting 'beep boop'
eadb22a224313d5fb5b811e50915f16491e7714dd32b83503c1e1a1db2bd9e9b
var hyperkv = require('hyperkv')
var hyperlog = require('hyperlog')
var sub = require('subleveldown')
var level = require('level')
var db = level('/tmp/kv.db')
var kv = hyperkv({
log: hyperlog(sub(db, 'log'), { valueEncoding: 'json' }),
db: sub(db, 'kv')
})
var key = process.argv[2]
kv.get(key, function (err, value) {
if (err) console.error(err)
else console.log(value)
})
$ node example/get.js greeting
{ eadb22a224313d5fb5b811e50915f16491e7714dd32b83503c1e1a1db2bd9e9b: 'beep boop' }
var hyperkv = require('hyperkv')
Set key
to a json value
.
If opts.links
is set, refer to previously set keys. Otherwise, the key will
refer to the current "head" key hashes.
cb(err, node)
fires from the underlying log.add()
call.
Get the list of current values for key
as cb(err, values)
where values
maps hyperlog hashes to set values.
If there are no known values for key
, values
will be {}
.
Remove key
.
If opts.links
is set, refer to previously set keys. Otherwise, the key will
refer to the current "head" key hashes.
Note that keys are only removed with respect to opts.links
, not globally and
that edits made in forks may cause deleted keys to "reappear". This is by
design.
cb(err, node)
fires from the underlying log.add()
call.
Insert an array of documents rows
atomically into the database.
Each row
object in the rows
array should have:
row.type
- required, one of:'put'
or'del'
row.key
- required key stringrow.value
- value, required whenrow.type === 'put'
row.links
- optional array of ancestor hashes, defaults to most recent heads for the key
cb(err, nodes)
fires from the underlying log.batch()
call.
Create a readable object mode stream
for each key/values in the store.
Each object row
has:
row.key
- the key set with.put()
row.links
- array of hashes that are the current holders for the keyrow.values
- object mapping hashes to values
Optionally:
opts.values
- set tofalse
to turn off settingrow.values
, which requires an extra lookup in the implementation
Create a readable object mode stream
with the history of key
.
Each row
object in the output stream has:
row.key
- the key (as in key/value) of the documentrow.link
- the hyperlog key (version hash) of the current documentrow.links
- array of version hashes that are ancestors of this documentrow.value
- value associated with this document
You might want to topologically sort the output before displaying it. Otherwise, documents will always appear before their ancestors, but documents in a fork have an undefined ordering.
Whenever a node is put, this event fires.
Whenever the indexes update through a put or replication, this event fires with
the underlying node
object from the hyperlog.
This package ships with a hyperkv
command.
hyperkv put KEY VALUE {OPTIONS}
Insert a json VALUE at KEY.
--links Comma-separated list of ancestor hashes
hyperkv get KEY
Print a json object for the values at KEY,
mapping hashes to values.
hyperkv list
Print a list of keys and values as json, one per line.
hyperkv push
hyperkv pull
hyperkv sync
Replicate with another hyperkv using stdin and stdout.
npm install hyperkv
npm install -g hyperkv
BSD