predis

Redis interface with promises

Predis = promise + redis

Predis is based on node-redis. Instead invoking callback function after communication with database it immediately returns a Promise object that will be resolved or rejected later.

Refer to node-redis documentation for available commands and arguments.

createClient method takes an optional argument with server, port, and password for redis server:

var predis = require("predis");
var client = predis.createClient({
    port: 10229,
    server: "pub-redis-777.us-east-1-4.2.ec2.garantiadata.com",
    password: "secret"
});

If no options provided node-redis will try to connect to 127.0.0.1 on port 6379 (default for redis-server)

Simple get example

client.get("user:123").then(function(user) {
    console.log(user);
});

Get all keys and their types

client.keys("*").then(function(keys) {
    return new Promise(keys.map(function(key) {
        return client.type(key);
    })).then(function(types) {
        return _.object(keys, types);
    })
});

Fetch set with pointers with their values

client.smembers('stooges').then(function(users) {
    // users => ['user:1', 'user:2', 'user:3'] 
    return client.mget(users).then(function(names) {
        // names => ['Andrei', 'Dave', 'Sasha'] 
        return _.object(users, names); // => {'user:1': 'Andrei', 'user:2': 'Dave', 'user:3': 'Sasha'} 
    })
});

Store new user and add to 'stooges'

client.incr('next:user').then(function(id) {
    return client.set(['user:' + id, 'Andy']).success(function() {
        alert('Andy stored as user:' + id);
    }).fail(function(err) {
        console.error('error occurred', err);
    });
}).then(function(id) {
    return client.sadd(['stooges', 'user:' + id]).success(function() {
        alert('done');
    }).fail(function(err) {
        console.error('error occurred', err);
    });
});

Predis does not return a promise after Multi and exec commands. You can use this approach instead:

var destroyModel = function(model) {
    var p = new Promise;
    var multi = client.multi();
 
    model.properties.forEach(function(key) {
        multi.del(key);
    });
 
    multi.exec(function (err) {
        if (err) {
            p.reject(err);
        } else {
            p.resolve("Model " + model.name + " successfully deleted");
        }
    });
 
    return p;
};
 
destroyModel(someModel).then(function(message) {
    console.log(message);
});