Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including level-multiply with all npm packages installed. Try it out:

var levelMultiply = require("level-multiply")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

level-multiply v0.0.1

Make LevelUP's get(), put() and del() accept multiples keys & values

Level-Multiply Build Status

LevelDB Logo

Make your LevelUP get(), put() and del() accept multiples keys & values.

Augment the standard implementations to take multiple keys and values and do the correct thing with them:

var levelup       = require('levelup')
  , levelMultiply = require('level-multiply')

levelup('/tmp/foo.db', function (err, db) {
  db = levelMultiply(db)

  // ------ put() -------- //

  // works like normal
  db.put('foo', 'bar', function (err) { /* .. */ })

  // turned into an atomic batch() exclusively composed of
  // 'put' operations.
  db.put({ foo: 'bar', boom: 'bang', whoa: 'dude' }, function (err) {
    // database now contains these 3 entries:
    //   foo=bar
    //   boom=bang
    //   whoa=dude

  // ------ get() -------- //

  // works like normal
  db.get('foo', function (err, value) { /* .. */ })

  // 4 separate get() operations combined and returned in a single object
  db.get([ 'foo', 'boom', 'whoa', 'wha' ], function (err, data) {
    // `data` looks like this:
    // {
    //     foo  : 'bar'
    //   , boom : 'bang'
    //   , whoa : 'dude'
    //   , wha  : null
    // }
    // note that missing values will be converted to `null`

  // ------ del() -------- //

  // works like normal
  db.del('foo', function (err, value) { /* .. */ })

  // 4 separate del() operations combined, callback triggered when
  // all have completed
  db.del([ 'foo', 'boom', 'whoa', 'wha' ], function (err, data) {
    // database no longer has 'foo', 'boom' or 'whoa' ('wha' was never there)

Hey! Don't molest my original methods!

OK, perhaps this causes problems for you because you're using JSON keys, so an operation like: get([ 'foo', 'bar' ]...) is a legitimate call because it could be converted to a key of '["foo","bar"]'. In this case, Level-Multiply is going to get in your way and intercept those calls. Or perhaps you just don't like monkey patching; fair enough.

levelup('/tmp/foo.db', function (err, db) {
  db = levelMultiply(db, 'multi')

  // Now your original db.get(), db.put() and db.del() methods
  // remain untouched but you have new ones, prefixed with 'multi'.
  // Note that this prefix can be whatever you like, just supply it
  // to the initialiser function above

  db.multiput({ foo: 'bar', boom: 'bang' }, function (err) { /* .. */ })

  db.multiget([ 'foo', 'boom' ], function (err, data) { /* .. */ })

  db.multidel([ 'foo', 'boom' ], function (err, data) { /* .. */ })


Both put() and del() operations are converted directly to batch() operations so your entries will be put or deleted atomically. As we don't (quite) yet have snapshots exposed through LevelUP/LevelDOWN there are no equivalent consistency guarantees about get(). While they will be queued asynchronously and will likely happen sequentially, or close to sequentially ... but they may not. If this kind of consistency is important to you then file an issue on LevelUP or LevelDOWN and moan about having snapshots implemented; once they are available, a multi-get() operation can operate on a consistent point-in-time view of the database.


Level-Multiply is Copyright (c) 2013 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.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free