level-create
insert a key if and only if it doesn't already exist
example
var level = ;var db = ;var create = ;var key = 'users!' + processargv2;var value = bio: processargv3 ;;
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 =
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