New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

levelplus

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

levelplus

Adds atomic updates to levelup database

  • 0.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

levelplus

Adds atomic updates to levelup database

Installation

You can install levelplus through npm:

$ npm install levelplus

Example

Atomic Updates

You can do atomic upates of values using db.update with a value:

var db = levelplus(levelup(dbPath,
  { keyEncoding: 'utf8', valueEncoding: 'json' }));

db.update('mykey', 1, function (err) {
  db.update('mykey', 2, function (err) {
    db.get('mykey', function (err, value) {
      expect(value).to.equal(2);
    });
  });
});

Atomic Increments

You can do atomic updates using db.inc;

var db = levelplus(levelup(dbPath,
  { keyEncoding: 'utf8', valueEncoding: 'json' }));

var c = 100;
for (var i = 0; i < 100; i++) {
  db.inc('mykey', 0, function (err) {
    if (err) return done(err);
    --c || next();
  });
}

function next() {
  db.get('mykey', function (err, value) {
    expect(value).to.equal(100);
    done();
  });
}

Atomic Array Pushes

You can do atomic updates using db.push:

var db = levelplus(levelup(dbPath,
  { keyEncoding: 'utf8', valueEncoding: 'json' }));

var c = 100;
for (var i = 0; i < 100; i++) {
  db.push('mykey', i, function (err) {
    if (err) return done(err);
    --c || next();
  });
}

function next() {
  db.get('mykey', function (err, value) {
    if (err) return done(err);
    expect(value).to.be.instanceof(Array);
    expect(value.length).to.equal(100);
    value.forEach(function (n) {
      expect(n).to.be.gte(0);
      expect(n).to.be.lte(100);
    });
    done();
  });
}

Atomic Set Addition

You can do atomic updates using db.sadd (adds to a set):

var db = levelplus(levelup(dbPath,
  { keyEncoding: 'utf8', valueEncoding: 'json' }));

var c = 100;
for (var i = 0; i < 100; i++) {
  db.sadd('mykey', i % 5, function (err) {
    --c || next();
  });
}

function next() {
  db.get('mykey', function (err, value) {
    expect(value).to.be.instanceof(Array);
    expect(value.length).to.equal(5);
    value.forEach(function (n) {
      expect(n).to.be.gte(0);
      expect(n).to.be.lt(5);
    });
    done();
  });
}

Atomic Updates Using Custom Functions

You can do atomic updates using db.update with a custom update function:

var db = levelplus(levelup(dbPath,
  { keyEncoding: 'utf8', valueEncoding: 'json' }));

function times(multiplier, init) {
  return function (data) {
    if (data === undefined) {
      data = init;
    }

    return data*multiplier;
  };
}

db.update('mykey', times(2, 42), function (err) {
  if (err) return done(err);
  db.get('mykey', function (err, value) {
    if (err) return done(err);
    expect(value).to.equal(84);
    done();
  });
});

Keywords

FAQs

Package last updated on 22 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc