# redis-obj

> Mapping Redis with Javascript Object.

Latest version **0.2.5** (published 2017-05-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install redis-obj
pnpm add redis-obj
yarn add redis-obj
bun add redis-obj
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.5 |
| Published | 2017-05-13 |
| First published | 2017-04-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | JungGukAn |
| Maintainers | junggukan |
| Keywords | redis-orm |

## Links

- npm: https://www.npmjs.com/package/redis-obj
- Repository: https://github.com/AnJungGuk/redis-obj
- Homepage: https://github.com/AnJungGuk/redis-obj#readme
- Issues: https://github.com/AnJungGuk/redis-obj/issues
- npm.io page: https://npm.io/package/redis-obj

## Dependencies (1)

- [redis](https://npm.io/package/redis.md) ^2.7.1

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.2.5 (latest) — 2017-05-13
- 0.2.2 — 2017-05-10
- 0.2.0 — 2017-05-07
- 0.1.1 — 2017-04-13

## README

# 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](./examples/save.PNG)

### 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](./examples/get.PNG)

### 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.
})
```

---
_Source: https://npm.io/package/redis-obj · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
