redis / node-redis

Redis Node.js client
https://redis.js.org/
MIT License
16.9k stars 1.89k forks source link

Bind client methods to the client instance #616

Closed balupton closed 9 years ago

balupton commented 10 years ago

It would be nice for the constructor of the redis client, to bind it's method to the instance, so I can throw around the methods using functional programming paradigms, without having to bind them myself. E.g. this is some code I recently wrote using ChainyJS and FeathersJS:

var services = {
    '/rsku': {
        // Return all todos from this service
        find: function(params, next) {
            Chainy.create()
                .set('rskus')
                .action(config.r.hgetall.bind(config.r))
                .decodetable()
                .done(next)
        },

        // Get an object
        get: function(id, params, next) {
            Chainy.create()
                .set(['rskus', id])
                .action(config.r.hget.bind(config.r))
                .decode()
                .done(next)
        },

        // Create a new Todo with the given data
        create: function(data, params, next) {
            console.log(data, params)
            Chainy.create()
                .set(['rskus:count'])
                .action(config.r.incr.bind(config.r))
                .log()
                .action(function(id){
                    data.id = id
                    return ['rskus', id, JSON.stringify(data)]
                })
                .action(config.r.hsetnx.bind(config.r))
                .action(function(result){
                    if ( !result ) {
                        err = new Error('id already existed')
                        return err
                    }
                    return data
                })
                .done(next)
        }
    }
}

Would be great if I could get rid of that .bind(config.r)

The following code added to the constructor of the client should do the trick:

var client = this;
Object.keys(client).forEach(function(key){
    var method = client[key];
    if ( typeof method !== 'function' )  return;
    client[key] = method.bind(client)
});
blainsmith commented 9 years ago

In order to better support this project and its new group of collaborators under a new org we are trying to clean up and close issues older than 6 months. Your issue may have been fixed since the issue was open, however, if you are still experiencing a problem please re-open this issue and we will label it accordingly.

BridgeAR commented 9 years ago

@balupton It is easy to implement this by your own in the way you want it to. That is likely the best to do here, as we'd also have to bind multi statements and batch commands that will be added at some point. And the later two would have a new instance each time you call them, so it'd be a performance overhead to bind the functions in that case.

So instead of adding this to the core, you can just do the same right after instantiating the client / multi.