From b1da2f81c7fd5d192545bb5ed1d379e22b833dcb Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Sat, 25 Jan 2020 13:52:37 -0700 Subject: [PATCH] Sequelize model relationship fixes --- .../sequelize/associations/BookReference.js | 4 ++- server/sequelize/associations/Review.js | 4 +-- server/sequelize/associations/Shelf.js | 4 ++- server/sequelize/associations/ShelfItem.js | 4 ++- server/sequelize/associations/Status.js | 5 ++-- server/sequelize/associations/User.js | 26 ++++++++++++------- server/sequelize/associations/index.js | 2 +- server/sequelize/models/Follow.js | 8 ++++-- server/sequelize/models/Shelf.js | 2 +- server/sequelize/models/index.js | 1 + 10 files changed, 40 insertions(+), 20 deletions(-) diff --git a/server/sequelize/associations/BookReference.js b/server/sequelize/associations/BookReference.js index c4efbcb..79739b1 100644 --- a/server/sequelize/associations/BookReference.js +++ b/server/sequelize/associations/BookReference.js @@ -4,7 +4,9 @@ module.exports = models => { Review, } = models; - BookReference.hasMany(Review); + BookReference.hasMany(Review, { + foreignKey: 'bookReferenceId', + }); return BookReference; } \ No newline at end of file diff --git a/server/sequelize/associations/Review.js b/server/sequelize/associations/Review.js index 0506868..825260e 100644 --- a/server/sequelize/associations/Review.js +++ b/server/sequelize/associations/Review.js @@ -4,7 +4,7 @@ module.exports = models => { User, PermissionLevel, BookReference, - Reactions, + Reaction, } = models; Review.belongsTo(User, { @@ -22,7 +22,7 @@ module.exports = models => { onDelete: 'SET NULL', }); - Review.hasMany(Reactions.scope('Review'), { + Review.hasMany(Reaction.scope('Review'), { foreignKey: 'targetId', constraints: false, as: 'Reactions', diff --git a/server/sequelize/associations/Shelf.js b/server/sequelize/associations/Shelf.js index 2129894..5c2e503 100644 --- a/server/sequelize/associations/Shelf.js +++ b/server/sequelize/associations/Shelf.js @@ -21,7 +21,9 @@ module.exports = models => { onDelete: 'CASCADE', }); - Shelf.hasMany(ShelfItem); + Shelf.hasMany(ShelfItem, { + foreignKey: 'shelfId', + }); return Shelf; } \ No newline at end of file diff --git a/server/sequelize/associations/ShelfItem.js b/server/sequelize/associations/ShelfItem.js index 1a86f73..1ed373c 100644 --- a/server/sequelize/associations/ShelfItem.js +++ b/server/sequelize/associations/ShelfItem.js @@ -16,7 +16,9 @@ module.exports = models => { onDelete: 'CASCADE', }); - ShelfItem.hasMany(Status); + ShelfItem.hasMany(Status, { + foreignKey: 'shelfItemId', + }); return ShelfItem; } \ No newline at end of file diff --git a/server/sequelize/associations/Status.js b/server/sequelize/associations/Status.js index 1635a1b..64ffd49 100644 --- a/server/sequelize/associations/Status.js +++ b/server/sequelize/associations/Status.js @@ -1,9 +1,10 @@ module.exports = models => { const { Status, + PermissionLevel, User, ShelfItem, - Reactions, + Reaction, } = models; Status.belongsTo(User, { @@ -21,7 +22,7 @@ module.exports = models => { onDelete: 'SET NULL', }); - Status.hasMany(Reactions.scope('Status'), { + Status.hasMany(Reaction.scope('Status'), { foreignKey: 'targetId', constraints: false, as: 'Reactions', diff --git a/server/sequelize/associations/User.js b/server/sequelize/associations/User.js index 47b59dc..85445af 100644 --- a/server/sequelize/associations/User.js +++ b/server/sequelize/associations/User.js @@ -13,19 +13,27 @@ module.exports = models => { foreignKey: 'permissionLevel', onDelete: 'SET NULL', }); - User.hasMany(Shelf); - User.hasMany(Status); - User.hasMany(Review); + User.hasMany(Shelf, { + foreignKey: 'userId', + }); + User.hasMany(Status, { + foreignKey: 'userId', + }); + User.hasMany(Review, { + foreignKey: 'userId', + }); User.hasMany(Recommendation, { foreignKey: 'toUser', }); - User.hasMany(Follow, { - foreignKey: 'follower', - as: 'Followers', - }); - User.belongsTo(Follow.scope('internal'), { + User.belongsToMany(User, { + through: Follow, foreignKey: 'following', - as: 'Follows', + as: 'following', + }); + User.belongsToMany(User, { + through: Follow, + foreignKey: 'follower', + as: 'follower', }); return User; diff --git a/server/sequelize/associations/index.js b/server/sequelize/associations/index.js index 8a230f5..f274a70 100644 --- a/server/sequelize/associations/index.js +++ b/server/sequelize/associations/index.js @@ -5,7 +5,7 @@ module.exports = models => { const associatedModels = {}; Object.keys(models).forEach(modelName => { - const associationFileName = path.resolve(__dirname, modelName, '.js'); + const associationFileName = path.resolve(__dirname, modelName + '.js'); if (fs.existsSync(associationFileName)) { associatedModels[modelName] = require(associationFileName)(models); } else { diff --git a/server/sequelize/models/Follow.js b/server/sequelize/models/Follow.js index dddb9bf..e121868 100644 --- a/server/sequelize/models/Follow.js +++ b/server/sequelize/models/Follow.js @@ -49,18 +49,22 @@ module.exports = sequelize => sequelize.define('Follow', { }, { indexes: [ { - fields: ['follower'], + fields: ['follower', 'domain'], }, { fields: ['following', 'domain'], }, ], scopes: { - internal: { + internalFollowers: { where: { followerDomain: { [Sequelize.Op.is]: null, }, + }, + }, + internalFollowing: { + where: { followingDomain: { [Sequelize.Op.is]: null, }, diff --git a/server/sequelize/models/Shelf.js b/server/sequelize/models/Shelf.js index ff8cf1a..550683c 100644 --- a/server/sequelize/models/Shelf.js +++ b/server/sequelize/models/Shelf.js @@ -23,7 +23,7 @@ module.exports = sequelize => sequelize.define('Shelf', { }, }, permissionLevel: { - type: Sequelize.BOOLEAN, + type: Sequelize.NUMBER, allowNull: false, references: { model: sequelize.models.PermissionLevel, diff --git a/server/sequelize/models/index.js b/server/sequelize/models/index.js index ead3a99..2b44ab3 100644 --- a/server/sequelize/models/index.js +++ b/server/sequelize/models/index.js @@ -11,6 +11,7 @@ module.exports = sequelize => { 'Review', 'Recommendation', 'Reaction', + 'Follow', ]; const models = {};