A CLI and a REPL for managing LevelDB instances
Clone or download
Pull request Compare This branch is 63 commits ahead of hxoht:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.githooks/pre-commit
bin
docs
lib
scripts
.gitignore
.levrc
CHANGELOG.md
LICENSE
README.md
index.js
package-lock.json
package.json

README.md

lev2

A CLI and a REPL for managing LevelDB instances.

This repo is a fork of lev, originally to make those changes available from NPM

Features

  • CLI
    • providing many basic tools to read and write on a leveldb from the command line
    • import / export, and delete by range
  • REPL
    • with colorized tab-completion and zsh/fish style key suggestions
    • automatically saves and reloads REPL history

screenshot

Summary

Installation

$ npm install -g lev2

CLI commands

These all match the parameters used with levelup. The default encoding for the database is set to json.

--get <key>

Get a value

lev --get foo

--put <key>

Put a value

lev --put foo --value bar

--del <key>

Delete a value

lev --del foo

--batch <operations>

Put or delete several values, using levelup batch syntax

lev --batch '[
{"type":"del","key":"father"},
{"type":"put","key":"name","value":"Yuri Irsenovich Kim"},
{"type":"put","key":"dob","value":"16 February 1941"},
{"type":"put","key":"spouse","value":"Kim Young-sook"},
{"type":"put","key":"occupation","value":"Clown"}
]'

or from a file

# there should be one entry per line
# either as valid JSON
echo '[
{"type":"del","key":"father"},
{"type":"put","key":"name","value":"Yuri Irsenovich Kim"},
{"type":"put","key":"dob","value":"16 February 1941"},
{"type":"put","key":"spouse","value":"Kim Young-sook"},
{"type":"put","key":"occupation","value":"Clown"}
]' > ops.json
# or as newline-delimited JSON
echo '
{"type":"del","key":"father"}
{"type":"put","key":"name","value":"Yuri Irsenovich Kim"}
{"type":"put","key":"dob","value":"16 February 1941"}
{"type":"put","key":"spouse","value":"Kim Young-sook"}
{"type":"put","key":"occupation","value":"Clown"}
' > ops.json
lev --batch ./ops.json

Import / Export

If 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.export

If 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 --batch

Delete by range

The --batch option can also be used to delete key/values by range in 2 steps:

# 1 - collect all the key/values to delete
lev --all --start 'foo' --end 'fooz' > ./to_delete
# 2 - pass the file as argument to the --batch option with a --del flag
lev --batch ./to_delete --del

--keys

List all the keys in the current range

lev --keys

--values

List all the values in the current range. Emit as a new-line delimited stream of json.

lev --values

--all

List all the keys and values in the current range. Emit as a new-line delimited stream of json.

lev --all

It 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.export

--start <key-pattern>

Specify the start of the current range. You can also use gt or gte.

# output all keys after 'foo'
lev --keys --start 'foo'
# which is equivalent to
lev --keys --gte 'foo'
# the same for values
lev --values --start 'foo'

--end <key-pattern>

Specify the end of the current range. You can also use lt and lte.

# output all keys before 'fooz'
lev --keys --end 'fooz'
# which is equivalent to
lev --keys --lte 'fooz'
# the same for values
lev --values --end 'fooz'
# output all keys between 'foo' and 'fooz'
lev --keys --start 'foo' --end 'fooz'

--match <key-pattern>

Filter keys or values by a pattern applied on the key

lev  --keys --match 'f*'
lev  --values --match 'f*'
lev  --all --match 'f*'
# Equivalent to
lev --match 'f*'

See minimatch doc for patterns

--limit <number>

Limit the number of records emitted in the current range.

lev --keys --limit 10
lev --values --start 'foo' --end 'fooz' --limit 100
lev --match 'f*' --limit 10

--reverse

Reverse the stream.

lev --keys --reverse
lev --keys --start 'foo' --end 'fooz' --limit 100 --reverse

--count

Output the count of the current range

# Count all the key/value pairs in the database
lev --count
# Counts the keys and values between 'foo' and 'fooz'
lev --start 'foo' --end 'fooz' --count

--valueEncoding <string>

Specify the encoding for the values (Defaults to 'json').

lev --values --valueEncoding buffer

--location <string>

Specify the path to the LevelDB to use. Defaults to the current directory.

lev --location /tmp/test-db --keys
# Equivalent to
lev /tmp/test-db --keys

--map <JS function string or path>

Pass streams 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.js

If the function, returns null or undefined, the result is filtered-out

REPL

Start the REPL

# in the current directory
$ lev
# somewhere else
$ lev path/to/db

Use upper or lower case for the following commands.

GET <key>

Get a key from the database.

PUT <key> <value>

Put a value into the database. If you have keyEncoding or valueEncoding set to json, these values will be parsed from strings into json.

DEL <key>

Delete a key from the database.

LS

Get all the keys in the current range.

START <key-pattern>

Defines the start of the current range. You can also use GT or GTE.

END <key-pattern>

Defines the end of the current range. You can also use LT or LTE.

LIMIT <number>

Limit the number of records in the current range (defaults to 5000).

REVERSE

Reverse the records in the current range.