Skip to content

Refactor Data Types API #8356

Closed
Closed

Description

@mkaufmaner

Background

Currently, the API data types doesn't allow for custom change comparisons. Specifically this comes down to implementing dialect specific behavior (See #8339).

Objective

The main objective is to abstract away data type logic in the Model.set method into the data type members. This would simply the Model code.

Proposal

The data type prototype members should not reference context of the model and instead return a value. However, these members are called with the model context if the logic references other model members.

Implement

DataType.prototype._sanitize(value, options)

/**
  * Sanitizes a value before setting the model attribute when calling `Model.set`.  NOTE: This is not to be confused with the `parse` method, which parses a value from a database query.
  * 
  * @param {*}        value The attribute value being set.
  * @param {Object}   [options]
  * @return {*} Returns the sanitized value.
  */

DataType.prototype._isChanged(value, originalValue, options)

/**
  * Determines if the model attribute value has changed by comparing the current value with the original value.
  * 
  * @param {*}        value The attribute value being set.
  * @param {*}        original Value The attribute value being set.
  * @param {Object}   [options]
  * @return {Boolean} Returns true if the model attribute's value being set differs from the previous value.
  */

Tasks

  • Move setter and compare logic out of the Model class into DataType objects.
  • Write new test cases.

Dialect: any
Dialect version: any
Database version: any
Sequelize version: 4.x
Tested with master branch: N/A

Activity

  1. added
    status: in discussion
    type: feature
    type: refactor
    on Sep 22, 2017
  2. mkaufmaner commented on Sep 22, 2017

    @mkaufmaner
    ContributorAuthor
  3. sushantdhiman commented on Sep 23, 2017

    @sushantdhiman
    Contributor

    I wanted to cleanup Datatypes from some time, if we look at current code there are some other methods which needs to be refactored. I would like to align that API as well

    My thinking is we should abstract any logic that is depending on datatype to datatype based methods away from model. Refactor datatype overall. I think current get / set implementations are ok, but data comparison and private API is very scattered, we should focus on that


    DataType.prototype._toSQL(value, options)

    There are two methods in code currently
    DataType.prototype.stringify vs DataType.prototype.toSQL

    I am not sure why we need these two methods, may be @janmeier can explain , Looks like both try to get SQL representation, should be just one method.

    So we are going to need a method to convert model datatype representation to SQL, just be single one. As proposed we can call it _toSQL

    DataType.prototype._parse(result, options)

    My understanding is current parse method is a way to read SQL result from dialect and convert it to model representation. We can preserve this method but should make it private. Refactor code where we can


    For current proposed methods

    DataType.prototype.compare(value, originalValue, key, options)

    So this method will be in charge of deciding if two values have changed, may be just call it _isChanged

    I think we just need true | false return so there wont be a need for key option. Although passing options is a good idea for further configuration

    DataType.prototype.set(value)

    Current implementation allow users to set data values using this.setDataValue. How different its now from current implementation.

    Its role will be ultimately to set values, may be we should use its return value then call model.set? then we wont even need key if we implement it correctly

    Using returning data will be a breaking change (because we currently allow users to set data by this.setDataValue) (I could be wrong here)

    DataType.prototype.get(value => { })

    Current implementation pass current value where user can then decide how it should actually be returned. User are allowed to use getDataValue to read other props and return a morphed value.

    I think we should actually pass value to this method, user should then use this.getDataValue to morph incoming value in any way they want.

  4. mkaufmaner commented on Sep 23, 2017

    @mkaufmaner
    ContributorAuthor

    @sushantdhiman After further review, I don't think we should implement set and get and instead just implement _isChanged. The whole idea was to implement a standardization between dialects in data types used by Sequelize however I am pretty sure I didn't understand the code before going in.

    Going to be dropping set and get from data types and instead stick to refactoring existing code.

  5. mkaufmaner commented on Sep 23, 2017

    @mkaufmaner
    ContributorAuthor

    @sushantdhiman The .toSql() method returns the the SQL casting for the value.

  6. mkaufmaner commented on Sep 23, 2017

    @mkaufmaner
    ContributorAuthor

    @sushantdhiman After further examination, the real problem is that we only partially implemented sanitation when calling Model.set and never extended the data types to support this behavior. In fact, we modify the value being set while checking for changes. See https://github.com/sequelize/sequelize/blob/master/lib/model.js#L3303-L3320

    In order to continue supporting this behavior, I am going to implement a DataType.prototype._sanitize method.

  7. self-assigned this
    on Sep 24, 2017
  8. papb commented on Jul 26, 2019

    @papb
    Member

    @mkaufmaner What is the status of this?

  9. added
    status: awaiting response
    and removed
    status: in discussion
    on Jul 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

status: awaiting responsetype: featuretype: refactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions