MokoJs / moko

Generator powered models
51 stars 3 forks source link

Complex objects as attrs don't get marked dirty #9

Open rschmukler opened 10 years ago

rschmukler commented 10 years ago

when setting nested properties or mutating arrays

paglias commented 10 years ago

For arrays probably the best solution is:

  1. Include a method to manually set a field as dirty like mongoose's markModified
  2. Proxy all the mutation methods on array, creating a custom class with methods like push, pop , ... that updates a stored private array and uses the necessary MongoDB operation http://docs.mongodb.org/manual/reference/operator/update-array/

This is how mongoose does it https://github.com/LearnBoost/mongoose/blob/3.8.x/lib/types/array.js#L27

For nested properties I suppose that when there is a defined set of sub properties in the schema like:

Model.attr('obj', {
    sub1: Date,
    sub2: String
})

(Or another syntax given that the second argument can be used for passing options.) And then transform each sub property using a getter and a setter, for objects without a defined set of sub property I think that the only thing that you can do is using a method like markModified to be used manually.