パッケージの詳細

encoding-down

Level3.3mMIT非推奨7.1.0

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

An abstract-leveldown implementation that wraps another store to encode keys and values

level

readme

encoding-down

An abstract-leveldown implementation that wraps another store to encode keys and values.

level badge npm Node version Test Coverage Standard Common Changelog Donate

Introduction

Stores like leveldown can only store strings and Buffers. Other types, though accepted, are serialized before storage, which is an irreversible type conversion. For a richer set of data types you can wrap such a store with encoding-down. It allows you to specify an encoding to use for keys and values independently. This not only widens the range of input types, but also limits the range of output types. The encoding is applied to all read and write operations: it encodes writes and decodes reads.

Many encodings are builtin courtesy of level-codec. The default encoding is utf8 which ensures you'll always get back a string. You can also provide a custom encoding like bytewise - or your own!

Usage

Without any options, encoding-down defaults to the utf8 encoding.

const levelup = require('levelup')
const leveldown = require('leveldown')
const encode = require('encoding-down')

const db = levelup(encode(leveldown('./db1')))

db.put('example', Buffer.from('encoding-down'), function (err) {
  db.get('example', function (err, value) {
    console.log(typeof value, value) // 'string encoding-down'
  })
})

Can we store objects? Yes!

const db = levelup(encode(leveldown('./db2'), { valueEncoding: 'json' }))

db.put('example', { awesome: true }, function (err) {
  db.get('example', function (err, value) {
    console.log(value) // { awesome: true }
    console.log(typeof value) // 'object'
  })
})

How about storing Buffers, but getting back a hex-encoded string?

const db = levelup(encode(leveldown('./db3'), { valueEncoding: 'hex' }))

db.put('example', Buffer.from([0, 255]), function (err) {
  db.get('example', function (err, value) {
    console.log(typeof value, value) // 'string 00ff'
  })
})

What if we previously stored binary data?

const db = levelup(encode(leveldown('./db4'), { valueEncoding: 'binary' }))

db.put('example', Buffer.from([0, 255]), function (err) {
  db.get('example', function (err, value) {
    console.log(typeof value, value) // 'object <Buffer 00 ff>'
  })

  // Override the encoding for this operation
  db.get('example', { valueEncoding: 'base64' }, function (err, value) {
    console.log(typeof value, value) // 'string AP8='
  })
})

And what about keys?

const db = levelup(encode(leveldown('./db5'), { keyEncoding: 'json' }))

db.put({ awesome: true }, 'example', function (err) {
  db.get({ awesome: true }, function (err, value) {
    console.log(value) // 'example'
  })
})
const db = levelup(encode(leveldown('./db6'), { keyEncoding: 'binary' }))

db.put(Buffer.from([0, 255]), 'example', function (err) {
  db.get('00ff', { keyEncoding: 'hex' }, function (err, value) {
    console.log(value) // 'example'
  })
})

Usage with level

The level module conveniently bundles encoding-down and passes its options to encoding-down. This means you can simply do:

const level = require('level')
const db = level('./db7', { valueEncoding: 'json' })

db.put('example', 42, function (err) {
  db.get('example', function (err, value) {
    console.log(value) // 42
    console.log(typeof value) // 'number'
  })
})

API

db = require('encoding-down')(db[, options])

  • db must be an abstract-leveldown compliant store
  • options are passed to level-codec:
    • keyEncoding: encoding to use for keys
    • valueEncoding: encoding to use for values

Both encodings default to 'utf8'. They can be a string (builtin level-codec encoding) or an object (custom encoding).

Custom encodings

Please refer to level-codec documentation for a precise description of the format. Here's a quick example with level and async/await just for fun:

const level = require('level')
const lexint = require('lexicographic-integer')

async function main () {
  const db = level('./db8', {
    keyEncoding: {
      type: 'lexicographic-integer',
      encode: (n) => lexint.pack(n, 'hex'),
      decode: lexint.unpack,
      buffer: false
    }
  })

  await db.put(2, 'example')
  await db.put(10, 'example')

  // Without our encoding, the keys would sort as 10, 2.
  db.createKeyStream().on('data', console.log) // 2, 10
}

main()

With an npm-installed encoding (modularity ftw!) we can reduce the above to:

const level = require('level')
const lexint = require('lexicographic-integer-encoding')('hex')

const db = level('./db8', {
  keyEncoding: lexint
})

Contributing

Level/encoding-down 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

7.1.0 - 2021-09-30

Added

  • Add db.getMany(keys) (#102) (4038a30) (Vincent Weevers).

7.0.0 - 2021-04-09

If you are upgrading: please see UPGRADING.md.

Changed

Added

Removed

6.3.0 - 2019-10-13

Added

6.2.0 - 2019-09-06

Changed

  • Upgrade hallmark devDependency from ^0.1.0 to ^2.0.0 (#85, #91) (@vweevers)
  • Upgrade standard devDependency from ^12.0.0 to ^14.0.0 (#84, #90) (@vweevers)
  • Upgrade memdown devDependency from ^4.0.0 to ^5.0.0 (#88) (@vweevers)

Added

6.1.0 - 2019-06-22

Changed

  • Upgrade nyc devDependency from ^13.2.0 to ^14.0.0 (#81)) (@vweevers)

Added

6.0.2 - 2019-03-31

Changed

6.0.1 - 2018-12-27

Changed

  • Replace remark-cli devDependency with hallmark (#76) (@vweevers)

Added

Fixed

  • Fix approximateSize() to encode start and end arguments (#75) (@vweevers)

6.0.0 - 2018-12-25

If you are upgrading: please see UPGRADING.md.

Changed

Added

Removed

5.0.4 - 2018-06-22

Added

5.0.3 - 2018-05-30

Changed

5.0.2 - 2018-05-23

Added

Changed

5.0.1 - 2018-05-19

Changed

5.0.0 - 2018-05-13

If you are upgrading: please see UPGRADING.md.

Added

Changed

Removed

4.0.1 - 2018-05-19

Changed

4.0.0 - 2018-02-12

If you are upgrading: please see UPGRADING.md.

Added

Changed

Removed

3.0.1 - 2017-12-18

Added

  • Test that default utf8 encoding stringifies numbers (@vweevers)

Fixed

  • Skip decoding if options.keys or options.values is false (@vweevers)

3.0.0 - 2017-11-11

If you are upgrading: please see UPGRADING.md.

Added

Changed

Removed

2.3.4 - 2017-10-24

Added

  • README: add example of npm installed encoding (@vweevers)

2.3.3 - 2017-10-22

Changed

2.3.2 - 2017-10-22

Changed

Fixed

  • Fix problems related to missing asBuffer, keyAsBuffer and valueAsBuffer (@ralphtheninja)

2.3.1 - 2017-10-02

Changed

2.3.0 - 2017-09-24

Added

2.2.1 - 2017-09-13

Fixed

2.2.0 - 2017-09-12

Added

Changed

2.1.5 - 2017-08-18

Added

Changed

2.1.4 - 2017-01-26

Fixed

  • Rename methods to _serializeKey() and _serializeValue() (@juliangruber)

2.1.3 - 2017-01-26

Added

  • Add _encodeKey() and _encodeValue() id functions (@juliangruber)

2.1.2 - 2017-01-26

Fixed

2.1.1 - 2017-01-26

Fixed

2.1.0 - 2017-01-26

Added

2.0.8 - 2017-01-26

Removed

Fixed

2.0.7 - 2017-01-26

Added

2.0.6 - 2017-01-26

Fixed

2.0.5 - 2017-01-26

Fixed

  • Fix bug in iterator._next() with undefined key or value (@juliangruber)

2.0.4 - 2017-01-26

Changed

2.0.3 - 2017-01-26

Fixed

2.0.2 - 2017-01-26

Fixed

  • Fix bug with incorrect db and missing new operator (@juliangruber)

2.0.1 - 2017-01-26

Fixed

2.0.0 - 2017-01-26

Changed

1.0.0 - 2017-01-26

:seedling: Initial release.