New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

redis-json

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis-json

A wrapper library to store JSON Objects in redis-hashsets and retrieve it back as JSON objects

  • 4.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.7K
decreased by-20.62%
Maintainers
1
Weekly downloads
 
Created
Source

redis-json npm version Build Status Coverage Status Maintainability

Nodejs library to store/retreive JSON Objects in RedisDB

Description

Every time set is called JSON object is flattened(embeded objects are converted to path keys) and then stored in Redis(just like a normal hashset), on get the hashset is unflattened and converted back to the original JSON object.

Now we use our own custom flattening and unflattening logic to accomodate more possibilites and eliminate certain bugs and also to improve efficiency.

We now support typescript since 3.1.0 🎉🎉🎊
Please see the below updated example.

What's new in v4.0.0?

Following up with #3 & #8 we decided to support types, which means, you get back exactly (===) same object when restored. But as a drawback, each set operation would cost 2 hashsets in redis (one for data and the other for type infomation).

Not just that, we also support custom stringifying and parsing logic for Custom Class (For Ex: Date, Person etc).
Examples for the same is given below.

Installation

npm install redis-json --save

Usage

import Redis from 'ioredis';
import JSONCache from 'redis-json';

const redis = new Redis() as any;

const jsonCache = new JSONCache<{
  name: string;
  age: 25;
  address: {
    doorNo: string;
    locality: string;
    pincode: number;
  }
}>(redis, {prefix: 'cache:'});

const user = {
  name: 'redis-json',
  age: 25,
  address: {
    doorNo: '12B',
    locality: 'pentagon',
    pincode: 123456
  },
  cars: ['BMW 520i', 'Audo A8']
}

await jsonCache.set('123', user)

const response = await jsonCache.get('123')
console.log(response)
// output
// {
//   name: 'redis-json',
//   age: 25,
//   address: {
//     doorNo: '12B',
//     locality: 'pentagon',
//     pincode: 123456
//   },
//   cars: ['BMW 520i', 'Audo A8']
// }

const response = await jsonCache.get('123', 'name', 'age');
// output
// {
//   name: 'redis-json',
//   age: 25,
// }


With custom stringifier and parser:

const jsonCache = new JSONCache(redis, {
  stringifier: {
    Date: (val: Date) => val.toISOString()
  },
  parser: {
    Date: (str: string) => new Date(str)
  }
})

const date = new Date()
await jsonCache.set('test', {
  date: date
})

// Redis hashset
> hgetall jc:test /// data
1) "date"
2) "2020-05-17T14:41:45.861Z"
> hgetall jc:test_t /// type info
1) "date"
2) "Date"


const result = await jsonCache.get('test')
result.date == date /// true

API

Please visit this page for detailed API documentation.

Constructor

new JSONCache(redisClient, options)

ParamDescription
redisClientRedisClient instance(Preferred ioredis - cient). It support any redisClient instance that has keys, multi, set, get & del methods implemented
options.prefixPrefix for redis keys. Defaults to jc: (jsonCache)

Methods

set(key: string, jsobObject: T, options): Promise<any>

ParamDescription
keyThe redis key that the JSON object has to be stored against
jsonObjectJSON obejct that needs to be stored
options.expireMax time-to-live before key expiry

If the key already exists, and is of type hashset, then the field in JSON object will get updated along with the existing data

get(key, ...fields): Promise<T | undefined>

ParamDescription
keyThe redis key in which the JSON object was stored
fields(optional) [New in v2.4.0]List of field to be retrieved from the given key. This can be used if the stored object is large and hence helps to reduce Network latency

resave rewrite(key, jsonObj): Promise<any>

ParamDescription
keyThe redis key that whose value needs to be replaced with the new one
jsonObjectJSON obejct that needs to be stored

Even if key is not of type hashset, resave rewrite will delete it and update the JSON object in the provided key

clearAll(): Promise<any>>

Clears/removes all the keys with the prefix from redis using multi command.
Useful when trying to refresh the entire cache.

Caveat

All the values will be parsed to String before saving the same in redis and hence during retreival, we cannot identify the type of data which was saved earlier and hence number will be stringified, whereas the rest of the property types will remain intact(due to internal reverse parsing).

Below is the table of original type vs retreived type of the object properties

Original TypeRetreived type
stringstring
numberstring
booleanboolean
undefinedundefined
nullnull
objectobject
arrayarray

Hence make sure to parse the number fields back to number by using a '+' in front of the property like => +obj.numField to ensure the numbers as treated as numbers 😜

Changelogs

For detailed ChangeLogs please refer this

Mocha & Chai (Testing)

npm test

Coverage Report

npm run coverage

Contributions

This is open-source, which makes it obvious for any PRs, but I would request you to add necessary test-cases for the same

LICENCE

MIT License

Keywords

FAQs

Package last updated on 17 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc