Basically, I am trying to store a JSON of my application in redis using redisJSON. And I have different functions here and there in the project where I modify and update the json. Now currently, when I tried storing and updating the values in redis, I have to continuously duplicate the values to make changes and then update it back to redis. Is there any alternative for this procedure?
const addUser = async () => {
try {
const USERS_KEY = 'users';
let mainUsers;
mainUsers = await redisClient.json.get(USERS_KEY, {
path: '.',
});
if (!mainUsers) {
await redisClient.json.set(USERS_KEY, '.', {});
mainUsers = await redisClient.json.get(USERS_KEY, {
path: '.',
});
}
const mainUsersCopy = { ...mainUsers };
console.log('The actual object we need', mainUsersCopy);
const newUser = {
socketId: 123,
roomId: 456,
teamName: teamA,
rank: 69,
userName: 'X'
};
mainUsersCopy[userName] = newUser;
await redisClient.json.set(USERS_KEY, '.', mainUsersCopy);
return { status: 2, userObj: mainUsersCopy[userName] };
} catch (err) {
console.log(err);
}
};
I have given an example of above and based on that can you guys provide me a solution for not using mainUsersCopy. FYI, console logging the mainUsers we get:
mainUsers is here [object Object]
So that is why the mainUsers has been destructured. Thanks in advance.