Feature Comments

Generate Table

sequelize model:create --name comments --attributes news_id:integer,name:string,comment:string

Setting Association

reference of association.

Sequelize

  1. Setting di Model comments in method associate
static associate(models) {
   // define association here
   this.belongsTo(models.news, {
     foreignKey: `news_id`
   })
}
  1. Setting di Model news in method associate
static associate(models) {
   // define association here
   this.hasMany(models.comments, {
     foreignKey : "news_id"
   })
}
  1. Add config relation in file migration comment on property news_id to set as foreignKey in table comments
news_id: {
   type: Sequelize.INTEGER,
   allowNull: false,
   onDelete: 'CASCADE', // untuk kasih konfig apabila data member dihapus maka data loan dihapus
   references: {
     model: "news",
     key: 'id'
   }
}
  1. Migrate table comments
npm run db:migrate

Create class service

Create class CommentService and create method store for function create data comment for news id.

Untitled

Create class controller

Crete class controller for handle rest expressjs.