Created
March 23, 2018 18:46
-
-
Save Frando/2f20e12d074edd4f51eb92ee4fd8237c to your computer and use it in GitHub Desktop.
hyperdrive & hypergraph in same hyperdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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