From 7d77a5a8f57d981da79d8b6ee9932e44945ef26f Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Wed, 15 Jan 2020 14:01:14 -0700 Subject: [PATCH] Add Reaction model with associations on Status & Review --- server/sequelize/associations/Review.js | 7 +++ server/sequelize/associations/Status.js | 7 +++ server/sequelize/models/Reaction.js | 72 +++++++++++++++++++++++++ server/sequelize/models/index.js | 1 + 4 files changed, 87 insertions(+) create mode 100644 server/sequelize/models/Reaction.js diff --git a/server/sequelize/associations/Review.js b/server/sequelize/associations/Review.js index d6432bc..0506868 100644 --- a/server/sequelize/associations/Review.js +++ b/server/sequelize/associations/Review.js @@ -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; } \ No newline at end of file diff --git a/server/sequelize/associations/Status.js b/server/sequelize/associations/Status.js index 7f15d31..1635a1b 100644 --- a/server/sequelize/associations/Status.js +++ b/server/sequelize/associations/Status.js @@ -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; } \ No newline at end of file diff --git a/server/sequelize/models/Reaction.js b/server/sequelize/models/Reaction.js new file mode 100644 index 0000000..e9760fa --- /dev/null +++ b/server/sequelize/models/Reaction.js @@ -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' }, + }, + } +}); \ No newline at end of file diff --git a/server/sequelize/models/index.js b/server/sequelize/models/index.js index 1dde40e..ead3a99 100644 --- a/server/sequelize/models/index.js +++ b/server/sequelize/models/index.js @@ -10,6 +10,7 @@ module.exports = sequelize => { 'Status', 'Review', 'Recommendation', + 'Reaction', ]; const models = {};