Added comment model, comment post function

This commit is contained in:
Rykkel
2024-07-31 18:59:33 +08:00
parent 4d0a814b73
commit 209b03f35d
4 changed files with 146 additions and 2 deletions

38
server/models/Comment.js Normal file
View 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;
}