dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

Changes in Object type attributes are not detected #592

Open felipou opened 9 years ago

felipou commented 9 years ago

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.

kapouer commented 9 years ago

Please have a look at https://github.com/dresende/node-orm2/issues/517 Some documentation is missing here.

felipou commented 9 years ago

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.

kapouer commented 9 years ago

what we need is Object.observe, which is not available everywhere i think. Or is it ?

felipou commented 9 years ago

You're right, Object.observe would do it. But it will only be available on version 0.12 :(

fydo23 commented 8 years ago

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!

fydo23 commented 8 years ago

@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().