nodejh / sequelize-automate

Automatically generate bare sequelize models from your database.
MIT License
116 stars 22 forks source link

Data type "double unsigned" (MySQL) throws an error #17

Closed soyniqolas closed 4 years ago

soyniqolas commented 4 years ago

Hi ! I'm trying to map a MySQL database into NodeJS models, but in one of the tables I get the next error:

(node:3291) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token, expected ";" (1:7)
    at Parser._raise (/node_modules/@babel/parser/lib/index.js:742:17)
    at Parser.raiseWithData (/node_modules/@babel/parser/lib/index.js:735:17)
    at Parser.raise (/node_modules/@babel/parser/lib/index.js:729:17)
    at Parser.unexpected (/node_modules/@babel/parser/lib/index.js:8757:16)
    at Parser.semicolon (/node_modules/@babel/parser/lib/index.js:8739:40)
    at Parser.parseExpressionStatement (/node_modules/@babel/parser/lib/index.js:11614:10)
    at Parser.parseStatementContent (/node_modules/@babel/parser/lib/index.js:11215:19)
    at Parser.parseStatement (/node_modules/@babel/parser/lib/index.js:11081:17)
    at Parser.parseBlockOrModuleBlockBody (/node_modules/@babel/parser/lib/index.js:11656:25)
    at Parser.parseBlockBody (/node_modules/@babel/parser/lib/index.js:11642:10)
    at Parser.parseTopLevel (/node_modules/@babel/parser/lib/index.js:11012:10)
    at Parser.parse (/node_modules/@babel/parser/lib/index.js:12637:10)
    at parse (/node_modules/@babel/parser/lib/index.js:12688:38)
    at /node_modules/sequelize-automate/src/generate/common/index.js:37:25
    at /node_modules/lodash/lodash.js:4905:15
    at baseForOwn (/node_modules/lodash/lodash.js:2990:24)
    at /node_modules/lodash/lodash.js:4874:18
    at Function.forEach (/node_modules/lodash/lodash.js:9342:14)
    at processFieldProperties (/node_modules/sequelize-automate/src/generate/common/index.js:33:5)
    at /node_modules/sequelize-automate/src/generate/common/index.js:116:28
    at /node_modules/lodash/lodash.js:4905:15
    at baseForOwn (/node_modules/lodash/lodash.js:2990:24)
(node:3291) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3291) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

So, I dug a bit into the source code and I discovered that when a column has a data type of "double unsigned" sequelize-automate doesn't recognize it. Example:

Current DataType: DataTypes.INTEGER(11).UNSIGNED
Current DataType: DataTypes.INTEGER(11).UNSIGNED
Current DataType: DataTypes.INTEGER(11).UNSIGNED
Current DataType: DataTypes.DATE
Current DataType: DataTypes.DATE
Current DataType: DataTypes.INTEGER(1).UNSIGNED
Current DataType: double unsigned
(node:3316) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token, expected ";" (1:7)

To fix this I just modified the attr.match to add "double" into the file /src/util/definition.js, at line 170, like this:

Before:

  if (attr.match(/^(float8|double precision|numeric)/)) {
    return 'DataTypes.DOUBLE';
  }

After:

  if (attr.match(/^(float8|double precision|double|numeric)/)) {
    return 'DataTypes.DOUBLE';
  }

After this modification I don't get that error and the mapping works fine.

soyniqolas commented 4 years ago

UPDATE: Also needs to add "UNSIGNED" if is the case.

  if (attr.match(/^(float8|double precision|double|numeric)/)) {
    let type = 'DataTypes.DOUBLE';
    const unsigned = attr.match(/unsigned/i);
    if (unsigned) {
      type += '.UNSIGNED';
    }
    return type;
  }
nodejh commented 4 years ago

Thanks! I fixed this issue. And integer, bigint, float and double support unsigned and zerofill properties, but PostgreSQL dose not.