nodejh / sequelize-automate

Automatically generate bare sequelize models from your database.
MIT License
114 stars 21 forks source link

`timestamps: false` not working #8

Closed athenawisdoms closed 4 years ago

athenawisdoms commented 4 years ago

Hello author!

I tried using sequelize-automate to generate sequelize model files with the option timestamps: false but this option is not included in the model file generated by sequelize-automate.

System

CLI Command used:

sequelize-automate -t js -h localhost -d database_development -u postgres -p password -P 5432 -e postgres -o models -c ./config/sequelize-automate.config.json

./config/sequelize-automate.config.json

Tried

{
  "options": {
    "define": {
      "timestamps": false
    }
  }
}

and

{
  "options": {
    "timestamps": false
  }
}

Model file generated by sequelize-automate

timestamps: false option is not found!

const {
  DataTypes
} = require('sequelize');

module.exports = sequelize => {
  const attributes = {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: "nextval(my_data_id_seq::regclass)",
      comment: null,
      primaryKey: false,
      field: "id"
    },
    score: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: null,
      primaryKey: false,
      field: "score"
    },
  };
  const options = {
    tableName: "my_data",
    comment: "",
    indexes: []
  };
  const MyDataModel = sequelize.define("my_data_model", attributes, options);
  return MyDataModel;
};

Any help is very much appreciated!

nodejh commented 4 years ago

Yes, sequelize-automate doesn't add option timestamps: false to models.

The timestamps field specify whether or not the createdAt and updatedAt fields will be created. We can use configurations such as timestamps paranoid in sequelize.define options and Sequelize constructor. e.g.:

// sequelize define
sequelize.define('modelName', attributes, {
  modelName: 'bar',
  timestamps: false,
  paranoid: true,
  underscored: true,
  // ...
});

// Sequelize constructor
const sequelize = new Sequelize(connectionURI, {
  define: {
    timestamps: false,
    paranoid: true,
    underscored: true,
  }
});

I think in most cases, we will use these configurations in the Sequelize constructor, so that it works for all tables/models. That's why sequelize-automate doesn't put these options in model files. If you only want to use these configurations in a specific table, you can manually modify the generated file by sequelize-automate.

athenawisdoms commented 4 years ago

Thanks for the swift, detailed explanation!