An drop-in replacement for LevelDOWN that works in memory only


A drop-in replacement for LevelDOWN that works in memory only. Can be used as a back-end for LevelUP rather than an actual LevelDB store.

As of version 0.7, LevelUP allows you to pass a 'db' option when you create a new instance. This will override the default LevelDOWN store with a LevelDOWN API compatible object. MemDOWN conforms exactly to the LevelDOWN API but only performs operations in memory, so your data is discarded when the process ends or you release a reference to the database.

var levelup = require('levelup')
  // note that if multiple instances point to the same location, 
  // the db will be shared, but only per process 
  , db = levelup('/some/location', { db: require('memdown') })
db.put('name', 'Yuri Irsenovich Kim')
db.put('dob', '16 February 1941')
db.put('spouse', 'Kim Young-sook')
db.put('occupation', 'Clown')
  .on('data', console.log)
  .on('close', function () { console.log('Show\'s over folks!') })

Note in this example we're not even bothering to use callbacks on our .put() methods even though they are async. We know that MemDOWN operates immediately so the data will go straight into the store.

Running our example gives:

{ key: 'dob', value: '16 February 1941' }
{ key: 'name', value: 'Yuri Irsenovich Kim' }
{ key: 'occupation', value: 'Clown' }
{ key: 'spouse', value: 'Kim Young-sook' }
Show's over folks!

Even though it's in memory the location parameter does do something. Memdown has a global cache which it uses to save databases by the path string. You may clear this via the Memdown.clearGlobalStore function. By default it doesn't delete the store but replaces it with a new one, open instance of memdown will not be affected. clearGlobalStore takes a single parameter, which if truthy clears the store strictly by deleting each individual key. If there is an in use instance of memdown this will cause the in use memdown instance to error.

MemDOWN is Copyright (c) 2013-2015 Rod Vagg @rvagg and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.