Skip to content

hughsk/level-inc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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