Closed kapouer closed 10 years ago
This problem is harder to solve than it seems. I tried the above as well as a few other things, and nothing worked.
var Place = db.define('place', {
position: { type: 'object' }
});
...
Place.create({ position: { x: 2, y: 3 } }, ...);
Place.get(1, function (err, item) {
console.log(item.saved()); // "true"
item.position.x = 5;
console.log(item.saved()); // "true"
})
.save()
will only persist to the database if saved()
returns false.
Unfortunately, in the above example when we set x to 5, the code on line 453 isn't called.
And even if it was (eg, by doing p = item.position; item.position = null; item.position = p
) the comparison still wouldn't return true because value
points to the same object in memory as opts.data[key]
so we'd be comparing it to itself.
The solutions I can see are:
save()
item.save({ force: true }, ...)
and maybe a global setting for thisitem.markAsDirty('position')
Number 1 goes against the principle of least surprise. Number 2 would result in extra queries, negatively impacting speed. Number 3 would double memory usage - problematic if a property is storing a Buffer() of a large file. Number 4 & 5 is rather manual.
I'm leaning towards number 4 and/or 5 but it's not exactly a nice solution..
That sounds better than my ideas.
We now have:
item.set('position.x', 2)
item.set(['position','x'], 2) // in case you have objects like { "a.b" : 4 }
item.markAsDirty('position')
set()
will only mark the object as dirty if you are changing the value. If x
was 2
and you set it to 2
, it won't affect dirty state.
Published as version 2.1.15.
that's wonderful, thank you
The only missing thing now is a mention of that in the docs...
Hi, the comparison
if (opts.data[key] !== value) {
at https://github.com/dresende/node-orm2/blob/38e76722/lib/Instance.js#L453 will never be true when modifying a property of a column that is an object. The horrible workaround is:i suggest to improve the comparison when the value is an object:
so a thorough comparison is done in case of equality of objects.