Socket
Book a DemoInstallSign in
Socket

node-redis-async

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-redis-async

Plugin for the [redis](https://github.com/NodeRedis/node_redis) package. Adds a useful `.cmd()` method to the redis client.

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
2
100%
Maintainers
1
Weekly downloads
 
Created
Source

node-redis-async

Plugin for the redis package. Adds a useful .cmd() method to the redis client.

Installation

npm install --save node-redis-async

# redis is required as a peer dependency
npm install --save redis

Usage

Basic

All commands are replaced by the promise-based .cmd() method.

const redis = require('redis');
require('node-redis-async'); // Load the plugin

const client = redis.createClient(6379, 'redis');

// node-redis-async usage
client.cmd(['set', 'key', 'val'])
	.then(res => console.log(res))
	.catch(err => console.error(err));

// You can still use redis normally
client.set('key', 'val', (err, res) => {
	if (err) {
		console.error(err);
	} else {
		console.log(res);
	}
});

Multi

Simply pass an array with multiple commands and they will be performed atomically. This removes boilerplate code by performing the necessary multi() and exec() calls under the hood.

client.cmd([
	['get', 'key'],
	['set', 'key1', '1'],
	['set', 'key2', '2'],
])
	.then(res => console.log(res)) // => [ 'val', 'OK', 'OK' ]
	.catch(err => console.error(err));

Casting

Redis stores all values as strings. By default, the .cmd() function casts values back to their original types.

client.cmd(['set', 'key', 2]);
client.cmd(['get', 'key'])
	.then(res => console.log(res, typeof res)); // => 2 Number

client.cmd(['set', 'key', true]);
client.cmd(['get', 'key'])
	.then(res => console.log(res, typeof res)); // => true Boolean

client.cmd(['set', 'key', '1a']);
client.cmd(['get', 'key'])
	.then(res => console.log(res, typeof res)); // => 1a String

Disable Casting

To disable casting, use the noCast flag.

client.cmd(['set', 'key', true]);
client.cmd(['get', 'key'], { noCast: true })
	.then(res => console.log(res, typeof res)); // => true String

Caveats

Note that a best guess is made when casting. Example:

// Set a string that is a valid number => returns a number
client.cmd(['set', 'key', '22']);
client.cmd(['get', 'key'])
	.then(res => console.log(res, typeof res)); // => 22 Number
	
// Set a string that is a valid bool => returns a bool
client.cmd(['set', 'key', 'true']);
client.cmd(['get', 'key'])
	.then(res => console.log(res, typeof res)); // => true Boolean

FAQs

Package last updated on 15 Feb 2018

Did you know?

Socket

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.