Repository navigation
Closed
Labels
status: awaiting responseFor issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no actiontype: featureDEPRECATED: replace with the "feature" issue typetype: refactorDEPRECATED: replace with the "meta" issue type
Description
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
Reacted by Alyx
Metadata
Metadata
Assignees
Labels
status: awaiting responseFor issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no actiontype: featureDEPRECATED: replace with the "feature" issue typetype: refactorDEPRECATED: replace with the "meta" issue type
Activity
mkaufmaner commentedon Sep 22, 2017
@sushantdhiman @janmeier @mickhansen ^^
sushantdhiman commentedon Sep 23, 2017
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 / setimplementations are ok, but data comparison and private API is very scattered, we should focus on thatDataType.prototype._toSQL(value, options)There are two methods in code currently
DataType.prototype.stringifyvsDataType.prototype.toSQLI 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
_toSQLDataType.prototype._parse(result, options)My understanding is current
parsemethod 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 canFor 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
_isChangedI think we just need
true | falsereturn so there wont be a need forkeyoption. Although passingoptionsis a good idea for further configurationDataType.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 correctlyUsing 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
getDataValueto read other props and return a morphed value.I think we should actually pass
valueto this method, user should then usethis.getDataValueto morph incoming value in any way they want.mkaufmaner commentedon Sep 23, 2017
@sushantdhiman After further review, I don't think we should implement
setandgetand 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.
mkaufmaner commentedon Sep 23, 2017
@sushantdhiman The
.toSql()method returns the the SQL casting for the value.mkaufmaner commentedon Sep 23, 2017
@sushantdhiman After further examination, the real problem is that we only partially implemented sanitation when calling
Model.setand 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-L3320In order to continue supporting this behavior, I am going to implement a
DataType.prototype._sanitizemethod.papb commentedon Jul 26, 2019
@mkaufmaner What is the status of this?