backbone-db-redis
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -181,3 +181,4 @@ /** | ||
/* | ||
* Read from multiple sets, storing union in new set temporarily | ||
* Read from multiple sets, storing union/intersection in new set temporarily | ||
* unionKey or intersectionKey should be given in options | ||
*/ | ||
@@ -187,2 +188,4 @@ readFromIndexes: function(collection, options, cb) { | ||
var unionKey = options.unionKey; | ||
var intersectionKey = options.intersectionKey; | ||
var expirationTime = options.expirationTime || 300; | ||
var params = _.clone(options.indexKeys); | ||
@@ -192,3 +195,3 @@ if (collection.indexSort) { | ||
} | ||
params.unshift(unionKey); // where to store | ||
params.unshift(unionKey || intersectionKey); // where to store | ||
if (options.weights) { | ||
@@ -203,9 +206,20 @@ params.push('WEIGHTS'); | ||
var unionFn = collection.indexSort ? | ||
_.bind(this.redis.zunionstore, this.redis) : | ||
_.bind(this.redis.sunionstore, this.redis); | ||
unionFn(params, function(err) { | ||
var fn; | ||
if (unionKey) { | ||
fn = collection.indexSort | ||
? _.bind(this.redis.zunionstore, this.redis) | ||
: _.bind(this.redis.sunionstore, this.redis); | ||
} else if (intersectionKey) { | ||
fn = collection.indexSort | ||
? _.bind(this.redis.zinterstore, this.redis) | ||
: _.bind(this.redis.sinterstore, this.redis); | ||
} | ||
if (!fn) { | ||
throw new Error('unionKey or intersectionKey must be given'); | ||
} | ||
fn(params, function(err) { | ||
if (err) return cb(err); | ||
self.redis.expire(unionKey, 300); | ||
options.indexKey = unionKey; | ||
self.redis.expire(unionKey, expirationTime); | ||
options.indexKey = unionKey || intersectionKey; | ||
return self.readFromIndex(collection, _.extend({limit_query: false}, options), cb); | ||
@@ -212,0 +226,0 @@ }); |
{ | ||
"name": "backbone-db-redis", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Redis driver for Backbone.Db", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -168,3 +168,3 @@ var assert = require('assert'); | ||
it('should read from multiple indexes', function(done) { | ||
it('should read from multiple indexes and store union', function(done) { | ||
var opts = { | ||
@@ -247,2 +247,15 @@ indexKeys: ['test:i:Foo:relation', 'test:i:Foo:relation2'], | ||
// TODO: improve this test | ||
it('should read from multiple indexes and store their intersection', function() { | ||
var opts = { | ||
indexKeys: ['test:i:Foo:relation', 'test:i:Foo:relation2'], | ||
intersectionKey: 'test:i:IntersectionFoo' | ||
}; | ||
var c = new TestCollection2(); | ||
return c | ||
.readFromIndexes(opts) | ||
.then(function() { | ||
assert.equal(c.length, 0); | ||
}); | ||
}); | ||
/* | ||
@@ -249,0 +262,0 @@ it('should remove model from index', function(done) { |
95226
2370