tinycbor-redis
v0.0.0
Published
An expansion of node-tinycbor which provides access to select Redis commands with internal CBOR encoding/decoding
Downloads
1
Readme
Node.js Addon for TinyCBOR with Redis Support
tinycbor-redis
is a NodeJS addon that combines use of CPP_Redis with the C based tinyCBOR library. With this addon, you can directly store and retrieve data from a Redis server connection with under-the-hood CBOR serialization and deserialization of passed in data.
Performance
Benchmarks are curerntly in progress and will be included as soon as completed.
Usage
This module provides the following methods:
flushDb()
, no arguments, clears redis server currently connected todisconnect()
, no arguments, terminates connection to current redis serverhset()
, requires a string:key, Array[string]:field, any:value, writes data to serverhget()
, requires a string:key, Array[string]:field, retrieves value mapped tofield
hgetall()
, requires a string:key, retrieves all field-value pairs stored tokey
hdel()
, requires a string:key, Array[string]:field, removesfield
and value stored tofield
from serverhscan()
, requires a string:key, Array[string]:field, retrieves all field-value pairs corresponding tofield
del()
, requires a string:key, removes all data stored tokey
from serverdeleteAll()
, requires a string:key, Array[string]:field, removes all subfields offield
,field
includedparsePath()
, requires a concatenated CBOR encoded stream (e.g. x61ax61bx62c), returns array of parsed keys (e.g. ['a', 'b', 'c']), useful for unflattening an objectencode()
, which consumes a JavaScript object and returns anArrayBuffer
containing the serialized datadecode()
, which consumes a nodeArrayBuffer
object and returns a JavaScript object identical to the originaltoJson()
, which consumes a nodeArrayBuffer
object and writes the JSON representation of said object into aCBORtoJSON.json
file in the top directorytoText()
, which consumes a nodeArrayBuffer
object and writes the text representation of said object into aCBORtoTEXT.txt
file in the top directory
For toJson()
and toText()
, if the file is not found, a new one will be generated. Otherwise, the CBOR conversion is appended to the existing file.
All data types are supported for value
, including null
and undefined
. Keys must be of type string
and fields of type Array[string]
.
The below code snippet writes and then retrieves a value from the redis-server, asserting that the value is correct.
const assert = require('assert');
const createClient = require('tinycbor-redis'); //used to obtain the C++ addon constructor
const CBOR = createClient() //call the retrieved function to instantiate a cbor instance
let key = "key"
let path = ['a', 'b', 'c']
let value = true
/*
* `path` and `value` are both encoded to CBOR format internally
* the encoded results are written to the redis-server
*/
CBOR.hset(key, path, value)
/*
* the result of the `hget` operation is decoded from CBOR and converted to the JavaScript equivalent
* it is then returned to the calling program
*/
let output = CBOR.hget(key, path)
assert.deepEqual(output, value);
Installation
To use tinycbor-redis, use:
yarn add tinycbor-redis
Otherwise, to manually install, enter the directory and use:
yarn install