Added comment model, comment post function
This commit is contained in:
38
server/models/Comment.js
Normal file
38
server/models/Comment.js
Normal file
@@ -0,0 +1,38 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Comment = sequelize.define("Comment", {
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
},
|
||||
content: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: false,
|
||||
},
|
||||
parentId: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: true,
|
||||
},
|
||||
}, {
|
||||
tableName: 'comments',
|
||||
timestamps: true,
|
||||
});
|
||||
|
||||
Comment.associate = (models) => {
|
||||
Comment.belongsTo(models.User, {
|
||||
foreignKey: 'userId',
|
||||
onDelete: 'CASCADE',
|
||||
});
|
||||
Comment.belongsTo(models.Post, {
|
||||
foreignKey: 'postId',
|
||||
onDelete: 'CASCADE',
|
||||
});
|
||||
Comment.hasMany(models.Comment, {
|
||||
foreignKey: 'parentId',
|
||||
as: 'replies',
|
||||
});
|
||||
};
|
||||
|
||||
return Comment;
|
||||
}
|
||||
Reference in New Issue
Block a user