thinky
thinky copied to clipboard
pre save hook is not called on updates
Tag.pre('save', function(next) {
this.name = this.name.toLowerCase(); //docs were wrong here, they didn't use `this`
next();
});
It works when I call Tag.save()
but not when I call .update()
var result = yield Tag.get(id).update(data).run();
So the hook save
is not called on update
for two reasons:
- I'll be honest, I think I forgot to add it.
- Because when you write:
Tag.get(id).update(data).run()
Thinky partially validates data
on the provided fields, but doesn't run the global validation (since you can have validations happening across fields) and directly send that to the server. There's no two round trips (in the normal case).
Anyway, this means that the pre-hook for save
is called with a partial document, so the hook was working for update
, this would break:
Tag.pre('save', function(next) {
this.name = this.name.toLowerCase();
next();
});
var result = yield Tag.get(id).update({foo: "bar}).run();
// The previous line throws with `cannot read toLowerCase of undefined since this.name is undefined.
The solution is probably to create a new hook update
rather than re-using the hook for save
.
+1 for update
hooks. It would be a very useful thing to have.
Hum, so this is a bit annoying and tricky to do in the case where the pre-hook is asynchronous.
+1
+1 for update hooks
+1 we need update hooks
+1 any new? I am updating the salt and encrypted password on save, but it's not possible to do it when it's updating.
Is there any solution?
@neumino Can this please be implemented? or provide a solution to update fields like "lastActive:r.now()" or "updatedAt:r.now()" otherwise I have to stick these Model data updates everywhere.
// Triggers on Model update User.pre('update', function(next) { this.updatedAt = r.now(); next(); });
Thanks for your continued effort towards thinky!