Increment leveldb keys
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE.md
README.md
index.js
package.json
test.js

README.md

level-inc Build Status

An increment call for levelup.

Why? Because if you're counting up a lot, the gap between a get and a put becomes important:

for (var i = 0; i < 100; i += 1) {
  db.get('a-key', function(err, val) {
    val = parseInt(val || 0, 10)
    val += 1

    db.put('a-key', val)
  })
}

The above example will probably result in a-key being set to 1 and staying there. level-inc keeps track of overlapping calls like this and handles them cleanly.

Installation

npm install level-inc

Usage

require('level-inc')(db)(key, amount, callback)

var db = require('levelup')(__dirname + '/db')
var inc = require('level-inc')(db)
var counter = 0

db.inc = inc

for (var i = 0; i < 200; i += 1) {
  db.inc('some-key', 10, function() {
    counter += 1
    if (counter !== 200) return
    db.get('some-key', function(err, val) {
      // val === "2000"
    })
  })
}