tinycbor-redis

0.0.0 • Public • Published

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 to
  • disconnect(), no arguments, terminates connection to current redis server
  • hset(), requires a string:key, Array[string]:field, any:value, writes data to server
  • hget(), requires a string:key, Array[string]:field, retrieves value mapped to field
  • hgetall(), requires a string:key, retrieves all field-value pairs stored to key
  • hdel(), requires a string:key, Array[string]:field, removes field and value stored to field from server
  • hscan(), requires a string:key, Array[string]:field, retrieves all field-value pairs corresponding to field
  • del(), requires a string:key, removes all data stored to key from server
  • deleteAll(), requires a string:key, Array[string]:field, removes all subfields of field, field included
  • parsePath(), requires a concatenated CBOR encoded stream (e.g. x61ax61bx62c), returns array of parsed keys (e.g. ['a', 'b', 'c']), useful for unflattening an object
  • encode(), which consumes a JavaScript object and returns an ArrayBuffer containing the serialized data
  • decode(), which consumes a node ArrayBuffer object and returns a JavaScript object identical to the original
  • toJson(), which consumes a node ArrayBuffer object and writes the JSON representation of said object into a CBORtoJSON.json file in the top directory
  • toText(), which consumes a node ArrayBuffer object and writes the text representation of said object into a CBORtoTEXT.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

Readme

Keywords

Package Sidebar

Install

npm i tinycbor-redis

Weekly Downloads

0

Version

0.0.0

License

ISC

Last publish

Collaborators

  • samuelvpatterson