0.1.1 • Published 10 years ago

level-inc v0.1.1

Weekly downloads
9
License
-
Repository
github
Last release
10 years ago

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"
    })
  })
}