redisify

0.0.1 • Public • Published

Redisify

  • adds object orientated redis calls to an object (or prototype)
  • acts as a proxy to node_redis client, but with a namespace key pulled from the object
  • Also trailing function calls act as transformations. E.g. obvious usecase - bulk load objects from redis ids

Usage

my_object.redis = redisify([client], [key_fn])
  • client: redis client.
  • key_fn: property or function that contains the object's redis namespace key (defaults to 'key')
  • 'redis' can be anything

Debugging can be turn on via the 'log' property of the proxy:

my_object.redis.log = console.log 

Examples

var redisify = require("redisify")
var client = require("redis").createClient()

var User = {
  key: "Users"
}
User.redis = redisify(client)

User.redis("get", "xx", function(val) {
  // redis "get Users:xx"
  // User == this
})

It can be mixed into a prototype as well:

function User(id) {
  this.key = "User:" + id
}

User.prototype.redis = redisify()
User.prototype.redis.client = client // alternate way of specifying client

var user = new User(42)

user.redis("get", "xx", function(val) {
  // redis "get User:42:xx" 
  // user == this
})

Example 2

Showing transformations: trailing function calls are called as an asynchronous stack of maps.

Eg. if User.load_bulk might instantiate a list of User objects from a list of ids:

User.redis("smembers", "all", User.load_bulk, function(users) {
  // users is now a list of instantiated objects
})

Example 3

Showing mounting to a different part of the object and a different property

var User = {
  namespace: "Users"
}

User.db = redisify(client, "namespace")
User.db.log = console.log // log out redisify proxy calls to node_redis

User.db("get", "mystring", function(val) {
  // redis "get Users:mystring"
  // User == this
})

Install

npm install redisify

run tests

make

License

(The MIT License)

Copyright (c) 2011 weepy <jonahfox@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Dependents (0)

Package Sidebar

Install

npm i redisify

Weekly Downloads

2

Version

0.0.1

License

none

Last publish

Collaborators

  • weepy