Fabio
Fabio

Reputation: 62

Sequelize-cli creates model with extending Model class

When I create a new model with the sequelize-cli it automatically creates a model by extending the Model class. I would like to create models with the sequelize.define syntax. How can I change this? Is this a configuration I can change?

Command

npx sequelize-cli model:generate --name test --attributes first_name:string,last_name:string

Desired result

    "use strict";
module.exports = (sequelize, DataTypes) => {
  const template = sequelize.define(
    "test",
    {
      first_name: DataTypes.STRING,
      last_name: DataTypes.STRING
    },
    {}
  );

  test.associate = function(models) {
  };
  return test;
};

Actual result

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class test extends Model {
    static associate(models) {
      
    }
  }
  test.init({
    first_name: DataTypes.STRING,
    last_name: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'test',
  });
  return test;
};

Upvotes: 1

Views: 1622

Answers (1)

Ricola3D
Ricola3D

Reputation: 2442

Unfortunately there is no way with the officiel sequelize-cli. Indeed it uses this hardcoded template to generate the model file.

The only ways would be:

  • To create your own fork of sequelize-cli, and change this template file.
  • To define your models manually, without the command line (you just have to copy/paste another model, then replace the names & fields).

Upvotes: 1

Related Questions