module.exports = function (sequelize, DataTypes) { const Comment = sequelize.define('Comment', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true }, entityId: { type: DataTypes.UUID, allowNull: false, }, entityName: { type: DataTypes.STRING, allowNull: false, }, content: { type: DataTypes.STRING, allowNull: false }, nameUserOld: { type: DataTypes.STRING(125), }, profile_pictureOld: { type: DataTypes.STRING(255), }, }, { indexes: [{ unique: false, fields: ['entityId'] }], tableName: 'comments', freezeTableName: true, timestamps: true, defaultScope: { include: [{ model: sequelize.models.User, as: 'user', attributes: ['name', 'surname', 'profile_picture'], }] }, }); Comment.addScope('onlyEvents', () => { return { where: { entityName: 'event' } } }); Comment.addScope('onlySpeakers', () => { return { where: { entityName: 'speaker' } } }); Comment.addScope('onlyPosts', () => { return { where: { entityName: 'post' } } }); Comment.associate = function (models) { Comment.User = Comment.belongsTo(models.User, { foreignKey: 'userId', constraints: false, as: 'user' }); }; return Comment; };