This module adds a layer of commands to interact with Redis ReJSON module on top of IORedis.
For IORedis commands please refer to IORedis repository.
This module uses ioredis and @types/ioredis as a dependencies
Known limitations: multi
/ pipeline
is not supported with this module.
Disclaimer: this module is not battle tested, if you find any issue with it please open an issue or a pull request.
-
Install module
yarn add ioredis-rejson yarn add -D @types/ioredis
or
npm install --save ioredis-rejson npm install --save-dev @types/ioredis
-
Create a client instance
import Redis from 'ioredis-rejson';
const redis = new Redis({
host: '127.0.0.1',
port: 16379,
});
- Interact with redis using the just created client instance
...
const main = async () => {
// ReJSON Methods
const res = await redis.json_set('JSONKEY', '.', { foo: 'bar' }, 'NX');
const res2 = await redis.json_get('JSONKEY');
console.log(res); // OK
console.log(res2); // { foo: "bar"}
// IORedis Methods
const res3 = await redis.set('KEY', 'VALUE');
const res4 = await redis.get('KEY');
console.log(res3); // OK
console.log(res4); // "VALUE"
}
main()
NOTE: ioredis-rejson serializes and deserializes data where needed to facilitate interaction, you can pass objects, arrays, strings, etc and it will be serialized as json where it needs to be, same for get requests, the returned data will be deserialized.
await redis.json_set('KEY', 'PATH', 'DATA', '*optional* NX | XX');
String OK
if executed correctly or null
if the specified NX
XX
conditions were not met.
O(M+N), where M is the size of the original value (if it exists) and N is the size of the new value.
await redis.json_get('KEY', '*optional* PATH');
Returns the parsed json data from path
or null
O(N), where N is the size of the value.
await redis.json_mget(['KEY1', 'KEY2', '...'], 'PATH');
Returns the parsed json data from path
or null
O(M*N), where M is the number of keys and N is the size of the value.
await redis.json_del('KEY', '*optional* PATH');
Integer, specifically the number of paths deleted (0 or 1).
O(N), where N is the size of the deleted value.
await redis.json_numincrby('KEY', 'PATH', 'NUMBER');
Float, specifically the new value
O(1).
await redis.json_nummultby('KEY', 'PATH', 'NUMBER');
Float, specifically the new value
O(1).
await redis.json_strappend('KEY', '*optional* PATH', 'NUMBER');
Integer, specifically the string's new length.
O(N), where N is the new string's length.
await redis.json_strlen('KEY', '*optional* PATH');
Integer, specifically the string's length.
O(1).
await redis.json_arrapend('KEY', 'PATH', 'DATA | [DATA]');
Integer, specifically the array's new length.
O(1).
await redis.json_arrindex(
'KEY',
'PATH',
'DATA',
'*optional* START',
'*optional* STOP'
);
Integer, specifically the position of the scalar value in the array, or -1 if unfound.
O(N), where N is the array's size.
await redis.json_arrinsert('KEY', 'PATH', 'INDEX', 'DATA | [DATA]');
Integer, specifically the array's new size.
O(N), where N is the array's size.
await redis.json_arrlen('KEY', '*optional* PATH');
Integer, specifically the array's length.
O(1).
await redis.json_arrpop('KEY', '*optional* PATH', '*optional* INDEX');
The deserialized popped value
O(N), where N is the array's size for index
other than the last element, O(1) otherwise.
await redis.json_arrtrim('KEY', 'PATH', 'START', 'STOP');
Integer, specifically the array's new size.
O(N), where N is the array's size.
await redis.json_objkeys('KEY', '*optional* PATH');
Array, specifically the key names in the object as strings.
O(N), where N is the number of keys in the object.
await redis.json_objlen('KEY', '*optional* PATH');
Integer, specifically the number of keys in the object.
O(1).
await redis.json_type('KEY', '*optional* PATH');
Simple String, specifically the type of value.
O(1).
same as json_del
await redis.json_forget('KEY', '*optional* PATH');
Integer, specifically the number of paths deleted (0 or 1).
O(N), where N is the size of the deleted value.
await redis.json_resp('KEY', '*optional* PATH');
Array, specifically the JSON's RESP form as detailed.
O(N), where N is the size of the JSON value.