Skip to content

Commit

Permalink
feat(datatypes): handle numbers passed as objects for bigint (#10496)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabegorelick authored and sushantdhiman committed Mar 3, 2019
1 parent 00e4984 commit de39cff
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Expand Up @@ -14,6 +14,7 @@
"new-cap": [
"warn",
{
"capIsNewExceptionPattern": "^BigInt",
"properties": false
}
],
Expand Down
10 changes: 10 additions & 0 deletions lib/data-types.js
Expand Up @@ -146,7 +146,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 @@ -55,6 +55,7 @@
"wkx": "^0.4.1"
},
"devDependencies": {
"big-integer": "^1.6.42",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"chai-datetime": "^1.5.0",
Expand Down
21 changes: 21 additions & 0 deletions test/integration/data-types.test.js
Expand Up @@ -11,6 +11,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 @@ -207,6 +208,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 stringify for DOUBLE', () => {
const Type = new Sequelize.DOUBLE();

Expand Down

0 comments on commit de39cff

Please sign in to comment.