store-redis

The Redis store is a wrapper over redis that provides connection state management and automatic creation of publisher/subscriber connections. All commands can run in both callback and promise mode.

Installation and usage
npm i --save thorin-store-redis@1.x
'use strict';
// app.js entry file
const thorin = require('thorin');

thorin.addStore(require('thorin-store-redis'));   // <- add this line
thorin.run((err) => {});

# run to setup the database and module
node app.js --setup=store.redis
Default configuration
  • debugfalseif set to true, log all redis operations
  • hostlocalhostthe redis server host
  • port6379the redis server port
  • passwordnullthe redis auth password (if any)
  • options{}additional redis-specific options.
  • namespacerns:The default project namespace to prepend for key() calls.
For a complete set of connection options, please visit the redis repository
Store functionality
storeObj.publish(channel, data) : Promise
Performs a publish to the given channel, of the given data. Internally, it will try to use the publish named redis connection. If no such connection exist, create a new one and use it in publish mode
  • channelstring the channel to publish to
  • dataanythe data to publish. If an object, will be stringified.
storeObj.subscribe(channel, callback) : Promise
Subscribes to the given channel, calling the callback function when a new event was published. Internally, it will try to use the subscribe named redis connection. If no such connection exists, create a new one and use it in subscribe mode.
  • channelstring the channel name to subscribe to
  • callbackfunctionthe function to call when an event was published.
storeObj.unsubscribe(channel, callback) : Promise
Unsubscribes from the given channel. If no callback is given, it will unsubscribe from all previously-subscribed channels.
  • channelstring the channel name to unsubscribe from.
  • callbackfunctionthe function that was used in the subscribe() call.
storeObj.exec(commands, fn) : Promise?
Executes any redis specific commands, returning a promise. If the last argument is a function, it will act as the callback and promises will not be used.
For a complete set of redis commands, visit the official docs.
'use strict';
const storeObj = thorin.store('redis');
storeObj.exec('GET', 'myKey').then((val) => log.info(val));
storeObj.exec('SETEX', 'myKey', 30, 'myValue', (err, res) => log.info(err, res));
storeObj.multi() : multiObj
Prepares for a MULTI command, returning an object that has exec() functions for command queueing and commit() to send out all the queued commands.
'use strict';
const storeObj = thorin.store('redis');
storeObj.multi()
   .exec('GET', 'myKey')
   .exec('SETEX', 'myKey', 'newVal')
   .commit()
   .then((results) => {});
storeObj.getConnection(name, opt) : Promise
Creates a new named connection or returns a previously created one.
  • namestringthe name of the new connection.
  • optobject additional options that will merge with the store's default configuration
storeObj.createConnection(name, opt, fn)
Creates a new named connection to the redis server.
  • namestringthe name of the new connection.
  • optobject additional options that will merge with the store's default configuration
  • fnfunctionthe callback function to call when the connection is ready.
storeObj.isConnected(name) : boolean
Verifies if the default or named redis connection is connected or not.
  • namestringthe name of the new connection.
storeObj.key(name) : string
Utility function that will use the configured namespace to generate a new key name.
Connection specific events
  • disconnect - the connection got disconnected
  • reconnect - the connection got reconnected
  • connect - fired once, when the connection was established
  • close - fired when the connection was closed.
  • subscribe.message (channel, msg) fired when a message was caught by a subscribed handler
  • subscribe.channel (channel, msg) fired when a handler was subscribed to a channel
  • unsubscribe (channel) - fired when a handler was unsubscribed from a channel
  • publish (channel, msg) - fired when a message was published to a channel by the current app
  • command (args) - fired when a command was executed
  • multi (args) - fired when a multi command was executed
'use strict';
const storeObj = thorin.store('redis');

storeObj.subscribe('my.channel', (message) => {
   log.info('Got a message', message);
}).then(() => {
   // subscription was created
   return storeObj.publish('my.channel', 'Hello world');
}).then(() => {
   // message got published.
   return storeObj.exec('SET', 'published:1', true);
});
Do you have a question or is something missing?

You can always create a new issue on GitHub or contact one of the core founders by chat.