Git Product home page Git Product logo

encoding-down's Introduction

encoding-down

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

level badge npm Node version Travis Coverage Status JavaScript Style Guide npm Backers on Open Collective Sponsors on Open Collective

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.

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

var 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!

var 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?

var 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?

var 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?

var 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'
  })
})
var 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:

var level = require('level')
var 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:

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

async function main () {
  var 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:

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

var 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

To sustain Level and its activities, become a backer or sponsor on Open Collective. Your logo or avatar will be displayed on our 28+ GitHub repositories and npm packages. ๐Ÿ’–

Backers

Open Collective backers

Sponsors

Open Collective sponsors

License

MIT ยฉ 2012-present Contributors.

encoding-down's People

Contributors

greenkeeper[bot] avatar huan avatar juliangruber avatar meirionhughes avatar ralphtheninja avatar vweevers avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.