What is encoding-down?
The encoding-down npm package is a codec layer for levelup that provides encoding and decoding of keys and values. It allows you to use different encodings for keys and values, making it easier to work with various data types in a LevelDB store.
What are encoding-down's main functionalities?
Basic Usage
This code demonstrates how to set up a LevelDB store with JSON encoding for values using encoding-down. It shows how to put and get a JSON object.
const levelup = require('levelup');
const leveldown = require('leveldown');
const encode = require('encoding-down');
const db = levelup(encode(leveldown('./mydb'), { valueEncoding: 'json' }));
db.put('key', { some: 'value' }, function (err) {
if (err) return console.log('Ooops!', err);
db.get('key', function (err, value) {
if (err) return console.log('Ooops!', err);
console.log('Got value:', value);
});
});
Custom Encoding
This code demonstrates how to use a custom encoding for values in a LevelDB store. The custom encoding converts values to and from Buffers.
const levelup = require('levelup');
const leveldown = require('leveldown');
const encode = require('encoding-down');
const customEncoding = {
encode: (val) => Buffer.from(val.toString()),
decode: (val) => val.toString(),
buffer: true,
type: 'custom'
};
const db = levelup(encode(leveldown('./mydb'), { valueEncoding: customEncoding }));
db.put('key', 'custom value', function (err) {
if (err) return console.log('Ooops!', err);
db.get('key', function (err, value) {
if (err) return console.log('Ooops!', err);
console.log('Got value:', value);
});
});
Binary Encoding
This code demonstrates how to use binary encoding for values in a LevelDB store. It shows how to put and get a binary value.
const levelup = require('levelup');
const leveldown = require('leveldown');
const encode = require('encoding-down');
const db = levelup(encode(leveldown('./mydb'), { valueEncoding: 'binary' }));
db.put('key', Buffer.from('binary value'), function (err) {
if (err) return console.log('Ooops!', err);
db.get('key', function (err, value) {
if (err) return console.log('Ooops!', err);
console.log('Got value:', value.toString());
});
});
Other packages similar to encoding-down
level-codec
The level-codec package provides encoding and decoding functionality for LevelDB. It allows you to define custom encodings for keys and values, similar to encoding-down. However, encoding-down is more integrated with the level ecosystem and provides a more streamlined API for setting up encodings.
level-transcoder
The level-transcoder package offers a way to transform data as it is read from or written to a LevelDB store. It supports various encoding and decoding schemes, similar to encoding-down. However, encoding-down is more focused on providing a codec layer specifically for levelup, making it easier to use in that context.
encoding-down
abstract-leveldown
wrapper supporting levelup@1 encodings. For motivation, see this issue.
Usage
const levelup = require('levelup')
const leveldown = require('leveldown')
const encode = require('encoding-down')
const db = levelup(encode(leveldown('./db')))
db.put('name', 'encoding-down', function (err) {
db.get('name', function (err, value) {
if (!err) console.log('name=', value)
})
})
API
const db = require('encoding-down')(db[, options])
db
abstract-leveldown
compatible db such as leveldown
, memdown
, level-js
etcoptions.keyEncoding
(string) defaults to 'utf8'
options.valueEncoding
(string) defaults to 'utf8'
options
are passed to level-codec
, see supported encodings for more information.
License
MIT