1.1.2 • Published 4 years ago

redis-everything v1.1.2

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Redis Everything

npm version Stars MIT license Issues

Built on top of Redis, this package allows for easy storage, access, and mutation of primitive and non-primitive data types.

Installation

npm install redis-everything

Usage Example

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)
      })
      
    })
   
}

API

redis.set(baseKey, key1, ..., value, callback)

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.

redis.get(baseKey, key1, ..., callback)

Gets an object, array, or primitive at the given baseKey. If more keys are provided, the object at the full path will be given.

redis.remove(baseKey, key1, ..., callback)

Removes an object, or array element at the given path. If just the baseKey is given, the full object will be removed from Redis.

redis.addKey(baseKey, key1, ..., newKey, value, callback)

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.

redis.push(baseKey, key1, ..., value1, value2, value3, ..., callback)

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.

redis.unshift(baseKey, key1, ..., value1, value2, value3, ..., callback)

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.

redis.pop(baseKey, key1, ..., callback)

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.

redis.shift(baseKey, key1, ..., callback)

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.

redis.arrayLength(baseKey, key1, ..., callback)

Returns the number of elements in a given array. The given path must lead to an array otherwise an error will be thrown.

redis.objectKeys(baseKey, key1, ..., callback)

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.

redis.keys(callback)

Gives a list of all baseKeys in the Redis

redis.allKeys(callback)

Gives a list of all keys in the Redis, including pointers to other objects.

redis.hasKey(baseKey, callback)

Returns true if the given baseKey exists in Redis. False otherwise.

redis.flushall(callback)

Removes all keys from Redis.

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago