npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

level-create-batch

v1.1.0

Published

insert a batch of keys if and only if none of the keys already exist

Downloads

29

Readme

level-create-batch

insert a batch of keys if and only if none of the keys already exist

build status

example

Suppose you want to create a user account for a website using leveldb.

First you'll need to make sure that the requested username hasn't already been taken. If you do a db.get() to check the username without locking the key first, if two requests come in to register the same username before any data is written, both requests will indicate that the username is available and the database will be left in an inconsistent state.

You can solve the problem of multiple simultaneous create requests for single keys using level-create, but now suppose that when a user creates an account, you also want to create a username and password in a separate key. Ordinarily in leveldb you could do this with db.batch:

db.batch([
  { type: 'put', key: 'user!substack', value: { bio: 'beep boop' } },
  { type: 'put', key: 'login!substack', value: { salt: '...', hash: '...' } }
])

db.batch() is atomic, so if the server crashed between inserting the first key and the second, both keys would not be written. Otherwise, a user might be accidentally created who couldn't log in, or a login might be created for a user that didn't exist.

We lose the atomicity benefits of db.batch() when using individual key locks with level-create, but with level-create-batch, we can do safe multi-record inserts if and only if all the requested keys do not yet exist:

var level = require('level');
var db = level('/tmp/users.db', { valueEncoding: 'json' });
var batch = require('level-create-batch');

var minimist = require('minimist');
var argv = minimist(process.argv.slice(2), {
    alias: { u: 'user', p: 'pass', b: 'bio' }
});

var crypto = require('crypto');
var shasum = require('shasum');

var salt = crypto.randomBytes(32);
var hash = shasum(Buffer.concat([ salt, Buffer(argv.pass) ]));

var rows = [
    {
        key: 'users!' + argv.user,
        value: { bio: argv.bio }
    },
    {
        key: 'login!' + argv.user,
        value: { salt: salt, hash: hash }
    }
];

batch(db, rows, function (err) {
    if (err) {
        console.error(err);
        process.exit(1);
    }
});

Now we can use this program to create accounts, but the program will properly fail to create an account if the username is already taken:

$ node useradd.js -u substack -p beepboop -b whatever
$ node useradd.js -u trex -p dino -b rawr
$ node useradd.js -u substack -p hax -b h4x3d
{ [Error: key already exists] code: 'EXISTS' }

methods

var batch = require('level-create-batch')

batch(db, rows, cb)

Insert a batch of rows into the leveldb database db.

cb(err) fires with any errors when the batch finishes or is aborted. Aside from ordinary database errors, err.code might be:

  • 'LOCKED' - when any of the requested keys are locked
  • 'EXISTS' - when any of the requested keys already exists

If a row has a type property explicitly set to 'put', the data will be inserted whether or not a key already exists. The keys will all still be locked in any case until the operation finishes.

Otherwise the default row type is 'create', which fails if a key already exists.

Rows can individually have keyEncoding and valueEncoding properties, which db.batch() respects.

install

With npm do:

npm install level-create-batch

license

MIT