diff --git a/backend/config/relations.js b/backend/config/relations.js index f3d3c58..65a43f6 100644 --- a/backend/config/relations.js +++ b/backend/config/relations.js @@ -103,10 +103,8 @@ Network.belongsToMany(Entry, { as: 'entries', }); -// * ENTRY & MEDIA - ONE TO MANY -// Entry.hasMany(Media, { foreignKey: 'entryId', as: 'medias', onDelete: 'CASCADE', }); -// Media.belongsTo(Entry, { foreignKey: 'entryId', as: 'entry' }); - +// ! MEDIA +// * MEDIA & ENTRY - MANY TO ONE Media.belongsTo(Entry, { foreignKey: 'mediaableId', as: 'entry', @@ -116,15 +114,6 @@ Media.belongsTo(Entry, { } }); -Media.belongsTo(Collection, { - foreignKey: 'mediaableId', - as: 'collection', - constraints: false, - where: { - mediaable: 'collection' - } -}); - Entry.hasMany(Media, { foreignKey: 'mediaableId', constraints: false, @@ -132,6 +121,16 @@ Entry.hasMany(Media, { scope: { mediaable: 'entry' } }); +// * MEDIA & Collection - MANY TO ONE +Media.belongsTo(Collection, { + foreignKey: 'mediaableId', + as: 'collection', + constraints: false, + where: { + mediaable: 'collection' + } +}); + Collection.hasMany(Media, { foreignKey: 'mediaableId', constraints: false, @@ -139,6 +138,41 @@ Collection.hasMany(Media, { scope: { mediaable: 'collection' } }); +// * MEDIA & Season - MANY TO ONE +Media.belongsTo(Season, { + foreignKey: 'mediaableId', + as: 'season', + constraints: false, + where: { + mediaable: 'season' + } +}); + +Season.hasMany(Media, { + foreignKey: 'mediaableId', + constraints: false, + as: 'medias', + scope: { mediaable: 'season' } +}); + +// * MEDIA & Episode - MANY TO ONE +Media.belongsTo(Episode, { + foreignKey: 'mediaableId', + as: 'episode', + constraints: false, + where: { + mediaable: 'episode' + } +}); + +Episode.hasMany(Media, { + foreignKey: 'mediaableId', + constraints: false, + as: 'medias', + scope: { mediaable: 'episode' } +}); +// ! MEDIA END + // * ENTRY & SEASON - ONE TO MANY Entry.hasMany(Season, { foreignKey: 'entry_id', as: 'seasons', onDelete: 'CASCADE', }); Season.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });