Add Reaction model with associations on Status & Review

This commit is contained in:
Robbie Antenesse 2020-01-15 14:01:14 -07:00
parent 071e1e6586
commit 7d77a5a8f5
4 changed files with 87 additions and 0 deletions

View File

@ -4,6 +4,7 @@ module.exports = models => {
User,
PermissionLevel,
BookReference,
Reactions,
} = models;
Review.belongsTo(User, {
@ -21,5 +22,11 @@ module.exports = models => {
onDelete: 'SET NULL',
});
Review.hasMany(Reactions.scope('Review'), {
foreignKey: 'targetId',
constraints: false,
as: 'Reactions',
});
return Review;
}

View File

@ -3,6 +3,7 @@ module.exports = models => {
Status,
User,
ShelfItem,
Reactions,
} = models;
Status.belongsTo(User, {
@ -20,5 +21,11 @@ module.exports = models => {
onDelete: 'SET NULL',
});
Status.hasMany(Reactions.scope('Status'), {
foreignKey: 'targetId',
constraints: false,
as: 'Reactions',
});
return Status;
}

View File

@ -0,0 +1,72 @@
const Sequelize = require('sequelize');
module.exports = sequelize => sequelize.define('Reaction', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: sequelize.models.User,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
},
},
targetType: {
type: Sequelize.ENUM,
values: ['Status', 'Review'],
allowNull: false,
comment: 'The model/table that `targetId` lives in.',
},
targetId: {
type: Sequelize.INTEGER,
allowNull: false,
comment: 'The id of the `targetType` record this Reaction relates to.',
},
reactionType: {
type: Sequelize.ENUM,
values: ['comment', 'like', 'emoji'],
allowNull: false,
comment: 'Stores what type of reaction this record is: Comment, Like, Emoji',
},
reaction: {
type: Sequelize.TEXT,
allowNull: true,
comment: 'Stores the Comment or Emoji value of the reaction if not a Like.',
},
// Timestamps
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
}, {
indexes: [
{
fields: ['userId'],
},
{
fields: ['targetType', 'targetId'],
},
{
fields: ['reactionType'],
},
],
scopes: {
Status: {
where: { targetType: 'Status' },
},
Review: {
where: { targetType: 'Review' },
},
}
});

View File

@ -10,6 +10,7 @@ module.exports = sequelize => {
'Status',
'Review',
'Recommendation',
'Reaction',
];
const models = {};