What is @redis/json?
@redis/json is an npm package that provides a client for Redis' JSON capabilities, allowing you to store, retrieve, and manipulate JSON documents directly within Redis. This package leverages the RedisJSON module, which is designed to handle JSON data efficiently.
What are @redis/json's main functionalities?
Set JSON Data
This feature allows you to set JSON data in a Redis key. The code sample demonstrates how to connect to a Redis client, set a JSON object under a specific key, and then disconnect from the client.
const { createClient } = require('@redis/json');
async function setJsonData() {
const client = createClient();
await client.connect();
await client.json.set('user:1', '$', { name: 'John Doe', age: 30, city: 'New York' });
await client.disconnect();
}
setJsonData();
Get JSON Data
This feature allows you to retrieve JSON data from a Redis key. The code sample shows how to connect to a Redis client, get the JSON object stored under a specific key, log the data, and then disconnect from the client.
const { createClient } = require('@redis/json');
async function getJsonData() {
const client = createClient();
await client.connect();
const userData = await client.json.get('user:1');
console.log(userData);
await client.disconnect();
}
getJsonData();
Update JSON Data
This feature allows you to update specific fields within a JSON object stored in Redis. The code sample demonstrates how to connect to a Redis client, update the 'age' field of the JSON object under a specific key, and then disconnect from the client.
const { createClient } = require('@redis/json');
async function updateJsonData() {
const client = createClient();
await client.connect();
await client.json.set('user:1', '$.age', 31);
await client.disconnect();
}
updateJsonData();
Delete JSON Data
This feature allows you to delete JSON data from a Redis key. The code sample shows how to connect to a Redis client, delete the JSON object stored under a specific key, and then disconnect from the client.
const { createClient } = require('@redis/json');
async function deleteJsonData() {
const client = createClient();
await client.connect();
await client.json.del('user:1');
await client.disconnect();
}
deleteJsonData();
Other packages similar to @redis/json
ioredis
ioredis is a robust, full-featured Redis client for Node.js. While it does not natively support JSON operations, it can be used in conjunction with RedisJSON to achieve similar functionality. ioredis is known for its performance and support for advanced Redis features like clustering and sentinel.
redis
redis is the official Node.js client for Redis. Like ioredis, it does not have built-in support for JSON operations but can be used with RedisJSON. It is a straightforward and reliable client for basic Redis operations.
node-redis
node-redis is another popular Redis client for Node.js. It provides a simple and efficient way to interact with Redis but lacks native JSON support. It can be paired with RedisJSON for handling JSON data.
@redis/json
This package provides support for the RedisJSON module, which adds JSON as a native data type to Redis. It extends the Node Redis client to include functions for each of the RedisJSON commands.
To use these extra commands, your Redis server must have the RedisJSON module installed.
Usage
For a complete example, see managing-json.js
in the Node Redis examples folder.
Storing JSON Documents in Redis
The JSON.SET
command stores a JSON value at a given JSON Path in a Redis key.
Here, we'll store a JSON document in the root of the Redis key "mydoc
":
import { createClient } from 'redis';
...
await client.json.set('noderedis:jsondata', '$', {
name: 'Roberta McDonald',
pets: [
{
name: 'Rex',
species: 'dog',
age: 3,
isMammal: true
},
{
name: 'Goldie',
species: 'fish',
age: 2,
isMammal: false
}
]
});
For more information about RedisJSON's path syntax, check out the documentation.
Retrieving JSON Documents from Redis
With RedisJSON, we can retrieve all or part(s) of a JSON document using the JSON.GET
command and one or more JSON Paths. Let's get the name and age of one of the pets:
const results = await client.json.get('noderedis:jsondata', {
path: [
'.pets[1].name',
'.pets[1].age'
]
});
results
will contain the following:
{ '.pets[1].name': 'Goldie', '.pets[1].age': 2 }
Performing Atomic Updates on JSON Documents Stored in Redis
RedisJSON includes commands that can atomically update values in a JSON document, in place in Redis without having to first retrieve the entire document.
Using the JSON.NUMINCRBY
command, we can update the age of one of the pets like this:
await client.json.numIncrBy('noderedis:jsondata', '.pets[1].age', 1);
And we can add a new object to the pets array with the JSON.ARRAPPEND
command:
await client.json.arrAppend('noderedis:jsondata', '.pets', {
name: 'Robin',
species: 'bird',
age: 1,
isMammal: false
});