mysqlnode.jssequelize.jssequelize-cli

sequelize migration file : adding createdAt and updatedAt columns


I have a sequelize V5 with nodeJS question. I have a couple of models, User, Product and want to add createdAt and updatedAt columns to those tables in my MYSQL database. Now I don't want to expose these fields to the consumer of the API (Front End application) but I want to populate these columns every time a record is created and updated.

Can somebody give me some advise? Many Thanks, Pete


Solution

  • you can add the timestamps property in the schema definition which will automatically add these fields and manage them whenever a record is created or updated.

     const model = sequelize.define('charity', {
    
        name: DataTypes.STRING,
        address: DataTypes.TEXT,
        contactPerson: DataTypes.STRING,
        phone: DataTypes.STRING,
        email: DataTypes.STRING,
    
    }, {
        timestamps: true
    });