Socket
Socket
Sign inDemoInstall

redis-obj

Package Overview
Dependencies
4
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    redis-obj

Mapping Redis with Javascript Object.


Version published
Weekly downloads
0
Maintainers
1
Install size
306 kB
Created
Weekly downloads
 

Readme

Source

redis-obj

redis-obj is Library mapping redis with javascript object.

Installation

npm install redis-obj

Usage Example

1-1. How to save object

let RedisObj = require('redis-obj');
let redis = require('redis');
let redisClient = redis.createClient();

// example class
// your class inherits RedisObj
class example extends RedisObj {
    constructor() { // until now, you can not use constructor parameters.
        super(); // you must call super() before using 'this' keyword.
        this.title = "redis-obj";
        this.name = "JungGukAn";
        this.money = 1;
        this.friends = ['friends1'],
        this.detail = {
            phone: '0101231234',
            skill: ['nodejs', 'c#', 'c++']
        }
    }
    showMeTheMoney() {
        if(this.name == "JungGukAn"){
            this.money = 10000000;
        }else{
            this.money = 0;
        }
    }

    changeName(name){
        this.name= name;
    }
}

let e = new example(); // create new instance.

e.save(1, redisClient).then((e) => {
    console.log(e.title);
    console.log(e.name);
    console.log(e.detail.skill);
}) //save current class properties in redis as id 1


1-2. Log Result

save

2-1. How to get object

// 'get' method makes you get object saved in redis.
// Caution! this method only guarantee locking until get object.
// if you want to guarantee locking from getting object to saving object, use open() and close().
example.get(1, redisClient).then((ex) => {
    console.log(ex.title);
    console.log(ex.name);
    console.log(ex.money);
    ex.showMeTheMoney();
    // 'save' method save current properties regardless of whether object has changed in redis.  
    ex.save().then((e) => {
        console.log(e.money);
    })
})

2-2. Log Result

get

3-1. How to lock all process

// 'open' method is similar with 'get' method.
// but if you want to guarantee locking from getting object to saving object, use open() and close().
// Caution! you have to use open() with close().
// In fact, this uses optimistic lock.
// So, when something in redis is changed in middle of process, repeat getting object, execution methods you used, and saving.

example.open(1, redisClient).then((ex) => {
    // ex.title = 'dont access property directly'
    ex.changeName("AlbertAn");
    ex.showMeTheMoney();
    ex.close(); //this also has promise pattern.
})

Keywords

FAQs

Last updated on 13 May 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc