A CLI for managing LevelDB instances.
This repo is a fork of lev, originally to make those changes available from NPM
- CLI
- providing many basic tools to read and write on a leveldb from the command line
- import / export, and delete by range
- REPL:
⚠️ This feature was unmaintained and removed inv8.0.0. If you need it, use an earlier version or the originallevpackage.
$ npm install -g lev2These all match the parameters used with levelup. The default encoding for the database is set to json.
Get a value
lev --get fooPut a value
lev --put foo --value barDelete a value
lev --del fooCan be used in combination with --keys, or --all, or implicit --all, to generate a stream of delete operations to be passed to the lev --batch command
lev --keys --del | lev --batch
lev --all --del | lev --batch
lev --gte abc --lte abd --del | lev --batchPut or delete several values, using levelup batch syntax
lev --batch '[
{"type":"del","key":"a"},
{"type":"put","key":"b","value":"123"},
{"type":"put","key":"c","value":"456"}
]'or from a file
# there should be one entry per line
# either as valid JSON
echo '[
{"type":"del","key":"a"},
{"type":"put","key":"b","value":"123"},
{"type":"put","key":"c","value":"456"}
]' > ops.json
# or as newline-delimited JSON
echo '
{"type":"del","key":"a"}
{"type":"put","key":"b","value":"123"}
{"type":"put","key":"c","value":"456"}
' > ops.json
lev --batch ./operations.jsonIf the type is omitted, defaults to put, which allows to use the command to do imports/exports, in combination with --all:
# export
lev --all > leveldb.export
# import
lev /tmp/my-new-db --batch leveldb.exportIf it's a large export, you can use compress it on the fly
# export
lev --all | gzip -9 > leveldb.export.gz
# import
gzip -d < leveldb.export.gz | lev /tmp/my-new-db --batchThe --batch option can also be used to delete key/values by range in 2 steps:
# 1 - collect all the key to delete
lev --prefix 'foo' --del > ./deletion_operations
# 2 - pass the file as argument to the --batch option
lev --batch ./deletion_operations
The same can be done with --match
lev --match '*foo*' --del > ./deletion_operations
List all the keys in the current range
lev --keysCan be used in combination with --del to generate a stream of delete operations
lev --keys --del | lev --batchList all the values in the current range. Emit as a new-line delimited stream of json.
lev --valuesList all the keys and values in the current range. Emit as a new-line delimited stream of json.
lev --allIt can be used to create an export of the database, to be imported with --batch
lev --all > leveldb.export
lev /tmp/my-new-db --batch leveldb.exportIt can be used in combinaision with other options, but can then also be omitted as its the default stream mode
lev --all --prefix 'foo'
# is equivalent to
lev --prefix 'foo'Start the range at keys greater than or equal to <key-pattern>. For strictly greater than, use --gt.
# output all keys and values after 'foo' (implicit --all)
lev --gte 'foo'
# output all keys after 'foo'
lev --keys --gte 'foo'
# the same for values
lev --values --gte 'foo'For keys and values strictly greater tha
End the range at keys lower than or equal to <key-pattern>. For strictly lower than, use --.
# output all keys and values before 'fooz' (implicit --all)
lev --lte 'fooz'
# output all keys before 'fooz'
lev --keys --lte 'fooz'
# the same for values
lev --values --lte 'fooz'
# output all keys between 'foo' and 'fooz'
lev --keys --gte 'foo' --lte 'fooz'
# which is equivalent toGet all entries for which the key starts by a given prefix
# get all the keys starting by foo
lev --keys --prefix 'foo'
# which is equivalent to
lev --keys --gte 'foo' --lte 'foo\uffff'Filter results by a pattern applied on the keys
lev --keys --match 'f*'
lev --values --match 'f*'
lev --all --match 'f*'
# Equivalent to
lev --match 'f*'See minimatch doc for patterns
Limit the number of records emitted in the current range.
lev --keys --limit 10
lev --values --prefix 'foo' --limit 100
lev --match 'f*' --limit 10Reverse the stream.
lev --keys --reverse
lev --keys --prefix 'foo' --limit 100 --reverseOutput the count of results in the selected range
# Count all the key/value pairs in the database
lev --count
# Counts the keys and values between 'foo' and 'fooz'
lev --prefix 'foo' --countSpecify the encoding for the values (Defaults to 'utf8').
lev --values --valueEncoding json
lev --values --valueEncoding hex
lev --values --valueEncoding base64Specify the path to the LevelDB to use. Defaults to the current directory.
lev --location /tmp/test-db --keys
# Equivalent to
lev /tmp/test-db --keysPass results in a map function
- either inline
lev --keys --map 'key => key.split(":")[1]'
lev --all --map 'data => data.value.replace(data.key, "")'- or from a JS file that exports a function
# in ./map_fn.js
module.exports = key => key.split(":")[1]lev --keys --map ./map_fn.jsIf the function, returns null or undefined, the result is filtered-out
This can be used to update the whole database:
# Create a map function that returns an object with the key and an updated value
echo 'module.exports = ({ key, value }) => ({
key,
value: value.replace('foo', 'bar')
})' > ./update_values.js
# Create an updated export of the database
lev --map ./update_values.js > ./updated_db
# And re-import
lev --batch ./updated_db