backbone-db-redis
Advanced tools
Comparing version 0.0.39 to 0.0.40
var _ = require('lodash'); | ||
var debug = require('debug')('backbone-db-redis:query'); | ||
var utils = require('./utils'); | ||
var async = require('async'); | ||
@@ -51,16 +52,16 @@ function objToJSON(data) { | ||
var self = this; | ||
this.multi = this.db.redis.multi(); | ||
var fetchFn; | ||
if (this.filterOptions.where) { | ||
this.filterModels(); | ||
fetchFn = this.filterModels.bind(this); | ||
} else { | ||
this.fetchAll(); | ||
fetchFn = this.fetchAll.bind(this); | ||
} | ||
if (this.destinationSet) this.multi.del(this.destinationSet); | ||
if (this.ids) { | ||
return this.searchByIds(this.ids, callback); | ||
} | ||
debug('multi query:', this.multi.queue); | ||
this.multi.exec(function(err, results) { | ||
fetchFn(function(err, results) { | ||
debug('results:', results); | ||
if (self.destinationSet) self.db.redis.del(self.destinationSet); | ||
if (self.ids) { | ||
return self.searchByIds(self.ids, callback); | ||
} | ||
var resultError = _.find(results, function(res) { | ||
@@ -72,3 +73,3 @@ return res.toString().indexOf('ERR') > -1; | ||
} | ||
var ids = results[self.idQueryNr]; | ||
var ids = results; | ||
self.searchByIds(ids, callback); | ||
@@ -78,3 +79,3 @@ }); | ||
filterModels: function() { | ||
filterModels: function(cb) { | ||
var self = this; | ||
@@ -105,3 +106,3 @@ this.requiredIndexes = []; | ||
// no need to query, since we know ids already | ||
return; | ||
return cb(null, null); | ||
} | ||
@@ -131,15 +132,14 @@ | ||
query.unshift(this.destinationSet); | ||
this.multi.zinterstore(query); | ||
this.db.redis.zinterstore.apply(this.db.redis, query); | ||
if (this.filterOptions.customSort) { | ||
this._customSort(this.destinationSet, this.filterOptions.customSort); | ||
this._customSort(this.destinationSet, this.filterOptions.customSort, cb); | ||
} else if (!canUseIndexForSorting && self.dbOptions.ModelClass.prototype.redis_type === 'hash') { | ||
this._sortUsingHash(this.destinationSet); | ||
this._sortUsingHash(this.destinationSet, {}, cb); | ||
} else { | ||
this._fetchSorted(this.destinationSet); | ||
this._fetchSorted(this.destinationSet, cb); | ||
} | ||
} else { | ||
this.idQueryNr = 0; | ||
this.multi.smembers(query[0]); | ||
this.db.redis.smembers(query[0], cb); | ||
} | ||
@@ -150,9 +150,8 @@ | ||
fetchAll: function() { | ||
if (!this.idQueryNr) this.idQueryNr = 0; | ||
fetchAll: function(cb) { | ||
if (!this.filterOptions.sort) { | ||
var collectionKey = this.dbOptions.collectionKey; | ||
this.multi.sort(collectionKey, 'BY', 'nosort'); | ||
this.db.redis.sort(collectionKey, 'BY', 'nosort', cb); | ||
} else { | ||
this._fetchSorted(); | ||
this._fetchSorted(null, cb); | ||
} | ||
@@ -197,12 +196,7 @@ }, | ||
mgetHashes: function(keys, callback) { | ||
var multi = this.db.redis.multi(); | ||
_.each(keys, function(key) { | ||
multi.hgetall(key); | ||
}); | ||
multi.exec(function(err, results) { | ||
callback(err, results); | ||
}); | ||
var redis = this.db.redis; | ||
async.map(keys, redis.hgetall.bind(redis), callback); | ||
}, | ||
_customSort: function(set, customSort) { | ||
_customSort: function(set, customSort, cb) { | ||
var parsedSort = utils.parseSort(this.filterOptions.sort); | ||
@@ -215,6 +209,6 @@ var sortParams = this.db.key(customSort[parsedSort.sortProp]); | ||
debug('customSort', params); | ||
this.multi.sort(params); | ||
this.db.redis.sort(params, cb); | ||
}, | ||
_sortUsingHash: function(set, options) { | ||
_sortUsingHash: function(set, options, cb) { | ||
var m = new this.dbOptions.ModelClass(); | ||
@@ -228,6 +222,6 @@ var idKey = this.db.getIdKey(m); | ||
} | ||
this.multi.sort(params); | ||
this.db.redis.sort(params, cb); | ||
}, | ||
_fetchSorted: function(set) { | ||
_fetchSorted: function(set, cb) { | ||
var self = this; | ||
@@ -247,5 +241,5 @@ var sortOpts = this._getSortSetKeyAndOrder(); | ||
if (sortOpts.sortOrder === undefined || sortOpts.sortOrder === 1) { | ||
this.multi.zrange(set, start, end); | ||
this.db.redis.zrange(set, start, end, cb); | ||
} else { | ||
this.multi.zrevrange(set, 0, -1); | ||
this.db.redis.zrevrange(set, 0, -1, cb); | ||
} | ||
@@ -268,2 +262,2 @@ }, | ||
query.execute(callback); | ||
}; | ||
}; |
{ | ||
"name": "backbone-db-redis", | ||
"version": "0.0.39", | ||
"version": "0.0.40", | ||
"description": "Redis driver for Backbone.Db", | ||
@@ -19,3 +19,4 @@ "main": "index.js", | ||
"when": "~3.1", | ||
"backbone-db": "~0.4" | ||
"backbone-db": "~0.4", | ||
"async": "^0.9.0" | ||
}, | ||
@@ -22,0 +23,0 @@ "devDependencies": { |
26
66536
8
1965
+ Addedasync@^0.9.0
+ Addedasync@0.9.2(transitive)