sequelize / sequelize-auto

Automatically generate bare sequelize models from your database.
2.9k stars 527 forks source link

createdAt / updateAt in table; missing in Model with timestamps = true; Postgres #650

Open mwbeckner opened 1 year ago

mwbeckner commented 1 year ago

Given a tables with created_at and updated_at defined --

CREATE TABLE scan_forms (
    id character varying(255) PRIMARY KEY,
    customer_id integer,
    pickup_address_id integer,
    customer_reference character varying(255),
    package_count integer,
    pickup_associate character varying(255),
    pickup_time character varying(255),
    ship_date timestamp without time zone NOT NULL,
    created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    total_weight numeric(6,2)
);

Generating the model with timestamps: true results in missing createdAt / updatedAt attributes --

// Model generated with timestamps: true
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
  return sequelize.define('ScanForm', {
    id: {
      type: DataTypes.STRING(255),
      allowNull: false,
      primaryKey: true
    },
    customerId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'customer_id'
    },
    pickupAddressId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'pickup_address_id'
    },
    customerReference: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'customer_reference'
    },
    packageCount: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'package_count'
    },
    pickupAssociate: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'pickup_associate'
    },
    pickupTime: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'pickup_time'
    },
    shipDate: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'ship_date'
    },
    totalWeight: {
      type: DataTypes.DECIMAL,
      allowNull: true,
      field: 'total_weight'
    }
  }, {
    sequelize,
    tableName: 'scan_forms',
    schema: 'public',
    timestamps: true,
    indexes: [
      {
        name: "scan_forms_pkey",
        unique: true,
        fields: [
          { name: "id" },
        ]
      },
    ]
  });
};

Generating with timestamps: false results in generated createdAt / updatedAt attributes, but disabled for use by Sequelize --

// Model generated with timestamps: false
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
  return sequelize.define('ScanForm', {
    id: {
      type: DataTypes.STRING(255),
      allowNull: false,
      primaryKey: true
    },
    customerId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'customer_id'
    },
    pickupAddressId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'pickup_address_id'
    },
    customerReference: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'customer_reference'
    },
    packageCount: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'package_count'
    },
    pickupAssociate: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'pickup_associate'
    },
    pickupTime: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'pickup_time'
    },
    shipDate: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'ship_date'
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
      field: 'created_at'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
      field: 'updated_at'
    },
    totalWeight: {
      type: DataTypes.DECIMAL,
      allowNull: true,
      field: 'total_weight'
    }
  }, {
    sequelize,
    tableName: 'scan_forms',
    schema: 'public',
    timestamps: false,
    indexes: [
      {
        name: "scan_forms_pkey",
        unique: true,
        fields: [
          { name: "id" },
        ]
      },
    ]
  });
};

My SequelizeAuto config is --


{
      host: 'localhost',
      dialect: 'postgres',
      port: '5432',
      directory: `./updated-models/${sourceDbName}`, // where to write files
      views: true,
      caseModel: 'p', // convert snake_case column names to PascalCase field names: user_id -> userId
      caseFile: 'k', // file names created for each model use kabob-case.js not snake_case.js
      caseProp: 'c', // file names created for each model use camelCase.js not snake_case.js
      singularize: true, // convert plural table names to singular model names
      additional: { // ...options added to each model
        timestamps: false // already config'd
      },
      tables: newTables,
      noInitModels: true
}
mwbeckner commented 1 year ago

My expectation is that Models should be generated to include all table fields/columns; timestamp setting should not affect that. CreatedAt / updatedAt should always be generated and the model option for timestamps be set from the generation config.