Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): handle Numbers passed as Objects #10492

Merged
merged 1 commit into from Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Expand Up @@ -32,6 +32,7 @@
"new-cap": [
"error",
{
"capIsNewExceptionPattern": "^BigInt",
"properties": false
}
],
Expand Down
11 changes: 11 additions & 0 deletions lib/data-types.js
Expand Up @@ -214,6 +214,17 @@ NUMBER.prototype.validate = function(value) {

return true;
};
NUMBER.prototype._stringify = function _stringify(number) {
if (typeof number === 'number' || number === null || number === undefined) {
return number;
}

if (typeof number.toString === 'function') {
return number.toString();
}

return number;
};
Object.defineProperty(NUMBER.prototype, 'UNSIGNED', {
get() {
this._unsigned = true;
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -53,6 +53,7 @@
"@types/bluebird": "^3.5.26",
"@types/node": "^10.12.27",
"@types/validator": "^10.9.0",
"big-integer": "^1.6.42",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"chai": "^4.x",
"chai-as-promised": "^7.x",
"chai-datetime": "^1.x",
Expand Down
21 changes: 21 additions & 0 deletions test/integration/data-types.test.js
Expand Up @@ -12,6 +12,7 @@ const chai = require('chai'),
uuid = require('uuid'),
DataTypes = require('../../lib/data-types'),
dialect = Support.getTestDialect(),
BigInt = require('big-integer'),
semver = require('semver');

describe(Support.getTestDialectTeaser('DataTypes'), () => {
Expand Down Expand Up @@ -227,6 +228,26 @@ describe(Support.getTestDialectTeaser('DataTypes'), () => {
}
});

it('should handle JS BigInt type', function() {
const User = this.sequelize.define('user', {
age: Sequelize.BIGINT
});

const age = BigInt(Number.MAX_SAFE_INTEGER).add(Number.MAX_SAFE_INTEGER);

return User.sync({ force: true }).then(() => {
return User.create({ age });
}).then(user => {
expect(BigInt(user.age).toString()).to.equal(age.toString());
return User.findAll({
where: { age }
});
}).then(users => {
expect(users).to.have.lengthOf(1);
expect(BigInt(users[0].age).toString()).to.equal(age.toString());
});
});

it('calls parse and bindParam for DOUBLE', () => {
const Type = new Sequelize.DOUBLE();

Expand Down