node-redis-promise

Helper methods for Redis and global connection handler

Usage no npm install needed!

<script type="module">
  import nodeRedisPromise from 'https://cdn.skypack.dev/node-redis-promise';
</script>

README

node-redis-promise

Build Status Code Coverage NPM

Helper methods for Redis and global connection handler

Note: This module uses ES6 code and it's meant to be used with iojs in conjunction with the --es_staging flag.

Install

npm install node-redis-promise --save

Usage Example

First you need to create a global connection, for example in your index.js:

var redis = require('node-redis-promise');

redis.connect();

And then in foo.js:

var redis = require('node-redis-promise');

redis.set({foo: 'bar'}, {
      prefix: 'bar_',
      expire: 60 // TTL of 1 minute
    })
    .then(function(key) {
      console.log(key); // => bar_sjk21x

      return redis.get(key, true); // get key and JSON-decode the value
    })
    .then(function(bar) {
      console.log(bar); // => {foo: 'bar'}
    })
    .catch(function(err) {
      // always handle your errors!
    });

If you need to quit the global conneciton you can do so it two ways:

var redis = require('node-redis-promise');
redis.quit();

// OR
global.redisCloseConn();

API

redis.connect()

Create a connection to Redis. Refer to the Node Redis module documentation.

Returns a Promise:

  • Fulfilled: on succesfull connection
  • Rejected: when a connection error occours

Promise, fulfilled on a succesfull connection and reject on a connection error

redis.get(key [, json])

Get the value of a key in Redis.

Params:

  • key (String): key to lookup
  • json (Boolean, optional): if set to true, JSON-decode the result

Returns: Promise

redis.set(value [, options])

Set a value in Redis, with some advanced options.

Params:

  • value: value to store
  • options (Object, optional): see below avaliable options

Options:

  • key (String): key to use when storing the value, defaults to a generated id using shortid
  • prefix (String): prefixes keys with the specified value
  • json (Boolean): JSON-encode the value before storing it. By default automatically attempts to detect Objects by using typeof value === 'object'
  • expire (Number): if sets, it will be used to set the TTL option on the value (seconds).

redis.client

Native Redis client

redis.quit

Safely closes the Redis conneciton

Tests

npm test

Contributing

Yes please. There is a .jshintrc and .jscsrc in this repo.

Use your favorite editor JsHint and JSCS plugins to lint the code, please.

License

The Unlicense