brave/level

Name: level

Owner: Brave Software

Description: Fast & simple storage - a Node.js-style LevelDB wrapper

Forked from: Level/level

Created: 2017-10-25 18:11:16.0

Updated: 2017-10-25 18:11:18.0

Pushed: 2017-10-17 16:49:24.0

Homepage:

Size: 47

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

level

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

level badge npm Node version Build Status dependencies JavaScript Style Guide npm

A convenience package that:

Use this package to avoid having to explicitly install leveldown when you just want plain old LevelDB from levelup.

level = require('level')

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

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

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

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


API

See levelup and leveldown for more details.

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

The main entry point for creating a new levelup instance.

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:

l(location, options, function (err, db) {
 (err) throw err

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


Versus the equivalent:

ill throw if an error occurs
t db = level(location, options)

et('foo', function (err, value) {
 (err) return console.log('foo does not exist')
nsole.log('got foo =', value)

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.

et('foo', function (err, value) {
 (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.

el('foo', function (err) {
 (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'.

t 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' }


atch(ops, function (err) {
 (err) return console.log('Ooops!', err)
nsole.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.

atch()
el('father')
ut('name', 'Yuri Irsenovich Kim')
ut('dob', '16 February 1941')
ut('spouse', 'Kim Young-sook')
ut('occupation', 'Clown')
rite(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:

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.

reateReadStream()
n('data', function (data) {
console.log(data.key, '=', data.value)

n('error', function (err) {
console.log('Oh my!', err)

n('close', function () {
console.log('Stream closed')

n('end', function () {
console.log('Stream ended')

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

Legacy options:

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.

reateKeyStream()
n('data', function (data) {
console.log('key=', data)


ame as:
reateReadStream({ keys: true, values: false })
n('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.

reateValueStream()
n('data', function (data) {
console.log('value=', data)


ame as:
reateReadStream({ keys: false, values: true })
n('data', function (data) {
console.log('value=', data)

Promise Support

level 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:

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

Example:

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

ut('foo', 'bar')
hen(function () { return db.get('foo') })
hen(function (value) { console.log(value) })
atch(function (err) { console.error(err) })

Or using async/await:

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

ait db.put('foo', 'bar')
nsole.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:

n('put', function (key, value) {
nsole.log('inserted', { key, value })

Contributing

level 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 CONTRIBUTING.md file for more details.

License & Copyright

Copyright (c) 2012-2017 level contributors.

level is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.