• Stars
    star
    146
  • Rank 252,769 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A convenience package bundling levelup and rocksdb.

level-rocksdb

Fast & simple storage. A Node.js-style RocksDB wrapper.

level badge npm Node version Test Coverage Standard Common Changelog Donate

Introduction

This is a convenience package that:

Use this package to avoid having to explicitly install rocksdb when you want to use RocksDB with levelup. See also level which does the same for LevelDB.

Usage

If you are upgrading: please see UPGRADING.md.

const level = require('level-rocksdb')

// 1) Create our database, supply location and options.
//    This will create or open the underlying RocksDB store.
const db = level('./mydb')

// 2) Put a key & value
db.put('name', 'Level', function (err) {
  if (err) return console.log('Ooops!', err) // some kind of I/O error

  // 3) Fetch by key
  db.get('name', function (err, value) {
    if (err) return console.log('Ooops!', err) // likely the key was not found

    // Ta da!
    console.log('name=' + value)
  })
})

With async/await:

await db.put('name', 'Level')
const value = await db.get('name')

API

See levelup and rocksdb for more details.

const db = level(location[, options[, callback]])

The main entry point for creating a new levelup instance.

  • location path to the underlying LevelDB.
  • options is passed on to the underlying store.
  • options.keyEncoding and options.valueEncoding are passed to encoding-down, default encoding is 'utf8'

Calling level('./db') will also open the underlying store. This is an asynchronous operation which will trigger your callback if you provide one. The callback should take the form function (err, db) {} where db is the levelup instance. If you don't provide a callback, any read & write operations are simply queued internally until the store is fully opened.

This leads to two alternative ways of managing a levelup instance:

level(location, options, function (err, db) {
  if (err) throw err

  db.get('foo', function (err, value) {
    if (err) return console.log('foo does not exist')
    console.log('got foo =', value)
  })
})

Versus the equivalent:

// Will throw if an error occurs
const db = level(location, options)

db.get('foo', function (err, value) {
  if (err) return console.log('foo does not exist')
  console.log('got foo =', value)
})

The constructor function has a .errors property which provides access to the different error types from level-errors. See example below on how to use it:

level('./db', { createIfMissing: false }, function (err, db) {
  if (err instanceof level.errors.OpenError) {
    console.log('failed to open database')
  }
})

db.open([callback])

Opens the underlying store. In general you should never need to call this method directly as it's automatically called by levelup().

However, it is possible to reopen the store after it has been closed with close(), although this is not generally advised.

If no callback is passed, a promise is returned.

db.close([callback])

close() closes the underlying store. The callback will receive any error encountered during closing as the first argument.

You should always clean up your levelup instance by calling close() when you no longer need it to free up resources. A store cannot be opened by multiple instances of levelup simultaneously.

If no callback is passed, a promise is returned.

db.put(key, value[, options][, callback])

put() is the primary method for inserting data into the store. Both key and value can be of any type as far as levelup is concerned.

options is passed on to the underlying store.

If no callback is passed, a promise is returned.

db.get(key[, options][, callback])

get() is the primary method for fetching data from the store. The key can be of any type. If it doesn't exist in the store then the callback or promise will receive an error. A not-found err object will be of type 'NotFoundError' so you can err.type == 'NotFoundError' or you can perform a truthy test on the property err.notFound.

db.get('foo', function (err, value) {
  if (err) {
    if (err.notFound) {
      // handle a 'NotFoundError' here
      return
    }
    // I/O or other error, pass it up the callback chain
    return callback(err)
  }

  // .. handle `value` here
})

options is passed on to the underlying store.

If no callback is passed, a promise is returned.

db.del(key[, options][, callback])

del() is the primary method for removing data from the store.

db.del('foo', function (err) {
  if (err)
    // handle I/O or other error
});

options is passed on to the underlying store.

If no callback is passed, a promise is returned.

db.batch(array[, options][, callback]) (array form)

batch() can be used for very fast bulk-write operations (both put and delete). The array argument should contain a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation inside the underlying store.

Each operation is contained in an object having the following properties: type, key, value, where the type is either 'put' or 'del'. In the case of 'del' the value property is ignored. Any entries with a key of null or undefined will cause an error to be returned on the callback and any type: 'put' entry with a value of null or undefined will return an error.

If key and value are defined but type is not, it will default to 'put'.

const ops = [
  { 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' }
]

db.batch(ops, function (err) {
  if (err) return console.log('Ooops!', err)
  console.log('Great success dear leader!')
})

options is passed on to the underlying store.

If no callback is passed, a promise is returned.

db.batch() (chained form)

batch(), when called with no arguments will return a Batch object which can be used to build, and eventually commit, an atomic batch operation. Depending on how it's used, it is possible to obtain greater performance when using the chained form of batch() over the array form.

db.batch()
  .del('father')
  .put('name', 'Yuri Irsenovich Kim')
  .put('dob', '16 February 1941')
  .put('spouse', 'Kim Young-sook')
  .put('occupation', 'Clown')
  .write(function () { console.log('Done!') })

batch.put(key, value)

Queue a put operation on the current batch, not committed until a write() is called on the batch.

This method may throw a WriteError if there is a problem with your put (such as the value being null or undefined).

batch.del(key)

Queue a del operation on the current batch, not committed until a write() is called on the batch.

This method may throw a WriteError if there is a problem with your delete.

batch.clear()

Clear all queued operations on the current batch, any previous operations will be discarded.

batch.length

The number of queued operations on the current batch.

batch.write([callback])

Commit the queued operations for this batch. All operations not cleared will be written to the underlying store atomically, that is, they will either all succeed or fail with no partial commits.

If no callback is passed, a promise is returned.

db.isOpen()

A levelup instance can be in one of the following states:

  • "new" - newly created, not opened or closed
  • "opening" - waiting for the underlying store to be opened
  • "open" - successfully opened the store, available for use
  • "closing" - waiting for the store to be closed
  • "closed" - store has been successfully closed, should not be used

isOpen() will return true only when the state is "open".

db.isClosed()

See isOpen()

isClosed() will return true only when the state is "closing" or "closed", it can be useful for determining if read and write operations are permissible.

db.createReadStream([options])

Returns a Readable Stream of key-value pairs. A pair is an object with key and value properties. By default it will stream all entries in the underlying store from start to end. Use the options described below to control the range, direction and results.

db.createReadStream()
  .on('data', function (data) {
    console.log(data.key, '=', data.value)
  })
  .on('error', function (err) {
    console.log('Oh my!', err)
  })
  .on('close', function () {
    console.log('Stream closed')
  })
  .on('end', function () {
    console.log('Stream ended')
  })

You can supply an options object as the first parameter to createReadStream() with the following properties:

  • gt (greater than), gte (greater than or equal) define the lower bound of the range to be streamed. Only entries where the key is greater than (or equal to) this option will be included in the range. When reverse=true the order will be reversed, but the entries streamed will be the same.

  • lt (less than), lte (less than or equal) define the higher bound of the range to be streamed. Only entries where the key is less than (or equal to) this option will be included in the range. When reverse=true the order will be reversed, but the entries streamed will be the same.

  • reverse (boolean, default: false): stream entries in reverse order. Beware that due to the way that stores like LevelDB work, a reverse seek can be slower than a forward seek.

  • limit (number, default: -1): limit the number of entries collected by this stream. This number represents a maximum number of entries and may not be reached if you get to the end of the range first. A value of -1 means there is no limit. When reverse=true the entries with the highest keys will be returned instead of the lowest keys.

  • keys (boolean, default: true): whether the results should contain keys. If set to true and values set to false then results will simply be keys, rather than objects with a key property. Used internally by the createKeyStream() method.

  • values (boolean, default: true): whether the results should contain values. If set to true and keys set to false then results will simply be values, rather than objects with a value property. Used internally by the createValueStream() method.

db.createKeyStream([options])

Returns a Readable Stream of keys rather than key-value pairs. Use the same options as described for createReadStream to control the range and direction.

You can also obtain this stream by passing an options object to createReadStream() with keys set to true and values set to false. The result is equivalent; both streams operate in object mode.

db.createKeyStream()
  .on('data', function (data) {
    console.log('key=', data)
  })

// same as:
db.createReadStream({ keys: true, values: false })
  .on('data', function (data) {
    console.log('key=', data)
  })

db.createValueStream([options])

Returns a Readable Stream of values rather than key-value pairs. Use the same options as described for createReadStream to control the range and direction.

You can also obtain this stream by passing an options object to createReadStream() with values set to true and keys set to false. The result is equivalent; both streams operate in object mode.

db.createValueStream()
  .on('data', function (data) {
    console.log('value=', data)
  })

// same as:
db.createReadStream({ keys: false, values: true })
  .on('data', function (data) {
    console.log('value=', data)
  })

Promise Support

level-rocksdb ships with native Promise support out of the box.

Each function taking a callback also can be used as a promise, if the callback is omitted. This applies for:

  • db.get(key[, options])
  • db.put(key, value[, options])
  • db.del(key[, options])
  • db.batch(ops[, options])
  • db.batch().write()

The only exception is the constructor, which if no callback is passed will lazily open the underlying store in the background.

Example:

const db = level('./my-db')

db.put('foo', 'bar')
  .then(function () { return db.get('foo') })
  .then(function (value) { console.log(value) })
  .catch(function (err) { console.error(err) })

Or using async/await:

const main = async () => {
  const db = level('./my-db')

  await db.put('foo', 'bar')
  console.log(await db.get('foo'))
}

Events

levelup is an EventEmitter and emits the following events.

Event Description Arguments
put Key has been updated key, value (any)
del Key has been deleted key (any)
batch Batch has executed operations (array)
opening Underlying store is opening -
open Store has opened -
ready Alias of open -
closing Store is closing -
closed Store has closed. -

For example you can do:

db.on('put', function (key, value) {
  console.log('inserted', { key, value })
})

Contributing

Level/level-rocksdb is an OPEN Open Source Project. This means that:

Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.

See the Contribution Guide for more details.

Donate

Support us with a monthly donation on Open Collective and help us continue our work.

License

MIT

More Repositories

1

levelup

A wrapper for abstract-leveldown compliant stores, for Node.js and browsers.
JavaScript
4,079
star
2

level

Universal abstract-level database for Node.js and browsers.
JavaScript
1,544
star
3

leveldown

Pure C++ Node.js LevelDB binding. An abstract-leveldown compliant store.
C++
773
star
4

level-js

An abstract-leveldown compliant store on top of IndexedDB.
JavaScript
542
star
5

awesome

An open list of awesome Level modules and resources.
JavaScript
287
star
6

memdown

In-memory abstract-leveldown store for Node.js and browsers.
JavaScript
285
star
7

rocksdb

Pure C++ Node.js RocksDB binding. An abstract-leveldown compliant store.
C++
229
star
8

abstract-leveldown

An abstract prototype matching the leveldown API.
JavaScript
146
star
9

party

Open a leveldb handle multiple times.
JavaScript
146
star
10

abstract-level

Abstract class for a lexicographically sorted key-value database.
JavaScript
120
star
11

subleveldown

Split a levelup database into sublevels with their own keyspace, encoding and events.
JavaScript
119
star
12

electron-demo

Demo app loading LevelDB into an Electron context.
JavaScript
79
star
13

level-ttl

Add a 'ttl' (time-to-live) option to levelup for put() and batch().
JavaScript
69
star
14

level-browserify

No longer maintained: superseded by level v5.0.0.
JavaScript
63
star
15

classic-level

An abstract-level database backed by LevelDB.
JavaScript
58
star
16

multileveldown

multilevel implemented using leveldowns with reconnect support.
JavaScript
58
star
17

browser-level

An abstract-level database for browsers, backed by IndexedDB.
JavaScript
45
star
18

leveljs.org

Source code of leveljs.org
Pug
43
star
19

community

Discussion, support and common information for projects in the community.
42
star
20

encoding-down

An abstract-leveldown implementation that wraps another store to encode keys and values.
JavaScript
41
star
21

level-hyper

A convenience package bundling levelup and leveldown-hyper.
JavaScript
30
star
22

leveldown-mobile

This project has been abandoned.
C++
27
star
23

codec

Encode keys, values and range options, with built-in or custom encodings.
JavaScript
27
star
24

memory-level

In-memory abstract-level database for Node.js and browsers.
JavaScript
25
star
25

mem

A convenience package bundling levelup and memdown.
JavaScript
24
star
26

level-ws

A basic writable stream for abstract-level databases.
JavaScript
22
star
27

packager

A levelup package helper for distributing with an abstract-leveldown compatible back-end.
JavaScript
20
star
28

level-test

Inject temporary and isolated level stores (leveldown, level-js, memdown or custom) into your tests.
JavaScript
20
star
29

deferred-leveldown

An abstract-leveldown implementation that queues operations while a real abstract-leveldown instance is being opened.
JavaScript
19
star
30

many-level

Share an abstract-level database over the network or other kind of stream.
JavaScript
17
star
31

leveldown-hyper

Pure C++ Node.js HyperLevelDB binding. An abstract-leveldown compliant store.
C++
15
star
32

errors

Error types for levelup.
JavaScript
13
star
33

web-stream

Read from an abstract-level database using Web Streams.
JavaScript
12
star
34

iterator-stream

Turn an abstract-leveldown iterator into a readable stream.
JavaScript
12
star
35

level-lmdb

This project has been abandoned.
JavaScript
11
star
36

bench

Benchmark abstract-level databases.
JavaScript
8
star
37

level-mobile

[Currently not actively maintained] Fast & simple storage - a Node.js-style LevelDB wrapper (a convenience package bundling levelup & leveldown-mobile) [requires JXcore 0.3.0.3+]
JavaScript
8
star
38

read-stream

Read from an abstract-level database using Node.js streams.
JavaScript
6
star
39

rave-level

Use a LevelDB database from multiple processes with seamless failover.
JavaScript
6
star
40

level-basho

This project has been abandoned.
JavaScript
5
star
41

webpack-starter

Demonstrates bundling level for browsers using webpack.
JavaScript
5
star
42

concat-iterator

Concatenate entries from an iterator into an array.
JavaScript
5
star
43

compose

Compose a database factory from abstract-leveldown and levelup layers.
JavaScript
4
star
44

transcoder

Encode data with built-in or custom encodings.
JavaScript
4
star
45

supports

Create a manifest describing the abilities of an abstract-level database.
JavaScript
4
star
46

level-fstream

This project has been abandoned.
JavaScript
3
star
47

lazy-open

This project has been abandoned.
JavaScript
3
star
48

typings

Experimental typing development for level repositories. Deprecated.
TypeScript
3
star
49

database

2
star
50

browserify-starter

Demonstrates bundling level for browsers using browserify.
JavaScript
2
star
51

.github

Default community health files.
1
star