Repository navigation
Description
Currently, Sequelize holds all attributes inside dataValues. You can use them on the instance, but it is all one big lie as on instance creation, sequelize simply defines getters and setters that proxy to dataValues and mark the attribute as dirty. The problem is though that setters don't catch the following changes to attributes:
- When using a JSON column, you can alter the object
- When using an ARRAY column, you can push new items, remove item etc ARRAY Datatype #6197
- When using a DATE column, you can call
setDate()etc on the object
You have to manually call instance.changed('myAttribute', true) (no autocomplete for the attribute name) or reassign with instance.myAttribute = instance.myAttribute (wtf, should be a noop).
Now think about how a Model is defined in Sequelize 4.0 and ES6 / TS:
class User extends Sequelize.Model {
public username: string;
public roles: string[];
public createdAt: Date;
}You define your class with these attributes, but in reality, they are not there - it's all a big lie. You look at the debugger and they are not there:
I think it would be a lot better if instead of dataValues, there was only _previousDataValues. When an instance is retrieved from the DB, the actual instance gets all attributes set as real attributes. _previousDataValues gets set to a deep copy of the attributes - that means every Date object, every JSON object, every Array gets deeply cloned. This does not include aggregate columns, it's only about the values of the database row.
A user can then modify and use the instance as much as he likes, nothing goes through any setters or getters (except if he defined them of course).
Only upon a call to save() we deeply compare the instance once to the _previousDataValues. That includes JSON objects and arrays. We can then generate a minimal query that only contains the exact changes that were made to the instance.
It doesn't even have to replace a whole array column, since we have an exact diff we can even use advanced array functions to only remove / insert what changed.
Performance
saving would be a bit more complex because we have to compare the objects, but every userland function containing property access (get/set) will be faster because getters and setters are an optimization killer.

Activity
mickhansen commentedon Jul 17, 2016
This will break all computes values that aren't targeted virtuals.
Virtuals don't make sense in all cases like doing a window function for a full count.
mickhansen commentedon Jul 17, 2016
Doesn't actually exist in Sequelize 4.0 / ES6 yet
felixfbecker commentedon Jul 17, 2016
@mickhansen
we discussed this on Slack already, how exactly will it break? Those can simply be set on the instance, but for
save()we only look at the known attributes.huh? this is exactly how it works in master... The properties can of course only be defined if you're using TypeScript. But my point were the semantics behind it (you expect these properties to exist on the instance).
mickhansen commentedon Jul 17, 2016
@felixfbecker
get()will break unless we simply enumerate overthis(which i have no idea the performance impact of vs just returningdataValues.They are defined on an object on the class, but i suppose it's close to the same.
felixfbecker commentedon Jul 17, 2016
@mickhansen
get()can indeed simply return the properties that are known attributes, likeHonestly I think the use case of manipulating arrays, date or JSON objects is much bigger than using
get(), since you can also just get the properties from the instance directly. We have a ~50k LOC codebase and don't useget()at all.mickhansen commentedon Jul 17, 2016
@felixfbecker You never use
toJSONorget? That's rare, a lot of people use Sequelize in JSON APIs where those two methods will be used implicitely. Read performance is important and a lot of people optimize for that, whygetwas written as it is.But manipulating arrays doesn't really have anything to do with how get works. You're advocating for deep compare on save rather than on set, but that doesn't strictly have anything to do with how the values are stored.
On-write diffs was introduced for better perf, but it introduces too many oddities so i agree it should be changed to deep diff on save.
mickhansen commentedon Jul 17, 2016
this won't work with aggregates or computed values that aren't VIRTUALs
felixfbecker commentedon Jul 17, 2016
I use
toJSONimplicitely, yes, by callingJSON.stringify(). But we don't strictly need a method for that if the properties sit directly on the instance. We could have sequelize internal methods / properties defined as non-enumerable properties or even use symbols.I was under the impression that
get()returned only columns in the database, but my example was just pseudo-code,get()/toJSONcould also just return everything except the sequelize internal properties. Or look at theattributesoption to see exactly what the user included.Well, when we do diff on save, we just don't need the setters on the instance anymore. Setters have quite a performance impact too. They would just be a useless proxy to
dataValues, so I advocate to remove the extra layer and put the properties directly on the instance as you would expect them to be.mickhansen commentedon Jul 17, 2016
It returns dataValues, but with
plainit also iterates over them so you get values back and not instances when you have includes.Could work, requires a loop on each
get, but so does most cases ofgetcurrently.setalso handles the logic for building nested instances, which are used in a few scenarios. Could be solved in other ways though.I'm not at all against this, i just see a ton of edge cases that might end up broken with this - But it's worth experimenting with.
sushantdhiman commentedon Jul 18, 2016
@felixfbecker Which tool or IDE you used to render this
felixfbecker commentedon Jul 18, 2016
@sushantdhiman The integrated Node debugger of Visual Studio Code