パッケージの詳細

subleveldown

Level491.4kMIT非推奨6.0.1

Superseded by abstract-level (https://github.com/Level/community#faq)

Split a levelup database into sublevels with their own keyspace, encoding and events

level

readme

subleveldown

Split a levelup database into sublevels with their own keyspace, encoding and events.

level badge npm Node version Test Coverage Standard Common Changelog Donate

Table of Contents

<summary>Click to expand</summary> - Usage - Background - API - subdb = sub(db[, prefix][, options]) - Install - Contributing - Donate - License

Usage

If you are upgrading: please see UPGRADING.md.

const sub = require('subleveldown')
const level = require('level')

const db = level('db')
const example = sub(db, 'example')
const nested = sub(example, 'nested')

The example and nested db's are just regular levelup instances:

example.put('hello', 'world', function () {
  nested.put('hi', 'welt', function () {
    // Prints { key: 'hi', value: 'welt' }
    nested.createReadStream().on('data', console.log)
  })
})

Or with promises and iterators:

await example.put('hello', 'world')
await nested.put('hi', 'welt')

for await (const [key, value] of nested.iterator()) {
  // Prints ['hi', 'welt']
  console.log([key, value])
}

Sublevels see their own keys as well as keys of any nested sublevels:

// Prints:
// { key: '!nested!hi', value: 'welt' }
// { key: 'hello', value: 'world' }
example.createReadStream().on('data', console.log)

They also support db.clear() which is very useful to empty a bucket of stuff:

example.clear(function (err) {})

// Or delete a range within `example`
example.clear({ gt: 'hello' }, function (err) {})

// With promises
await example.clear()

Background

subleveldown separates a levelup database into sections - or sublevels from here on out. Think SQL tables, but evented, ranged and realtime!

Each sublevel is a levelup of its own. This means it has the exact same interface as its parent database, but its own keyspace and events. In addition, sublevels are individually wrapped with encoding-down, giving us per-sublevel encodings. For example, it's possible to have one sublevel with Buffer keys and another with 'utf8' encoded keys. The same goes for values. Like so:

sub(db, 'one', { valueEncoding: 'json' })
sub(db, 'two', { keyEncoding: 'binary' })

There is one limitation, however: keys must encode to either strings or Buffers. This is not likely to affect you, unless you use custom encodings or the id encoding (which bypasses encodings and thus makes it your responsibility to ensure keys are either strings or Buffers). If in that case you do pass in a key that is not a string or Buffer, it will be irreversibly converted to a string.

Authored by @mafintosh and inspired by level-sublevel by @dominictarr, subleveldown has become an official part of Level. As level-sublevel is no longer under active development, we recommend switching to subleveldown to get the latest and greatest of the Level ecosystem. These two modules largely offer the same functionality, except for hooks and per-batch prefixes.

API

subdb = sub(db[, prefix][, options])

Returns a levelup instance that uses subleveldown to prefix the keys of the underlying store of db. The required db parameter must be a levelup instance. Any layers that this instance may have (like encoding-down or subleveldown itself) are peeled off to get to the innermost abstract-leveldown compliant store (like leveldown). This ensures there is no double encoding step.

The prefix must be a string. If omitted, the effective prefix is two separators, e.g. '!!'. If db is already a subleveldown-powered instance, the effective prefix is a combined prefix, e.g. '!one!!two!'.

The optional options parameter has the following subleveldown specific properties:

  • separator (string, default: '!') Character for separating sublevel prefixes from user keys and each other. Must sort before characters used in prefixes. An error will be thrown if that's not the case.
  • open (function) Optional open hook called when the underlying levelup instance has been opened. The hook receives a callback which must be called to finish opening.

Any other options are passed along to the underlying levelup and encoding-down constructors. See their documentation for further details.

Install

With npm do:

npm i subleveldown -S

Contributing

Level/subleveldown 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

更新履歴

Changelog

6.0.1 - 2021-10-02

Fixed

  • Bump levelup to fix API parity with abstract-leveldown (bd73ace) (Vincent Weevers).

6.0.0 - 2021-10-01

If you are upgrading: please see UPGRADING.md.

Changed

  • Breaking: bump abstract-leveldown, encoding-down, levelup (60bd660) (Vincent Weevers)
  • Breaking: hide private properties with Symbol (1f9d550) (Vincent Weevers)
  • Refactor: use addRestOptions() for iterator (717066e) (Vincent Weevers)
  • Modernize syntax and bump standard (0bd41cc) (Vincent Weevers)

Added

Removed

  • Breaking: drop Node.js 6 and 8 (ab713d1) (Vincent Weevers).

5.0.1 - 2020-06-26

Added

Fixed

5.0.0 - 2020-04-05

If you are upgrading: please see UPGRADING.md.

Changed

  • Breaking: parent db must support deferredOpen (#89) (@vweevers)
  • Upgrade dependency-check devDependency from ^3.3.0 to ^4.1.0 (b47f991) (@vweevers)

Fixed

  • Breaking: fix iterating buffer keys that contain bytes 196-255 (#88) (@vweevers)

4.1.4 - 2019-10-08

Fixed

4.1.3 - 2019-09-17

Changed

4.1.2 - 2019-09-12

Changed

  • Upgrade standard devDependency from ^13.0.1 to ^14.0.0 (#71) (@vweevers)
  • Upgrade hallmark devDependency from ^1.0.0 to ^2.0.0 (#72) (@vweevers)

Removed

Fixed

  • Polish clear() (#74) (@vweevers)
    • Bump levelup, abstract-leveldown and encoding-down to prevent dedupe
    • Opt-in to new clear() tests
    • Prefer optimized implementation of clear()

4.1.1 - 2019-08-18

Changed

  • Upgrade memdown devDependency from ^4.0.0 to ^5.0.0 (#70) (@vweevers)
  • Upgrade hallmark devDependency from ^0.1.0 to ^1.0.0 (#68) (@vweevers)
  • Upgrade standard devDependency from ^12.0.1 to ^13.0.1 (#67) (@vweevers)

Fixed

4.1.0 - 2019-06-28

Changed

  • Upgrade nyc devDependency from ^13.3.0 to ^14.0.0 (#63) (@vweevers)

Added

4.0.0 - 2019-04-06

If you are upgrading: please see UPGRADING.md.

Changed

  • Upgrade abstract-leveldown from ^5.0.0 to ^6.0.2 (#61) (@vweevers)
  • Upgrade encoding-down from ^5.0.3 to ^6.0.1 (#61) (@vweevers)
  • Upgrade levelup from ^3.0.1 to ^4.0.1 (#61) (@vweevers)
  • Avoid unnecessary copy of batch operations (#61) (@vweevers)
  • Invoke abstract tests from single function (#61) (@vweevers)
  • Add mandatory db argument to abstract iterator (#61) (@vweevers)
  • Upgrade memdown devDependency from ^3.0.0 to ^4.0.0 (#61) (@vweevers)
  • Upgrade nyc devDependency from ^12.0.2 to ^13.3.0 (#61) (@vweevers)
  • Upgrade standard devDependency from ^11.0.1 to ^12.0.1 (#61) (@vweevers)
  • Apply common project tweaks (#58, #59) (@vweevers)

Added

  • Test that errors from open() and iterators bubble up (#61) (@vweevers)
  • Test without a user-provided levelup layer (#61) (@vweevers)
  • Gitignore coverage directory (#61) (@vweevers)

Removed

  • Remove obsolete _batch() checks (#61) (@vweevers)
  • Remove dummy location from abstract-leveldown constructor call (#61) (@vweevers)

Fixed

3.0.1 - 2018-07-27

Added

Removed

Fixed

  • Pass on fillCache option to SubIterator (@Nocory)

3.0.0 - 2018-06-07

If you are upgrading: please see UPGRADING.md.

Added

Changed

Removed

3.0.0-rc1 - 2018-06-03

Changed

Added

Removed

Fixed

2.1.0 - 2015-10-30

Changed

  • Use standard for linting (@mafintosh)
  • Upgrade abstract-leveldown from ^2.1.0 to ^2.4.1 (@mafintosh)
  • Upgrade levelup from ^0.19.0 to ^1.2.1 (@mafintosh)
  • Upgrade memdown devDependency from ^1.0.0 to ^1.1.1 (@mafintosh)
  • Upgrade tape devDependency from ^3.0.3 to ^4.2.2 (@mafintosh)
  • Add opts parameter to SubDown for custom open logic (@mafintosh)
  • Add opts.open open hook (@mafintosh)

2.0.0 - 2015-02-04

Changed

  • Do not strip separator when nested for more transparent usage (@mafintosh)

1.1.0 - 2015-01-28

Changed

1.0.6 - 2015-01-06

Fixed

1.0.5 - 2015-01-06

Fixed

1.0.4 - 2014-12-31

Changed

  • Do not require prefix and forward separator (@mafintosh)

1.0.3 - 2014-12-31

Fixed

1.0.2 - 2014-12-31

Changed

Added

1.0.1 - 2014-12-29

Fixed

1.0.0 - 2014-12-23

:seedling: Initial release.