Created
September 11, 2015 01:29
-
-
Save elf-pavlik/2e34221b4affdacf01a4 to your computer and use it in GitHub Desktop.
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 Promise = require('promise'); | |
var concat = require('concat-stream'); | |
module.exports = function(fdb) { | |
this.read = function(key){ | |
return new Promise(function(resolve, reject){ | |
var headsCS = concat(function(hashes) { | |
var readCS = concat(function(buffer){ | |
resolve(JSON.parse(buffer.toString())); | |
}); | |
fdb.createReadStream(hashes[0].hash).pipe(readCS); | |
}); | |
fdb.heads(key).pipe(headsCS); | |
}); | |
}; | |
this.write = function(doc, prev) { | |
return new Promise(function(resolve, reject){ | |
var meta = { key: doc.key }; | |
if (prev) meta.prev = prev; | |
fdb.createWriteStream(meta, function(err, id) { | |
if (err) reject(err); | |
else resolve(doc); | |
}).end(JSON.stringify(doc)); | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment