redis-json
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -0,1 +1,5 @@ | ||
# v4.2.0 | ||
* adds support for transactions via `setT`, `delT` & `rewriteT` methods | ||
* `rewrite` method now support expiry. Please check the API docs for more details | ||
# v4.1.0 | ||
@@ -2,0 +6,0 @@ * adds `.del()` method |
@@ -49,5 +49,8 @@ [redis-json](../README.md) › [JSONCache](jsoncache.md) | ||
* [del](jsoncache.md#del) | ||
* [delT](jsoncache.md#delt) | ||
* [get](jsoncache.md#get) | ||
* [rewrite](jsoncache.md#rewrite) | ||
* [rewriteT](jsoncache.md#rewritet) | ||
* [set](jsoncache.md#set) | ||
* [setT](jsoncache.md#sett) | ||
@@ -60,3 +63,3 @@ ## Constructors | ||
Defined in src/lib/jsonCache.ts:55 | ||
Defined in src/lib/jsonCache.ts:57 | ||
@@ -69,3 +72,3 @@ Intializes JSONCache instance | ||
------ | ------ | ------ | ------ | | ||
`redisClient` | any | - | RedisClient instance(Preferred ioredis - cient). It support any redisClient instance that has `'hmset' | 'hmget' | 'hgetall' | 'expire' | 'del' | 'keys'` methods implemented | | ||
`redisClient` | any | - | RedisClient instance(Preferred ioredis - cient). It supports any redisClient instance that has `'hmset' | 'hmget' | 'hgetall' | 'expire' | 'del' | 'keys'` methods implemented | | ||
`options` | [IOptions](../interfaces/ioptions.md) | {} | Options for controlling the prefix | | ||
@@ -81,3 +84,3 @@ | ||
Defined in src/lib/jsonCache.ts:171 | ||
Defined in src/lib/jsonCache.ts:207 | ||
@@ -95,3 +98,3 @@ Removes/deletes all the keys in the JSON Cache, | ||
Defined in src/lib/jsonCache.ts:196 | ||
Defined in src/lib/jsonCache.ts:232 | ||
@@ -117,2 +120,29 @@ Removes the given key from Redis | ||
### delT | ||
▸ **delT**(`transaction`: Transaction, `key`: string): *Transaction* | ||
Defined in src/lib/jsonCache.ts:253 | ||
Removes the given key from Redis | ||
using the given transaction | ||
Please use this method instead of | ||
directly using `redis.del` as this method | ||
ensures that even the corresponding type info | ||
is removed. It also ensures that prefix is | ||
added to key, ensuring no other key is | ||
removed unintentionally | ||
**Parameters:** | ||
Name | Type | Description | | ||
------ | ------ | ------ | | ||
`transaction` | Transaction | Redis transaction | | ||
`key` | string | Redis key | | ||
**Returns:** *Transaction* | ||
___ | ||
### get | ||
@@ -122,3 +152,3 @@ | ||
Defined in src/lib/jsonCache.ts:116 | ||
Defined in src/lib/jsonCache.ts:140 | ||
@@ -143,5 +173,5 @@ Retrieves the hashset from redis and | ||
▸ **rewrite**(`key`: string, `obj`: T): *Promise‹any›* | ||
▸ **rewrite**(`key`: string, `obj`: T, `options?`: [ISetOptions](../interfaces/isetoptions.md)): *Promise‹any›* | ||
Defined in src/lib/jsonCache.ts:162 | ||
Defined in src/lib/jsonCache.ts:186 | ||
@@ -156,2 +186,3 @@ Replace the entire hashset for the given key | ||
`obj` | T | JSON Object of type T | | ||
`options?` | [ISetOptions](../interfaces/isetoptions.md) | - | | ||
@@ -162,2 +193,23 @@ **Returns:** *Promise‹any›* | ||
### rewriteT | ||
▸ **rewriteT**(`transaction`: Transaction, `key`: string, `obj`: T, `options?`: [ISetOptions](../interfaces/isetoptions.md)): *Transaction* | ||
Defined in src/lib/jsonCache.ts:198 | ||
Replace the entire hashset for the given key | ||
**Parameters:** | ||
Name | Type | Description | | ||
------ | ------ | ------ | | ||
`transaction` | Transaction | Redis transaction | | ||
`key` | string | Redis key | | ||
`obj` | T | JSON Object of type T | | ||
`options?` | [ISetOptions](../interfaces/isetoptions.md) | - | | ||
**Returns:** *Transaction* | ||
___ | ||
### set | ||
@@ -167,3 +219,3 @@ | ||
Defined in src/lib/jsonCache.ts:90 | ||
Defined in src/lib/jsonCache.ts:91 | ||
@@ -182,1 +234,24 @@ Flattens the given json object and | ||
**Returns:** *Promise‹any›* | ||
___ | ||
### setT | ||
▸ **setT**(`transaction`: Transaction, `key`: string, `obj`: T, `options`: [ISetOptions](../interfaces/isetoptions.md)): *Transaction* | ||
Defined in src/lib/jsonCache.ts:116 | ||
Flattens the given json object and | ||
stores it in Redis hashset using | ||
the given transaction | ||
**Parameters:** | ||
Name | Type | Default | Description | | ||
------ | ------ | ------ | ------ | | ||
`transaction` | Transaction | - | redis transaction | | ||
`key` | string | - | Redis key | | ||
`obj` | T | - | JSON object to be stored | | ||
`options` | [ISetOptions](../interfaces/isetoptions.md) | {} | | | ||
**Returns:** *Transaction* |
@@ -370,3 +370,3 @@ import { promisify } from 'util'; | ||
* @param redisClient RedisClient instance(Preferred ioredis - cient). | ||
* It support any redisClient instance that has | ||
* It supports any redisClient instance that has | ||
* `'hmset' | 'hmget' | 'hgetall' | 'expire' | 'del' | 'keys'` | ||
@@ -386,3 +386,2 @@ * methods implemented | ||
scan: promisify(redisClient.scan).bind(redisClient), | ||
multi: redisClient.multi.bind(redisClient), | ||
}; | ||
@@ -415,2 +414,22 @@ this.flattener = new Flattener(options.stringifier, options.parser); | ||
/** | ||
* Flattens the given json object and | ||
* stores it in Redis hashset using | ||
* the given transaction | ||
* | ||
* @param transaction redis transaction | ||
* @param key Redis key | ||
* @param obj JSON object to be stored | ||
* @param options | ||
*/ | ||
setT(transaction, key, obj, options = {}) { | ||
const flattened = this.flattener.flatten(obj); | ||
transaction.hmset(this.getKey(key), flattened.data); | ||
transaction.hmset(this.getTypeKey(key), flattened.typeInfo); | ||
if (options.expire) { | ||
transaction.expire(this.getKey(key), options.expire); | ||
transaction.expire(this.getTypeKey(key), options.expire); | ||
} | ||
return transaction; | ||
} | ||
/** | ||
* Retrieves the hashset from redis and | ||
@@ -470,9 +489,20 @@ * unflattens it back to the original Object | ||
*/ | ||
rewrite(key, obj) { | ||
rewrite(key, obj, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.redisClientInt.del(this.getKey(key)); | ||
yield this.set(key, obj); | ||
yield this.set(key, obj, options); | ||
}); | ||
} | ||
/** | ||
* Replace the entire hashset for the given key | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
* @param obj JSON Object of type T | ||
*/ | ||
rewriteT(transaction, key, obj, options) { | ||
transaction.del(this.getKey(key)); | ||
return this.setT(transaction, key, obj, options); | ||
} | ||
/** | ||
* Removes/deletes all the keys in the JSON Cache, | ||
@@ -513,2 +543,21 @@ * having the prefix. | ||
} | ||
/** | ||
* Removes the given key from Redis | ||
* using the given transaction | ||
* | ||
* Please use this method instead of | ||
* directly using `redis.del` as this method | ||
* ensures that even the corresponding type info | ||
* is removed. It also ensures that prefix is | ||
* added to key, ensuring no other key is | ||
* removed unintentionally | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
*/ | ||
delT(transaction, key) { | ||
transaction.del(this.getKey(key)); | ||
transaction.del(this.getTypeKey(key)); | ||
return transaction; | ||
} | ||
/****************** | ||
@@ -515,0 +564,0 @@ * PRIVATE METHODS |
@@ -372,3 +372,3 @@ 'use strict'; | ||
* @param redisClient RedisClient instance(Preferred ioredis - cient). | ||
* It support any redisClient instance that has | ||
* It supports any redisClient instance that has | ||
* `'hmset' | 'hmget' | 'hgetall' | 'expire' | 'del' | 'keys'` | ||
@@ -388,3 +388,2 @@ * methods implemented | ||
scan: util.promisify(redisClient.scan).bind(redisClient), | ||
multi: redisClient.multi.bind(redisClient), | ||
}; | ||
@@ -417,2 +416,22 @@ this.flattener = new Flattener(options.stringifier, options.parser); | ||
/** | ||
* Flattens the given json object and | ||
* stores it in Redis hashset using | ||
* the given transaction | ||
* | ||
* @param transaction redis transaction | ||
* @param key Redis key | ||
* @param obj JSON object to be stored | ||
* @param options | ||
*/ | ||
setT(transaction, key, obj, options = {}) { | ||
const flattened = this.flattener.flatten(obj); | ||
transaction.hmset(this.getKey(key), flattened.data); | ||
transaction.hmset(this.getTypeKey(key), flattened.typeInfo); | ||
if (options.expire) { | ||
transaction.expire(this.getKey(key), options.expire); | ||
transaction.expire(this.getTypeKey(key), options.expire); | ||
} | ||
return transaction; | ||
} | ||
/** | ||
* Retrieves the hashset from redis and | ||
@@ -472,9 +491,20 @@ * unflattens it back to the original Object | ||
*/ | ||
rewrite(key, obj) { | ||
rewrite(key, obj, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.redisClientInt.del(this.getKey(key)); | ||
yield this.set(key, obj); | ||
yield this.set(key, obj, options); | ||
}); | ||
} | ||
/** | ||
* Replace the entire hashset for the given key | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
* @param obj JSON Object of type T | ||
*/ | ||
rewriteT(transaction, key, obj, options) { | ||
transaction.del(this.getKey(key)); | ||
return this.setT(transaction, key, obj, options); | ||
} | ||
/** | ||
* Removes/deletes all the keys in the JSON Cache, | ||
@@ -515,2 +545,21 @@ * having the prefix. | ||
} | ||
/** | ||
* Removes the given key from Redis | ||
* using the given transaction | ||
* | ||
* Please use this method instead of | ||
* directly using `redis.del` as this method | ||
* ensures that even the corresponding type info | ||
* is removed. It also ensures that prefix is | ||
* added to key, ensuring no other key is | ||
* removed unintentionally | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
*/ | ||
delT(transaction, key) { | ||
transaction.del(this.getKey(key)); | ||
transaction.del(this.getTypeKey(key)); | ||
return transaction; | ||
} | ||
/****************** | ||
@@ -517,0 +566,0 @@ * PRIVATE METHODS |
{ | ||
"name": "redis-json", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "A wrapper library to store JSON Objects in redis-hashsets and retrieve it back as JSON objects", | ||
@@ -73,4 +73,6 @@ "sideEffects": false, | ||
"@types/chai": "^4.2.3", | ||
"@types/ioredis": "^4.17.4", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^12.7.12", | ||
"@types/redis": "^2.8.27", | ||
"chai": "^4.1.2", | ||
@@ -77,0 +79,0 @@ "cli-progress": "^3.3.1", |
@@ -6,9 +6,8 @@ # redis-json [![npm version](https://badge.fury.io/js/redis-json.svg)](https://badge.fury.io/js/redis-json) [![Build Status](https://travis-ci.com/AkashBabu/redis-json.svg?branch=master)](https://travis-ci.com/AkashBabu/redis-json) [![Coverage Status](https://coveralls.io/repos/github/AkashBabu/redis-json/badge.svg?branch=master)](https://coveralls.io/github/AkashBabu/redis-json?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/0015747bb31d085adae8/maintainability)](https://codeclimate.com/github/AkashBabu/redis-json/maintainability) | ||
## 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. | ||
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(with the same types as the original object). | ||
## What's new in v4.0.0? | ||
Following up with [#3](https://github.com/AkashBabu/redis-json/issues/3) & [#8](https://github.com/AkashBabu/redis-json/issues/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). | ||
## What's new in v4.2.0? | ||
- In response to issue: [#13](https://github.com/AkashBabu/redis-json/issues/13), we now support transactions 🎉 via `setT()`, `rewriteT()` & `delT()` methods. | ||
- Now `rewrite` method also supports `expiry` options(same as [ISetOptions](./docs/interfaces/isetoptions.md)) | ||
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. | ||
@@ -19,5 +18,9 @@ ## Installation | ||
## API | ||
Please visit [this page](docs/README.md) for detailed API documentation. | ||
## Usage | ||
**Simple** | ||
```typescript | ||
@@ -102,3 +105,3 @@ import Redis from 'ioredis'; | ||
With custom stringifier and parser: | ||
**With custom stringifier and parser:** | ||
```typescript | ||
@@ -132,6 +135,23 @@ const jsonCache = new JSONCache(redis, { | ||
## API | ||
**With transactions:** | ||
```typescript | ||
const transaction = redisClient.multi(); | ||
Please visit [this page](docs/README.md) for detailed API documentation. | ||
transaction | ||
.set('name', 'foo') | ||
.set('bar', 'baz') | ||
jsonCache.setT(transaction, 'test', {name: 'testing'}) | ||
jsonCache.delT(transaction, 'test1') | ||
jsonCache.rewriteT(transaction, 'test2', {name: 'testing', age: 25}) | ||
transaction | ||
.exec(function(err, replies) => { | ||
/// your logic here after | ||
}) | ||
``` | ||
Please note that only `setT()`, `rewriteT()` & `delT()` supports transaction, where as `get()` & `clearAll()` do NOT support transaction because we process those results before returning it to the calling function. Moreover there is no real usecase in adding `get` methods to a transaction! | ||
## Since v4.0.0 | ||
@@ -138,0 +158,0 @@ |
import { IOptions, ISetOptions } from '../interfaces'; | ||
declare type Transaction = any; | ||
interface IJSONCache<T> { | ||
set(key: string, obj: T, options: ISetOptions): Promise<any>; | ||
get(key: string, ...fields: string[]): Promise<Partial<T> | undefined>; | ||
rewrite(key: string, obj: T): Promise<any>; | ||
rewrite(key: string, obj: T, options?: ISetOptions): Promise<any>; | ||
clearAll(): Promise<any>; | ||
del(key: string): Promise<any>; | ||
setT(transaction: Transaction, key: string, obj: T, options: ISetOptions): Transaction; | ||
rewriteT(transaction: Transaction, key: string, obj: T, options?: ISetOptions): Transaction; | ||
delT(transaction: Transaction, key: string): Transaction; | ||
} | ||
@@ -39,3 +43,3 @@ /** | ||
* @param redisClient RedisClient instance(Preferred ioredis - cient). | ||
* It support any redisClient instance that has | ||
* It supports any redisClient instance that has | ||
* `'hmset' | 'hmget' | 'hgetall' | 'expire' | 'del' | 'keys'` | ||
@@ -56,2 +60,13 @@ * methods implemented | ||
/** | ||
* Flattens the given json object and | ||
* stores it in Redis hashset using | ||
* the given transaction | ||
* | ||
* @param transaction redis transaction | ||
* @param key Redis key | ||
* @param obj JSON object to be stored | ||
* @param options | ||
*/ | ||
setT(transaction: Transaction, key: string, obj: T, options?: ISetOptions): Transaction; | ||
/** | ||
* Retrieves the hashset from redis and | ||
@@ -74,4 +89,12 @@ * unflattens it back to the original Object | ||
*/ | ||
rewrite(key: string, obj: T): Promise<any>; | ||
rewrite(key: string, obj: T, options?: ISetOptions): Promise<any>; | ||
/** | ||
* Replace the entire hashset for the given key | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
* @param obj JSON Object of type T | ||
*/ | ||
rewriteT(transaction: Transaction, key: string, obj: T, options?: ISetOptions): Transaction; | ||
/** | ||
* Removes/deletes all the keys in the JSON Cache, | ||
@@ -94,2 +117,17 @@ * having the prefix. | ||
del(key: string): Promise<any>; | ||
/** | ||
* Removes the given key from Redis | ||
* using the given transaction | ||
* | ||
* Please use this method instead of | ||
* directly using `redis.del` as this method | ||
* ensures that even the corresponding type info | ||
* is removed. It also ensures that prefix is | ||
* added to key, ensuring no other key is | ||
* removed unintentionally | ||
* | ||
* @param transaction Redis transaction | ||
* @param key Redis key | ||
*/ | ||
delT(transaction: Transaction, key: string): Transaction; | ||
/****************** | ||
@@ -96,0 +134,0 @@ * PRIVATE METHODS |
156112
2336
178
39