Skip to content

fix(DataTypes/DATE): Adds support for DATE with Infinity/-Infinity - #8339

Closed
mkaufmaner wants to merge 6 commits into
sequelize:masterfrom
mkaufmaner:8336-infinity-date-support
Closed

mkaufmaner wants to merge 6 commits into
sequelize:masterfrom
mkaufmaner:8336-infinity-date-support

Conversation

@mkaufmaner

@mkaufmaner mkaufmaner commented Sep 19, 2017 •

Copy link
Copy Markdown
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Adds support for Infinity/-Infinity with for DATE data types.

Closes #8336

@mention-bot

Copy link
Copy Markdown

@mkaufmaner, thanks for your PR! By analyzing the history of the files in this pull request, we identified @felixfbecker, @janmeier and @mickhansen to be potential reviewers.

@codecov

codecov Bot commented Sep 19, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #8339 into master will decrease coverage by 8.05%.
The diff coverage is 53.33%.

@mkaufmaner mkaufmaner added the wip label Sep 20, 2017
@mkaufmaner

Copy link
Copy Markdown
Contributor Author

Marked as WIP because I am going to be working on the test coverage.

@mkaufmaner mkaufmaner removed the wip label Sep 20, 2017
Comment thread lib/model.js Outdated
}
} else { // go ahread and parse as Date if required
if (!(value instanceof Date)) {
if (!(value instanceof Date) && value !== Infinity && value !== -Infinity) {

@sushantdhiman sushantdhiman Sep 20, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much logic here which should be handled by datatype and is postgres specific.

cc @janmeier Should we get dialect date type and call specific method to parse value ? This way postgres can do its thing and other dialect can do theirs ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, a parse method on the date type could be a good idea

@mkaufmaner mkaufmaner Sep 20, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe custom getter and setter for the DATE.prototype?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could work :)

@mkaufmaner

Copy link
Copy Markdown
Contributor Author

I was looking into parse methods on the data type and will make changes accordingly. Thanks for the quick response!

@mkaufmaner

mkaufmaner commented Sep 21, 2017 •

Copy link
Copy Markdown
Contributor Author

@janmeier @sushantdhiman After reviewing the code for Model.get and Model.set maybe a good idea would be to extend getters and setters into the DataType members. The visibility of these changes would be private. The DataType getter and setter would be called before user defined getter and setter methods (Getters-Setters). This will allow us to support dialect specific functionality for abstract data types.

For example, in order to support Infinity and -Infinity values for DataType.DATE attributes in PostgreSQL, we would add the following code to /lib/dialects/postgres/data-types.js;

DATE.prototype.set = function set(value, key) {
	if (!(value instanceof Date) && value !== Infinity && value !== -Infinity) {
		value = new Date(value);
	}

	this.setDataValue(key, value);
};

@mkaufmaner

mkaufmaner commented Sep 21, 2017 •

Copy link
Copy Markdown
Contributor Author

Maybe we can go as far as abstracting the changed logic into the DataType as well;

DATE.prototype.comparator = function comparator(value, originalValue) {
	if (originalValue) {
		if (!(originalValue instanceof Date) && value !== Infinity && value !== -Infinity) {
			originalValue = new Date(originalValue);
		}
		if (value === originalValue || value instanceof Date && originalValue instanceof Date && value.getTime() === originalValue.getTime()) {
			return true;
		}
	}
};

Thoughts?

@mkaufmaner mkaufmaner added the wip label Sep 22, 2017
@sushantdhiman

Copy link
Copy Markdown
Contributor

I see you are allowing users to set their own getter / setter for types, good idea but I think they should be able to call super.get() / super.set() perhaps.

we should lock in an API about what we really want so we can improve other types and reduce code from model file as well. Please open an issue and we can discuss over there.

First proposal could be to include a way in datatypes to set, get or compare. Let see what we can refactor

@janmeier janmeier left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good approach, it will be cleaner to move some if this code to datatypes

Comment thread lib/model.js
this._virtualAttributes.push(name);
} else if (definition.type instanceof DataTypes.GEOMETRY) {
this._geometryAttributes.push(name);
if (definition.type) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case would the definition not have a type?

}).then(user => {
expect(user.username).to.equal('bob');
expect(user.beforeTime).to.equal(-Infinity);
expect(user.sometime).to.not.equal(Infinity);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to.be.closeTo(now)

@mkaufmaner

Copy link
Copy Markdown
Contributor Author

@sushantdhiman I will work on a quick proposal with some examples as to what we should be aiming for when it comes to implementing a new API for data types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants