Manage many hyperdbss
npm install multihyperdb
var multi = require('multihyperdb')
var opts = {
path: './db',
dbOpts: {valueEncoding: 'json'}
}
var Multi = multi(opts)
multi.createDB({}, {name: 'First database'}, function(err, db) {
db.put('hello', 'world', function (err, node) { } )
})
Create a new MultiHyperDB. Stores info about multiple hyperdbs in a master hyperdb.
Options include:
{
path: './db' // Path to store the databases in
masterPath, dbPath // Set basepath for master databases and child databases manually
dbOpts // default hyperdb opts for child databases
storage // callback to return a random-access-storage (see below)
}
Create a new hyperdb. opts
are regular hyperdb opts (they override the global dbOpts
). meta
may have any JSON to be stored in the master hyperdb for this child db. cb
will be called with (err, db)
after the database is ready.
Add a hyperdb with key key
. Other options as in createDB
.
Drop database key
from being managed in this MultiHyperDB. Pass {delete: true}
as opts to physically delete the database from disc.
Execute a callback for each database. callback
s parameters are (db, key, meta)
.
Get a combined read stream on all child databases. The on('data'
callback receives an object with properties node
(the node as returned from a regular hyperdb read stream, with key
and value
properties) and dbKey
(the key of the database).
Get a database by key.
Get a database by meta property. If single
is true, only the first matching database is returned.
By default, both master and child dbs are created with random-access-file storage in the path(s) configured via opts. Alternatively, pass a function as opts.storage
.
opts.storage = function(master, key, destroy, cb) {
}
If destroy
is false, return a random-access-storage for either master (if master
is true
) or the child database with key key
. If destroy is true
, destroy (delete) the child database with key key
and call the callback cb
.
... is as simple as
var multi = require('multihyperdb')
var hyperdiscovery = require('hyperdiscovery')
var Multi = multi({path: './db1'})
// Create some databases or add existing keys.
// Two-way sync them all!
Multi.each(function(db) {
hyperdiscovery(db, {live: true})
})
Try it out with example-cli.js:
$ node example-cli.js db1 create myfirstdb
db created with key: d7f69b22386fdc7751fb26e6a458a45c1a5d369525ac0e428eaf0331f59ff0e1
$ node example-cli.js db1 write myfirstdb hello world
Wrote node: Node(key=hello, value='world', seq=0, feed=0))
$ node example-cli.js db1 share
# In another terminal or on another computer
$ node example-cli.js db2 add remotedb d7f69b22386fdc7751fb26e6a458a45c1a5d369525ac0e428eaf0331f59ff0e1
$ node example-cli.js db2 share
# wait, then abort
$ node example-cli.js db2 read
Node(key=hello, value='world', seq=0, feed=0))
# This was synced from db1.myfirstdb !