npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis-json-forked

v3.1.0

Published

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

Downloads

2

Readme

redis-json Build Status Coverage Status

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.
~~Under the hood it uses flat library for flattening and unflattening JSON objects~~ 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.

Installation

npm i redis-json -D

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,
// }

API

Constructor

new JSONCache(redisClient, options)

| Param | Description | |:------|:------------| | redisClient | RedisClient instance(Preferred ioredis - cient). It support any redisClient instance that has keys, multi, set, get & del methods implemented | | options.prefix | Prefix for redis keys. Defaults to jc: (jsonCache) |

Methods

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

| Param | Description | |:------|:------------| | key | The redis key that the JSON object has to be stored against | | jsonObject | JSON obejct that needs to be stored | | options.expire | Max 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>

| Param | Description | |:------|:------------| | key |The 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>

| Param | Description | |:------|:------------| |key | The redis key that whose value needs to be replaced with the new one | |jsonObject | JSON 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 Type | Retreived type | |:--------------|:---------------| | string | string | | number | string | | boolean | boolean | | undefined | undefined | | null | null | | object | object | | array | array |

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