Skip to content

Diff against a clone of attributes instead of using setters #6270

Closed

Description

@felixfbecker

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:

image

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

  1. mickhansen commented on Jul 17, 2016

    @mickhansen
    Contributor

    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.

  2. mickhansen commented on Jul 17, 2016

    @mickhansen
    Contributor
    class User extends Sequelize.Model {
      public username: string;
      public roles: string[];
      public createdAt: Date;
    }

    Doesn't actually exist in Sequelize 4.0 / ES6 yet

  3. felixfbecker commented on Jul 17, 2016

    @felixfbecker
    ContributorAuthor

    @mickhansen

    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.

    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.

    Doesn't actually exist in Sequelize 4.0 / ES6 yet

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

  4. mickhansen commented on Jul 17, 2016

    @mickhansen
    Contributor

    @felixfbecker

    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.

    get() will break unless we simply enumerate over this (which i have no idea the performance impact of vs just returning dataValues.

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

    They are defined on an object on the class, but i suppose it's close to the same.

  5. felixfbecker commented on Jul 17, 2016

    @felixfbecker
    ContributorAuthor

    @mickhansen get() can indeed simply return the properties that are known attributes, like

    get() {
      return _.pick(this, Object.keys(this.constructor.attributes));
    }

    Honestly 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 use get() at all.

  6. mickhansen commented on Jul 17, 2016

    @mickhansen
    Contributor

    @felixfbecker You never use toJSON or get? 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, why get was 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.

  7. mickhansen commented on Jul 17, 2016

    @mickhansen
    Contributor
    get() {
      return _.pick(this, Object.keys(this.constructor.attributes));
    }
    

    this won't work with aggregates or computed values that aren't VIRTUALs

  8. felixfbecker commented on Jul 17, 2016

    @felixfbecker
    ContributorAuthor

    I use toJSON implicitely, yes, by calling JSON.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() / toJSON could also just return everything except the sequelize internal properties. Or look at the attributes option to see exactly what the user included.

    get() {
      return _.omit(this, ['isNewRecord', 'hasPrimaryKeys', '_previousDataValues']);
    }

    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.

    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.

  9. mickhansen commented on Jul 17, 2016

    @mickhansen
    Contributor

    I was under the impression that get() returned only columns in the database

    It returns dataValues, but with plain it also iterates over them so you get values back and not instances when you have includes.

    get() {
      return _.omit(this, ['isNewRecord', 'hasPrimaryKeys', '_previousDataValues']);
    }

    Could work, requires a loop on each get, but so does most cases of get currently.

    Well, when we do diff on save, we just don't need the setters on the instance anymore

    set also 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.

  10. sushantdhiman commented on Jul 18, 2016

    @sushantdhiman
    Contributor

    @felixfbecker Which tool or IDE you used to render this

  11. felixfbecker commented on Jul 18, 2016

    @felixfbecker
    ContributorAuthor

    @sushantdhiman The integrated Node debugger of Visual Studio Code

  12. self-assigned this
    on Aug 21, 2016
  13. added this to the 4.0 milestone on Oct 19, 2016
  14. modified the milestone: 4.0 on Nov 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions