Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Increment leveldb keys

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 LICENSE.md
Octocat-spinner-32 README.md
Octocat-spinner-32 index.js
Octocat-spinner-32 package.json
Octocat-spinner-32 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"
    })
  })
}
Something went wrong with that request. Please try again.