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,
} = models;
BookReference.hasMany(Review);
BookReference.hasMany(Review, {
foreignKey: 'bookReferenceId',
});
return BookReference;
}

View File

@ -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',

View File

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

View File

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

View File

@ -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',

View File

@ -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;

View File

@ -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 {

View File

@ -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,
},

View File

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

View File

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