A nodejs reimagining of the redis-objects ruby gem, acting as an object-oriented bridge between redis and node.
This has a number of advantages over working with the node-redis
api directly.
- Complex data structures are automatically marshaled (if {marshal: true})
- Type coercion for things that are numeric
- Cleans up some warts (sorted set results with scores are returned as a arrays of pairs of [value, score])
There is a class that maps to each Redis type, with methods for each Redis API command.
Note that calling new
does not imply it's actually a "new" value - it just
creates a mapping between that object and the corresponding Redis data
structure, which may already exist on the redis-server
.
// eventScores is a redis sorted set where all the values are player IDs.
var eventId = 1;
var eventScores = redis_objects.SortedSet('eventScore:' + eventId, {marshal: 'Integer'});
eventScores.add(/* player ID */ 1, /* score */ 2, function(e, res) {
// res is 1, because one item was added.
});
eventScores.addAll([[2, 3], [4, 1]], function(e, res) {});
eventScores.slice(0, 3, function(e, res) {
// res is now [4, 1]
});
Add it to your application's package.json, or run:
npm install redis-objects --save
Then, set up the redis-object connection:
var redis_objects = require('redis-objects');
var redis = require('redis');
// This sets it up globally:
redis_objects.connect(redis.createClient());
// You can also set it when creating a new redis object:
var newValue = new redis_objects.Value('testKey', redis.createClient(6379));
Sweet sweet comprehensive documentation will be forthcoming.
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don’t break it in a future version unintentionally.
- Run the tests, either using
npm test
ornpm test --cover
(this will also generate a pretty coverage report.) - Commit your changes.
- Send me a pull request.
- Bam! Done!