Open felipou opened 9 years ago
Please have a look at https://github.com/dresende/node-orm2/issues/517 Some documentation is missing here.
Great, thanks for the reference. Although it's a better solution than my workaround, shouldn't it be possible to come up with a more seamless solution? One that doesn't need a special function to update the attribute value?
I mean, something that warns the managed instance that its object property was modified. I'm thinking of creating a generic package that offers managed objects that could solve this type of problem. I'm gonna give it a try and I'll post here when I have something.
what we need is Object.observe, which is not available everywhere i think. Or is it ?
You're right, Object.observe would do it. But it will only be available on version 0.12 :(
For others looking for a quick fix, the copy idea suggested by @felipou worked well. Our character model was storing an array in it's skills attribute (an Object type). We added character.skills = character.skills.slice();
just before character.save();
Thanks for the help m8!
@kapouer, I think using set()
is a workaround to the underlying issue. Things like array push() won't work with #517 . The fix should work on existing functions on objects regardless of their type.
I think a hash should somehow be leveraged to determine uniqueness of objects. For example, compute a hash after you fetch the object, re-compute the hash and compare before you save it. The frequency of two consecutive hashes clashing is pretty much 0. It may add overhead, but it'll save time on redundant SQL queries or having to preform a slice()
.
Attributes (columns) with type Object (encoded as JSON) can have their own attributes updated and node-orm won't detect the changes, and therefore won't update the database when save is called.
I can't think of an easy fix, from what I saw in the source code. :/ But I think this should be properly documented.
My work around is to copy the object value, update it, and assign it again with the new object.