predis

Redis interface with promises

npm install predis
1 downloads in the last day
1 downloads in the last week
9 downloads in the last month

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. Check Promise code, tests, and poor documentation to get some ideas how you can use it.

Usage

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)

Examples

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 and 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, add to 'stooges', and list all stooges

client.incr('next:user').then(function(id) {
    return client.set(['user:' + id, 'Andy']).then(function() {
         return client.sadd(['stooges', 'user:' + id]).success(function() {
             console.log('done');
         }).fail(function(err) {
             console.error('error occurred', err);
         });
    });
}).always(function() {
    return client.smembers('stooges').then(client.mget).then(function(stooges) {
        console.log(stooges); // => ['Andrei', 'Dave', 'Sasha', 'Andy']
    })
});

multi & exec

Predis doesn't 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);
});
npm loves you