From 183646a9deb44d8ff861dc9849f9fa3fd3b214ab Mon Sep 17 00:00:00 2001 From: Gabe Gorelick Date: Fri, 1 Mar 2019 19:19:30 -0600 Subject: [PATCH] feat(types): handle Numbers passed as Objects This adds support for Number types that aren't represented as actual Javascript Number instances. For example, BigInt (new in Node 10), Decimal.js, etc. Fixes #10491 --- .eslintrc.json | 1 + lib/data-types.js | 11 +++++++++++ package.json | 1 + test/integration/data-types.test.js | 21 +++++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/.eslintrc.json b/.eslintrc.json index 606c54271d9d..c4d54035b9de 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -32,6 +32,7 @@ "new-cap": [ "error", { + "capIsNewExceptionPattern": "^BigInt", "properties": false } ], diff --git a/lib/data-types.js b/lib/data-types.js index 5a8e798666c0..34fdb1ef53ca 100644 --- a/lib/data-types.js +++ b/lib/data-types.js @@ -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; diff --git a/package.json b/package.json index 45a5b02b3fef..314ca5d52f85 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "@types/bluebird": "^3.5.26", "@types/node": "^10.12.27", "@types/validator": "^10.9.0", + "big-integer": "^1.6.42", "chai": "^4.x", "chai-as-promised": "^7.x", "chai-datetime": "^1.x", diff --git a/test/integration/data-types.test.js b/test/integration/data-types.test.js index c1c357391baf..d2c0188fd2b3 100644 --- a/test/integration/data-types.test.js +++ b/test/integration/data-types.test.js @@ -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'), () => { @@ -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();