feathers-plus / generator-feathers-plus

A Yeoman generator to (re)generate a FeathersJS application supporting both REST and GraphQL architectural concepts and their query languages.

Home Page:https://generator.feathers-plus.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invalid sequelize ts model definitions

grbspltt opened this issue · comments

Steps to reproduce
Generate app, then generate sequelize service

Expected behavior
Expect model.ts to properly call sequelize ts type functions.

Actual behavior
In ./src/services/serviceName.ts does not use the DataTypes namespace for data type checking. The error prevents app from starting.

// tslint:disable-next-line:no-unused-variable
const DataTypes = (Sequelize as any).DataTypes as Sequelize.DataTypes; -- <-- unused variable

let moduleExports = merge({},
  // !<DEFAULT> code: sequelize_model
  {
    id: {
      type: function (length) {
    var options = typeof length === 'object' && length || {
      length: length
    };
    if (!(this instanceof INTEGER)) return new INTEGER(options); // <-- error at INTEGER, should be DataTypes.INTEGER
    NUMBER.call(this, options);
  },

Should be:

  if (!(this instanceof DataTypes.INTEGER)) return new DataTypes.INTEGER(options); 
    DataTypes.NUMBER.call(this, options);

System configuration
Tell us about the applicable parts of your setup.

{
"options": {
"ver": "1.0.0",
"inspectConflicts": false,
"semicolons": true,
"freeze": [],
"ts": true
},
"app": {
"environmentsAllowingSeedData": "test-dev",
"seedData": true,
"name": "test
"description": "test feathers project",
"src": "src",
"packager": "npm@>= 3.0.0",
"providers": [
"rest",
"socketio"
]
},
"services": {
"users": {
"name": "users",
"nameSingular": "user",
"subFolder": "",
"fileName": "users",
"adapter": "sequelize",
"path": "/users",
"isAuthEntity": true,
"requiresAuth": true,
"graphql": true
},
"roles": {
"name": "roles",
"nameSingular": "role",
"subFolder": "",
"fileName": "roles",
"adapter": "sequelize",
"path": "/roles",
"isAuthEntity": false,
"requiresAuth": true,
"graphql": true
},
"menu": {
"name": "menu",
"nameSingular": "menu",
"subFolder": "",
"fileName": "menu",
"adapter": "sequelize",
"path": "/menu",
"isAuthEntity": false,
"requiresAuth": true,
"graphql": true
}
},
"hooks": {},
"authentication": {
"strategies": [
"local"
],
"entity": "users"
},
"connections": {
"sequelize": {
"database": "mssql",
"adapter": "sequelize",
"connectionString": "mssql://test:test@localhost:1433/test"
}
}
}

Module versions (especially the part that's not working):
feathers-plus version 0.7.76

{
"dependencies": {
"@feathers-plus/test-utils": "^0.3.5",
"@feathersjs/authentication": "^2.1.15",
"@feathersjs/authentication-jwt": "^2.0.9",
"@feathersjs/authentication-local": "^1.2.9",
"@feathersjs/configuration": "^2.0.6",
"@feathersjs/errors": "^3.3.6",
"@feathersjs/express": "^1.3.1",
"@feathersjs/feathers": "^3.3.1",
"@feathersjs/socketio": "^3.2.9",
"ajv": "^5.5.2",
"compression": "^1.7.3",
"cors": "^2.8.5",
"cross-env": "^5.2.0",
"feathers-hooks-common": "^4.20.2",
"feathers-sequelize": "^3.1.3",
"helmet": "^3.15.0",
"lodash.merge": "^4.6.1",
"mssql": "^4.3.0",
"sequelize": "^4.42.0",
"serve-favicon": "^2.5.0",
"winston": "^3.1.0"
},
"devDependencies": {
"@types/compression": "0.0.36",
"@types/cors": "^2.8.4",
"@types/feathersjs__authentication": "^2.1.2",
"@types/feathersjs__authentication-jwt": "^1.0.4",
"@types/feathersjs__authentication-local": "^1.0.3",
"@types/feathersjs__configuration": "^1.0.1",
"@types/feathersjs__errors": "^3.2.1",
"@types/feathersjs__express": "^1.1.4",
"@types/feathersjs__feathers": "^3.1.1",
"@types/feathersjs__socketio": "^3.0.3",
"@types/helmet": "0.0.40",
"@types/lodash.merge": "^4.6.4",
"@types/mocha": "^5.2.5",
"@types/request-promise": "^4.1.42",
"@types/sequelize": "^4.27.34",
"@types/serve-favicon": "^2.2.30",
"@types/winston": "^2.4.4",
"mocha": "^5.2.0",
"nodemon": "^1.18.9",
"request": "^2.88.0",
"request-promise": "^4.2.2",
"ts-mocha": "^2.0.0",
"ts-node": "^7.0.1",
"tslint": "^5.12.1",
"typescript": "^3.2.4"
}
}

NodeJS version:
8.15.0
Operating System:
Windows 10.0.17134.523

The Sequelize models are created using https://github.com/ronalddddd/sequelizer so we have to define INTERGER, et al.

Please add let INTEGER = DataType.INTEGER , etc. within // !code: imports // !end and // !code: init // !end for now. The generator will generate these shortly, at which point you may have to remove your newly added custom code.

Please post your serviceName.schema.?s and serviceName.sequelize.?s modules.

The if statement seems to be coming from serializing Sequelize.INTEGER.

I created a Sequelize service with your JSON-schema on Ubuntu and obtained a correct serviceName.sequelize.?s module:

/* eslint quotes: 0 */
// Defines Sequelize model for service `xxx`. (Can be re-generated.)
const merge = require('lodash.merge');
const Sequelize = require('sequelize');
// eslint-disable-next-line no-unused-vars
const DataTypes = Sequelize.DataTypes;
// !code: imports // !end
// !code: init // !end

let moduleExports = merge({},
  // !<DEFAULT> code: sequelize_model
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true
    },
    email: {
      type: DataTypes.TEXT
    },
    firstName: {
      type: DataTypes.STRING
    },
    lastName: {
      type: DataTypes.STRING
    },
    password: {
      type: DataTypes.TEXT
    },
    roleId: {
      type: DataTypes.INTEGER
    }
  },
  // !end
  // !code: moduleExports // !end
);

// !code: exports // !end
module.exports = moduleExports;

// !code: funcs // !end
// !code: end // !end
  • Have you generated Sequelize models successfully before?
  • Do you remember making any other changes in this app?
  • Can you regenerate again please, using cli+ 0.8.0 which run generator 0.8.0?
  • Are you able to regen on Ubuntu or Mac? This would show if the problem is the platform is Win10.

Your generated code clearly looks something like DataType.INTEGER.toString(). There is some sense in this as https://github.com/ronalddddd/sequelizer uses Sequelize "sequelize": "^3.19.3", However why don't I experience the same situation?

I need to recreate what you are getting. I'll look some more.

Also

  • Did you install cli+ with npm or yarn?
  • Did you by an chance run npm i sequelize on cli+?

Ok I feel terrible, I put the wrong steps to reproduce. I just realized the model was generated by running feathers-plus generate authentication

I installed the cli with npm

I ran

  • generate app
  • generate authentication (local)
  • added the JSON-schema
  • generate authentication

to confirm I saw no difference between generate authentication and generate service.

So please reply to:

  • Have you generated Sequelize models successfully before?
  • Do you remember making any other changes in this app?
  • Can you regenerate again please, using cli+ 0.8.0 which run generator 0.8.0?
  • Are you able to regen on Ubuntu or Mac? This would show if the problem is the platform is Win10.
  • Did you by an chance run npm i sequelize on cli+?

Also

  • How are you running feathers-plus on Windows10? Does the issue still remain if you run it other ways #103

@eddyystop Sorry for the delay
Updated feathers-plus/cli to 0.8.2
Using node 8.15.0 64 bit
NPM 6.8.0
I did not install sequelize from the CLI.

The exact commands I ran from this latest test after upgrading
feathers-plus generate options

  • TypeScript - Y
  • Semicolons - Y
  • Module Changes - N

feathers-plus generate app
** select default options **

  • Use NPM

feathers-plus generate authentication

  • Username & Password
  • Sequelize
  • GraphQL
  • SQL Server

I added the JSON schema and ran
feathers-plus generate all

Looks like it generated the right Sequelize model.

I ran npm start and now fails due to invalid TS code generated in sequelize-mssql.ts, not due to the error I originally created this issue for.

I'll close this and create a new issue.

I