maritz/nohm

Update data using SAVE() doesn't reset index

wl1711 opened this issue · 3 comments

I'm using Nohm v0.9.8 module on node.js v6.11.0 and Redis v2.7.1(Windows)

I am having trouble to update field with index

My model is:
{
"username": {
"type": "string",
"index": true,
"validations": [
"notEmpty"
]
},
"password": {
"type": "string"
},
role:{
type: 'string',
defaultValue: 'user'
},
......
"isActive": {
"type": "boolean",
"index": true, <-----
"defaultValue": true
},
....
}

I added a couple of users using this model. However when I try to update "isActive" field from false to true, the corresponding index was not removed from the set
before update:
xxxx:index:User:isActive:true {1,3}
xxxx:index:User:isActive:false {2}
after update:
xxxx:index:User:isActive:true {1,2,3}
xxxx:index:User:isActive:false {2}

And the value of isActive in xxxx:hash:User:2 is NOT updated to true. None index field like 'Role' is updated OK.

here is my update:

var updateUser = function (req, res) {
var user = nohm.factory('User');
user.id = req.params.id;
user.p(req.body);
user.save(function (err) {
if(err === 'invalid'){
console.log('properties were invalid: ', user.errors);
} else if (err){
console.log('updateUser: '+err);
} else {
res.send(user.allProperties(true));
}
});
}

Is there a workaround for this issue? I can think of one, delete it first and then save it again. But I don't really like this approach, since I will get a different id, plus I need to preserve some of data before I delete, say createdDate.

Sorry, I haven't really had the time to look into it. I'm assuming it's a bug and needs to be fixed, but can't say for sure right now.

Thanks for reporting this and huge sorry for the long delay.

This is actually a pretty big bug.

The workaround is to do user.load(req.params.id) before you do anything with it.

This is fixed in master (in v2.0.0.alpha.12 and up) so that regardless of what you do to the created instance, old indices (and uniques, they are affected by this the same way) get deleted.