Built on top of Redis, this package allows for easy storage, access, and mutation of primitive and non-primitive data types.
npm install redis-everything
For a full list of available options see https://www.npmjs.com/package/redis#options-object-properties
let options = {
host: "localhost",
port: 6379,
namespace: "myDb"
}
const redis = require('redis-everything')(options)
let test = async () => {
let userObj = {
name: "Sam",
age: 20,
address:{
number: 251,
street: "6th St",
city: "San Francisco",
state: "CA",
zip: 94103
},
friends: [{ name: "Jake" }, { name: "Ryan" }, { name: "Andrew" }]
}
//Insert user object into Redis with id "userId"
await redis.set("userId", userObj)
//Get user's address object
let myAddress = await redis.get(["userId", "address"])
/*
* myAddress = {
* number: 251,
* street: "6th St",
* city: "San Francisco"
* state: "CA",
* zip: 94103
* }
*/
//Get first object in friend array.
let firstFriend = await redis.get(["userId", "friends", 0])
/*
* firstFriend = {
* name: "Jake"
* }
*/
let newFriend = { name: "Matt" }
//Add new friend to the friend array
await redis.push(["userId", "friends"], newFriend)
//Add new attribute to user object
await redis.addKey(["userId", "car"], "Ford Escape")
//Update user attributes
await redis.set(["userId", "age"], 21)
let newCarSpecs = {
make: "Ford",
model: "Escape"
}
await redis.set(["userId", "car"], newCarSpecs)
//Remove element from an array
await redis.remove(["userId", "friends", 2])
//Remove a key
await redis.remove(["userId", "name"])
//Get full object from redis
userObj = await redis.get("userId")
/*
* userObj = {
* age: 21,
* address:{
* number: 251,
* street: "6th St",
* city: "San Francisco",
* state: "CA",
* zip: 94103
* },
* friends: [{ name: "Jake" }, { name: "Ryan" }, { name: "Matt" }],
* car:{
* make: "Ford",
* model: "Escape"
* }
* }
*/
//Get full list of keys in namespace
let keys = await redis.keys()
/*
* keys = ["userId"]
*/
//Remove full object from Redis
await redis.remove("userId")
//All functions also can use callbacks instead
redis.set("myObj", { key1: "value" }, (err) => {
if(err) throw err
redis.get("myObj", (err, myObj) =>{
if(err) throw err
console.log("myObj is: ", myObj)
})
})
}
Sets an object, array, or primitive at the given baseKey. If baseKey does not exists, then it will be created. In order to mutate an object, or array, you can use this function and specify the path of the object you would like to update.
Gets an object, array, or primitive at the given baseKey. If more keys are provided, the object at the full path will be given.
Removes an object, or array element at the given path. If just the baseKey is given, the full object will be removed from Redis.
Adds an object, array, or primitive at the given baseKey. The last key given will be the name of the new key assigned to the given value.
Adds an object, array or primitive at the end of the given array. The given path must lead to an array otherwise an error will be thrown.
Adds an object, array or primitive at the begining of the given array. The given path must lead to an array otherwise an error will be thrown.
Removes an object, array or primitive at the end of the given array. This will function will return the object that was popped. The given path must lead to an array otherwise an error will be thrown.
Removes an object, array or primitive at the begining of the given array. This will function will return the object that was removed. The given path must lead to an array otherwise an error will be thrown.
Returns the number of elements in a given array. The given path must lead to an array otherwise an error will be thrown.
Returns an array of strings, each string being a key from the given object. The given path must lead to a json object otherwise an error will be thrown.
Gives a list of all baseKeys in the Redis
Gives a list of all keys in the Redis, including pointers to other objects.
Returns true if the given baseKey exists in Redis. False otherwise.
Removes all keys from Redis.