Skip to content

Instantly share code, notes, and snippets.

@frank-dspeed
Forked from Frando/index.js
Created August 18, 2021 11:25
Show Gist options
  • Save frank-dspeed/6378f11b8eef5fbd2dfc5cc41545cc4d to your computer and use it in GitHub Desktop.
Save frank-dspeed/6378f11b8eef5fbd2dfc5cc41545cc4d to your computer and use it in GitHub Desktop.
hyperdrive & hypergraph in same hyperdb
var hyperdb = require('hyperdb')
var sub = require('subhyperdb')
var hyperdrive = require('hyperdrive')
var hypergraph = require('hyper-graph-db')
var hyperdriveMessages = require('hyperdrive/lib/messages')
var dbName = process.argv[2]
var op = process.argv[3]
if (op !== 'read' && op !== 'write') {
console.log('op has to be "read" or "write"')
return 1
}
var db = hyperdb(dbName, {valueEncoding: 'binary', contentFeed: true})
var dbDrive = sub(db, '@drive', {valueEncoding: hyperdriveMessages.Stat})
var dbGraph = sub(db, '@graph', {valueEncoding: 'json'})
var drive = hyperdrive(null, null, {checkout: dbDrive})
var graph = hypergraph(null, null, {db: dbGraph})
db.on('ready', function() {
console.log('db ready')
if (op === 'write') {
write(files)
}
else if (op === 'read') {
read(dbDrive, 'drive')
read(dbGraph, 'graph')
}
})
function read(sub, name) {
var stream = sub.createReadStream()
stream.on('data', function(node) {
console.log(name, node)
})
}
var files = [
{path: 'test.txt', content: 'hello!', title: 'it works' },
{path: 'foo.txt', content: 'bazz!', title: 'yeah' }
]
function write(files) {
files.forEach(function(file) {
var buf = Buffer.from(file.content)
drive.writeFile(file.path, buf, function(err, data) {
if (err) console.log('err', err)
else console.log('write finished')
})
var triple = {
subject: 'file:' + file.path,
predicate: 'title',
object: file.title
}
graph.put(triple, function(err) {
if (err) return console.log('error!', err)
})
})
}
{
"name": "hyperdb-experiments",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"hyper-graph-db": "github:frando/hyper-graph-db#master",
"hyperdb": "github:mafintosh/hyperdb#master",
"hyperdrive": "github:mafintosh/hyperdrive#hyperdb-backend",
"subhyperdb": "github:frando/subhyperdb#master"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Franz Heinzmann",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment