Sequelize model relationship fixes

This commit is contained in:
Robbie Antenesse 2020-01-25 13:52:37 -07:00
parent b9c9c6bec6
commit b1da2f81c7
10 changed files with 40 additions and 20 deletions

View File

@ -4,7 +4,9 @@ module.exports = models => {
Review, Review,
} = models; } = models;
BookReference.hasMany(Review); BookReference.hasMany(Review, {
foreignKey: 'bookReferenceId',
});
return BookReference; return BookReference;
} }

View File

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

View File

@ -21,7 +21,9 @@ module.exports = models => {
onDelete: 'CASCADE', onDelete: 'CASCADE',
}); });
Shelf.hasMany(ShelfItem); Shelf.hasMany(ShelfItem, {
foreignKey: 'shelfId',
});
return Shelf; return Shelf;
} }

View File

@ -16,7 +16,9 @@ module.exports = models => {
onDelete: 'CASCADE', onDelete: 'CASCADE',
}); });
ShelfItem.hasMany(Status); ShelfItem.hasMany(Status, {
foreignKey: 'shelfItemId',
});
return ShelfItem; return ShelfItem;
} }

View File

@ -1,9 +1,10 @@
module.exports = models => { module.exports = models => {
const { const {
Status, Status,
PermissionLevel,
User, User,
ShelfItem, ShelfItem,
Reactions, Reaction,
} = models; } = models;
Status.belongsTo(User, { Status.belongsTo(User, {
@ -21,7 +22,7 @@ module.exports = models => {
onDelete: 'SET NULL', onDelete: 'SET NULL',
}); });
Status.hasMany(Reactions.scope('Status'), { Status.hasMany(Reaction.scope('Status'), {
foreignKey: 'targetId', foreignKey: 'targetId',
constraints: false, constraints: false,
as: 'Reactions', as: 'Reactions',

View File

@ -13,19 +13,27 @@ module.exports = models => {
foreignKey: 'permissionLevel', foreignKey: 'permissionLevel',
onDelete: 'SET NULL', onDelete: 'SET NULL',
}); });
User.hasMany(Shelf); User.hasMany(Shelf, {
User.hasMany(Status); foreignKey: 'userId',
User.hasMany(Review); });
User.hasMany(Status, {
foreignKey: 'userId',
});
User.hasMany(Review, {
foreignKey: 'userId',
});
User.hasMany(Recommendation, { User.hasMany(Recommendation, {
foreignKey: 'toUser', foreignKey: 'toUser',
}); });
User.hasMany(Follow, { User.belongsToMany(User, {
foreignKey: 'follower', through: Follow,
as: 'Followers',
});
User.belongsTo(Follow.scope('internal'), {
foreignKey: 'following', foreignKey: 'following',
as: 'Follows', as: 'following',
});
User.belongsToMany(User, {
through: Follow,
foreignKey: 'follower',
as: 'follower',
}); });
return User; return User;

View File

@ -5,7 +5,7 @@ module.exports = models => {
const associatedModels = {}; const associatedModels = {};
Object.keys(models).forEach(modelName => { Object.keys(models).forEach(modelName => {
const associationFileName = path.resolve(__dirname, modelName, '.js'); const associationFileName = path.resolve(__dirname, modelName + '.js');
if (fs.existsSync(associationFileName)) { if (fs.existsSync(associationFileName)) {
associatedModels[modelName] = require(associationFileName)(models); associatedModels[modelName] = require(associationFileName)(models);
} else { } else {

View File

@ -49,18 +49,22 @@ module.exports = sequelize => sequelize.define('Follow', {
}, { }, {
indexes: [ indexes: [
{ {
fields: ['follower'], fields: ['follower', 'domain'],
}, },
{ {
fields: ['following', 'domain'], fields: ['following', 'domain'],
}, },
], ],
scopes: { scopes: {
internal: { internalFollowers: {
where: { where: {
followerDomain: { followerDomain: {
[Sequelize.Op.is]: null, [Sequelize.Op.is]: null,
}, },
},
},
internalFollowing: {
where: {
followingDomain: { followingDomain: {
[Sequelize.Op.is]: null, [Sequelize.Op.is]: null,
}, },

View File

@ -23,7 +23,7 @@ module.exports = sequelize => sequelize.define('Shelf', {
}, },
}, },
permissionLevel: { permissionLevel: {
type: Sequelize.BOOLEAN, type: Sequelize.NUMBER,
allowNull: false, allowNull: false,
references: { references: {
model: sequelize.models.PermissionLevel, model: sequelize.models.PermissionLevel,

View File

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