roseredis
Easy to use abstraction layer for node_redis
Installation
npm install roseredis --save
Usage Example
var rose = ;var roseClient = rose; var redisKey = foo: 'fooKey' bar: 'barKey'; var commands = setFoo: { return 'set'redisKeyfoovalue; } getFoo: { return command: 'get'redisKeyfoo { return resultfoo = reply; } ; } setBar: { return 'set'redisKeybarvalue; } getBar: { return command: 'get'redisKeybar { resultbar = || 0; } ; }; roseClient; roseClient ;
Creating the client
A rose client can be created by calling rose.createClient()
var rose = ;var roseClient = rose;
If you already have a redis client configured, you may pass this in.
var rose = ; var redis = ;var redisClient = redis; // Configure your redisClient here var roseClient = rose;
Once the rose client has been created, you may access it's redisClient at anytime from roseClient.redisClient
roseClientredisClient;// This will disconnect from the redis server
Full documentation of node_redis can be found here:
https://github.com/NodeRedis/node_redis#readme
Client clones
You may clone the client using client.createClient().
Commands registered in the clone will not affect the scope of the parent client.
Commands registered to the parent after the clone has been created will not affect the clone's scope.
roseClient; var cloneClient = roseClient; cloneClient; roseClient; console; // functionconsole; // undefinedconsole; // function console; // functionconsole; // functionconsole; // undefined
Creating commands
Rose commands are methods that return a redis command array and optional reply handler.
{ return command: 'get' 'fooKey' { resultfoo = reply; } ;}
Command is the redis command array.
(This gets the value stored at redis key 'fooKey')
command: 'get' 'fooKey'
Handler wraps the reply from redis.
reply is the redis response from your command
result is the object that gets returned in the callback
This allows you to give your result values meaningful names, instead of having to keep track of reply indices
{ resultfoo = reply;}
You can optionally return just the redis command if you're not doing anything with the redis reply.
(This is common for setters)
{ return 'set' 'fooKey' value;}
You can use the handler to provide additional formatting of the data, such as parsing values from the reply.
{ resultbar = || 0;}
Use result.setKey to set a deeply nested field in the result object.
{ return command: 'hget''foo' 'bar' { result; } ;} roseClient;roseClient;
(See the deepref github page for full documentation of setKey)
https://github.com/isaymatato/deepref
Registering commands
Once created, you'll need to register commands with the rose client.
This can be done one at a time, using client.registerCommand(label, method);
{ return 'set' 'fooKey' value;} roseClient;
Or, more typically, you can use registerCommands to register all the commands defined in an object
var commands = setFoo: { return 'set'redisKeyfoovalue; } getFoo: { return command: 'get'redisKeyfoo { resultfoo = reply; } ; };roseClient;
Executing commands
Once registered, you may execute a single command by calling roseClient[commandName]
The command will return a promise.
{ return 'set''foo'value;} roseClient;roseClient;
You may also execute multiple commands using roseClient.multi()
roseClient ;
Tests
npm test
Release Notes
As of version 1.4.0, all async methods return a promise. Callbacks are still supported, though it's recommended you use promises instead. The last argument passed in to any of the async methods will be treated as a callback if it is a function.
roseClient ;
Contributing
Please use the included style guide. If you change anything, please test and add unit tests for any new functionality. If you're fixing a bug, please add a unit test that would have failed before the bug was fixed. Thanks!