app2-api/modules/comments/comment.model.js
2019-08-21 20:37:58 +02:00

77 lines
1.7 KiB
JavaScript

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;
};