level-create

1.1.0 • Public • Published

level-create

insert a key if and only if it doesn't already exist

build status

example

var level = require('level');
var db = level('/tmp/users.db', { valueEncoding: 'json' });
var create = require('level-create');
 
var key = 'users!' + process.argv[2];
var value = { bio: process.argv[3] };
 
create(db, key, value, function (err) {
    if (err) {
        console.error(err);
        process.exit(1)
    }
});

The first time this file is run with a username, it succeeds. The second time, the program fails because the username already exists.

$ node example/useradd.js substack
$ node example/useradd.js substack
{ [Error: key already exists] code: 'EXISTS' }

If multiple requests try to create a key at the same time, the first key obtains a lock that prevents other updates from succeeding.

methods

var create = require('level-create')

create(db, key, value, cb)

Put value at key into the leveldb db if and only if the key doesn't already exist in the database.

cb(err) fires with an err.code of 'EXISTS' when the requested key already exists and an err.code of 'LOCKED' when the key has been write-locked by another simultaneous request.

cb(err) will also forward errors from the underlying db.put()

The keyEncoding of db is respected for the provided key, which is automatically encoded into the right format for setting locks.

install

With npm do:

npm install level-create

license

MIT

Package Sidebar

Install

npm i level-create

Weekly Downloads

2

Version

1.1.0

License

MIT

Last publish

Collaborators

  • nopersonsmodules