Last active
January 10, 2022 14:56
-
-
Save fergiemcdowall/5375331 to your computer and use it in GitHub Desktop.
A Gist to demonstrate writing and reading image files to LevelDB via the node module level up
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 levelup = require('levelup') | |
var fs = require('fs'); | |
var db = levelup('./imagedb') | |
db.put('name', fs.readFileSync('image.png'), { encoding: 'binary' }, function (err) { | |
db.get('name', { encoding: 'binary' }, function (err, value) { | |
fs.writeFile('image-copy.png', value, function (err) { | |
console.log('image-copy.png saved!'); | |
}); | |
}) | |
}) |
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": "LevelDBFileReader", | |
"version": "0.0.1", | |
"private": true, | |
"scripts": { | |
"start": "node LevelDBFileReader.js" | |
}, | |
"dependencies": { | |
"levelup": "*" | |
} | |
} |
i somehow got a this.db.createWriteStream is not a function
when try to make a stream, i might be missing something obvious :(
here's my relevant code
const db = levelup(`${__dirname}/db`, { valueEncoding: 'binary' });
const store = Store(db);
const upload = multer({ dest: './uploads/' });
app.post('/', upload.single('image'), (req, res, next) => {
store.createWriteStream(`${__dirname}/${req.file.path}`)
...
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here's how to do it in a streaming way: