backbone-db-redis
Advanced tools
Comparing version 0.0.44 to 0.0.45
10
index.js
@@ -330,2 +330,3 @@ var _ = require('lodash'), | ||
var done = function(err, data) { | ||
data = data || []; // Data might be null on errors | ||
var models = []; | ||
@@ -387,2 +388,7 @@ var i = 0; | ||
params = [setKey, 'BY', sortParams]; | ||
if (options.limit_query !== false && options.offset) { | ||
params.push("LIMIT", options.offset || 0, options.limit || -1); | ||
} | ||
if (parsedSort.sortOrder === -1) { | ||
@@ -475,3 +481,3 @@ params.push('DESC'); | ||
options.indexKey = unionKey; | ||
return self.readFromIndex(collection, options, cb); | ||
return self.readFromIndex(collection, _.extend({limit_query: false}, options), cb); | ||
}); | ||
@@ -555,2 +561,2 @@ }, | ||
RedisDb.Hash = require('./lib/hash'); | ||
module.exports = RedisDb; | ||
module.exports = RedisDb; |
@@ -59,2 +59,4 @@ var _ = require('lodash'); | ||
this._offsetApplied = false; | ||
fetchFn(function(err, results) { | ||
@@ -74,2 +76,3 @@ debug('results:', results); | ||
var ids = results; | ||
self.searchByIds(ids, callback); | ||
@@ -150,3 +153,4 @@ }); | ||
if (!this.filterOptions.sort) { | ||
this.db.redis.sort(collectionKey, 'BY', 'nosort', cb); | ||
this._offsetApplied = true; | ||
this.db.redis.sort(collectionKey, 'BY', 'nosort', 'LIMIT', this.offset, this.limit, cb); | ||
} else { | ||
@@ -162,4 +166,6 @@ if (this.filterOptions.customSort) { | ||
if (!ids.length) return callback(null, []); | ||
//TODO: already apply limit & offset on query | ||
ids = ids.splice(this.offset, this.limit); | ||
if (this._offsetApplied !== true) { | ||
ids = ids.splice(this.offset, this.limit); | ||
} | ||
var keys = _.map(ids, function(id) { | ||
@@ -204,3 +210,4 @@ // if indexed value is the hash key | ||
var sortParams = this.db.key(customSort[parsedSort.sortProp]); | ||
var params = [set, 'BY', sortParams]; | ||
this._offsetApplied = true; | ||
var params = [set, 'BY', sortParams, 'LIMIT', this.offset, this.limit]; | ||
if (parsedSort.sortOrder === -1) { | ||
@@ -218,3 +225,4 @@ params.push('DESC'); | ||
var sortParams = idKey + ':*->' + parsedSort.sortProp; | ||
var params = [set, 'BY', sortParams]; | ||
this._offsetApplied = true; | ||
var params = [set, 'BY', sortParams, 'LIMIT', this.offset, this.limit]; | ||
if (parsedSort.sortOrder === -1) { | ||
@@ -221,0 +229,0 @@ params.push('DESC'); |
{ | ||
"name": "backbone-db-redis", | ||
"version": "0.0.44", | ||
"version": "0.0.45", | ||
"description": "Redis driver for Backbone.Db", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -205,2 +205,11 @@ require('chai').should(); | ||
it('should read from index sorted by `a` with offset', function() { | ||
return index | ||
.readFromIndex({sort: 'a', offset: 1}) | ||
.then(function() { | ||
index.length.should.equal(2); | ||
index.at(0).get('a').should.equal(125); | ||
}); | ||
}); | ||
it('should read from index sorted by `-a`', function() { | ||
@@ -215,2 +224,11 @@ return index | ||
it('should read from index sorted by `-a` with offset', function() { | ||
return index | ||
.readFromIndex({sort: '-a', offset: 1}) | ||
.then(function() { | ||
index.length.should.equal(2); | ||
index.at(0).get('a').should.equal(125); | ||
}); | ||
}); | ||
it('should add models to customIndex', function(next) { | ||
@@ -257,2 +275,20 @@ var coll = new HashCollection(); | ||
it('should fetch from customIndex sorting by value set to customsort with offset and limit', function() { | ||
var opts = { | ||
sort: 'a', | ||
customSort: { | ||
a: 'customsort:*' | ||
}, | ||
limit: 2, | ||
offset: 1 | ||
}; | ||
var coll = new CustomIndexedHashCollection(); | ||
return coll | ||
.fetch(opts) | ||
.then(function() { | ||
coll.length.should.equal(2); | ||
coll.at(0).get('a').should.equal(125); | ||
}); | ||
}); | ||
it('should defer to customIndex with where params', function() { | ||
@@ -277,2 +313,2 @@ var opts = { | ||
}); | ||
}); | ||
}); |
353206
40
2408