app2-api/modules/comments/comment.model.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-05-23 09:40:50 +00:00
module.exports = function (sequelize, DataTypes) {
const Comment = sequelize.define('Comment', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
2019-07-05 07:06:29 +00:00
entityId: {
type: DataTypes.UUID,
allowNull: false,
},
entityName: {
2019-05-23 09:40:50 +00:00
type: DataTypes.STRING,
2019-07-05 07:06:29 +00:00
allowNull: false,
2019-05-23 09:40:50 +00:00
},
content: {
type: DataTypes.STRING,
allowNull: false
},
2019-07-26 14:50:53 +00:00
nameUserOld: {
type: DataTypes.STRING(125),
},
profile_pictureOld: {
type: DataTypes.STRING(255),
},
}, {
indexes: [{
unique: false,
fields: ['entityId']
}],
tableName: 'comments',
freezeTableName: true,
timestamps: true,
2019-07-05 07:06:29 +00:00
2019-07-26 14:50:53 +00:00
defaultScope: {
include: [{
model: sequelize.models.User,
as: 'user',
attributes: ['name', 'surname', 'profile_picture'],
}]
},
});
2019-07-05 07:06:29 +00:00
2019-07-26 14:50:53 +00:00
Comment.addScope('onlyEvents', () => {
return {
where: {
entityName: 'event'
2019-07-05 07:06:29 +00:00
}
2019-07-26 14:50:53 +00:00
}
});
2019-05-23 09:40:50 +00:00
2019-07-26 14:50:53 +00:00
Comment.addScope('onlySpeakers', () => {
return {
where: {
entityName: 'speaker'
}
}
});
2019-07-05 07:06:29 +00:00
2019-05-23 09:40:50 +00:00
Comment.associate = function (models) {
2019-07-24 14:50:41 +00:00
Comment.User = Comment.belongsTo(models.User, { foreignKey: 'userId', constraints: false, as: 'user' });
2019-05-23 09:40:50 +00:00
};
2019-07-26 14:50:53 +00:00
2019-05-23 09:40:50 +00:00
return Comment;
};