v14.0.0: Redis Config and Named Validators & Formatters

9 views
Skip to first unread message

Evan Tahler

unread,
Jun 23, 2016, 6:47:28 PM6/23/16
to actionHero.js
https://github.com/evantahler/actionhero/releases/tag/v14.0.0

Named Validators & Formatters

Allows for action validators and formatters to use both named methods and direction functions.

exports.cacheTest = {
  name: 'cacheTest',
  description: 'I will test the internal cache functions of the API',
  outputExample: {},

  inputs: {
    key: {
      required: true,
      formatter: [
         function(s){ return String(s); },
         'api.formatter.uniqueKeyName' // <----------- HERE
    },
    value: {
      required: true,
      formatter: function(s){ return String(s); },
      validator: function(s){
        if(s.length < 3){ return '`value` should be at least 3 letters long'; }
        else{ return true; }
      }
    },
  },

  run: function(api, data, next){
    // ...
  }

};

And then you would define an initializer with your formatter:

'use strict';

module.exports = {
  initialize: function(api, next){
    api.formatter = {
      uniqueKeyName: function(key){
        return key + '-' + this.connection.id;
      }
    };

    next();
  },
};

Redis Client

There are so many ways to configure redis these days... handling the config options for all of them (sentinel? cluster?) is a pain... so lets just let the users configure things directly. It will be so much simpler!

This will be a breaking change

  • in config/redis.js, you now define the 3 redis connections you need explicitly rather than passing config options around:
var host     = process.env.REDIS_HOST || '127.0.0.1';
var port     = process.env.REDIS_PORT || 6379;
var database = process.env.REDIS_DB   || 0;

exports['default'] = {
  redis: function(api){
    var Redis = require('ioredis');

    return {
      '_toExpand': false,
      // create the redis clients
      client:     Redis.createClient(port, host),
      subscriber: Redis.createClient(port, host),
      tasks:      Redis.createClient(port, host),
    };
  }
};
  • move api.config.redis.channel to api.config.general.channel
  • move api.config.redis. rpcTimeout to api.config.general. rpcTimeout
  • throughout the code, use api.config.redis.client rather than api.redis.client
Reply all
Reply to author
Forward
0 new messages