Level / memdown

In-memory abstract-leveldown store for Node.js and browsers.
MIT License
287 stars 37 forks source link

Can I get a JSON/Buffer/WritableStream of MemDOWN for storage? #65

Closed ohenepee closed 7 years ago

ohenepee commented 7 years ago

I want to use levelgraph, which requires levelup, which in-turn requires either leveldown/memdown/level.js... Unfortunately the platform is mobile without an IndexedDB (Fuse to be precise ) so I can't use level.js. So I was thinking there was a way to work with memdown and write the database to the filesystem, then load it anytime I need it

vweevers commented 7 years ago

Perhaps you can use jsondown or sqldown with SQLite? If you want to combine backends, see cachedown or level-live-cache.

ohenepee commented 7 years ago

Awesome!!! Thanks a bunch

juliangruber commented 7 years ago

If you want to use memdb still, you could import / export data on demand like this:

var fs = require('fs')
var JSONStream = require('JSONStream')
var WriteStream = require('level-ws').WriteStream

function store (db, file, cb) {
  db.createReadStream()
    .pipe(JSONStream.stringify())
    .pipe(fs.createWriteStream(file))
    .on('finish', cb)
}

function restore (db, file, cb) {
  fs.createReadStream(file)
    .pipe(JSONStream.parse([true]))
    .pipe(WriteStream(db))
    .on('end', cb)
}
ohenepee commented 7 years ago

Yeah I think this approach is much efficient and performance since jsondown isn't streaming and sqldown is for me like "really?". Also both seem to have been abandoned, my guess.

austinfrey commented 7 years ago

it looks like the above example would save the entire contents of the DB to a .json file every save? is that correct? are there examples of being able to save just a single batch or put to disk rather than the entire DB, assuming levelDOWN is not an option.

ralphtheninja commented 7 years ago

it looks like the above example would save the entire contents of the DB to a .json file every save? is that correct?

Yep, that's correct.

are there examples of being able to save just a single batch or put to disk rather than the entire DB, assuming levelDOWN is not an option.

You could tweak the above store and restore functions to just stream stuff you're interested in, that could be an option. But I don't know anything about storing/restoring single batches or puts. Another thing you can do is to use sublevels e.g. subleveldown and call store/restore on single sublevels.