diff --git a/lib/abstract-class.js b/lib/abstract-class.js index 9c7aa885..db0579b0 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -241,10 +241,10 @@ AbstractClass.prototype.propertyChanged = function (name) { }; AbstractClass.prototype.toObject = function (onlySchema) { - // blind faith: we only enumerate properties var data = {}; var ds = this.constructor.schema.definitions[this.constructor.modelName]; var properties = ds.properties; + // weird Object.keys(onlySchema ? properties : this).concat(['id']).forEach(function (property) { data[property] = this[property]; }.bind(this)); diff --git a/lib/adapters/redis.js b/lib/adapters/redis.js index c475b54b..09a60244 100644 --- a/lib/adapters/redis.js +++ b/lib/adapters/redis.js @@ -40,11 +40,11 @@ BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) { BridgeToRedis.prototype.save = function (model, data, callback) { this.client.hmset(model + ':' + data.id, data, function (err) { if (err) return callback(err); - this.updateIndexes(model, data, callback); + this.updateIndexes(model, data.id, data, callback); }.bind(this)); }; -BridgeToRedis.prototype.updateIndexes = function (model, data, callback) { +BridgeToRedis.prototype.updateIndexes = function (model, id, data, callback) { var i = this.indexes[model]; var schedule = []; Object.keys(data).forEach(function (key) { @@ -52,7 +52,7 @@ BridgeToRedis.prototype.updateIndexes = function (model, data, callback) { schedule.push([ 'sadd', 'i:' + model + ':' + key + ':' + data[key], - model + ':' + data.id + model + ':' + id ]); } }.bind(this)); @@ -107,7 +107,7 @@ BridgeToRedis.prototype.possibleIndexes = function (model, filter) { var foundIndex = []; Object.keys(filter).forEach(function (key) { - if (this.indexes[model][key]) { + if (this.indexes[model][key] && typeof filter[key] === 'string') { foundIndex.push('i:' + model + ':' + key + ':' + filter[key]); } }.bind(this)); @@ -186,6 +186,8 @@ BridgeToRedis.prototype.count = function count(model, callback) { }; BridgeToRedis.prototype.updateAttributes = function updateAttrs(model, id, data, cb) { - this.client.hmset(model + ':' + id, data, cb); + this.client.hmset(model + ':' + id, data, function () { + this.updateIndexes(model, id, data, cb); + }.bind(this)); };