Back: reworked the media/entry relation

This commit is contained in:
X4Lo committed 2024-09-10 21:32:46 +01:00
1 parent d06a21097f
commit b42e055691
5 files changed
+41 -30

No files matched your search

+15 -7
View File
@@ -4,7 +4,7 @@ const Entry = require('../models/Entry')
const EntryId = require('../models/EntryId')
const EntryRating = require('../models/EntryRating')
const EntrySeen = require('../models/EntrySeen');
const UserCollection = require('../models/UserCollection');
const Collection = require('../models/Collection');
const UserCollectionEntry = require('../models/UserCollectionEntry');
const Season = require('../models/Season');
const Episode = require('../models/Episode');
@@ -41,7 +41,7 @@ Entry.hasMany(EntrySeen, {
EntrySeen.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
// * ENTRY & LIST - MANY TO MANY
Entry.belongsToMany(UserCollection, {
Entry.belongsToMany(Collection, {
through: UserCollectionEntry,
foreignKey: 'entry_id',
otherKey: 'user_collection_id',
@@ -49,7 +49,7 @@ Entry.belongsToMany(UserCollection, {
as: 'collections',
});
UserCollection.belongsToMany(Entry, {
Collection.belongsToMany(Entry, {
through: UserCollectionEntry,
foreignKey: 'user_collection_id',
otherKey: 'entry_id',
@@ -59,8 +59,8 @@ UserCollection.belongsToMany(Entry, {
Entry.hasMany(UserCollectionEntry, { foreignKey: 'entry_id', as: 'userCollectionEntries' });
UserCollectionEntry.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
UserCollection.hasMany(UserCollectionEntry, { foreignKey: 'user_collection_id', as: 'userCollectionEntries' });
UserCollectionEntry.belongsTo(UserCollection, { foreignKey: 'user_collection_id', as: 'userCollection' });
Collection.hasMany(UserCollectionEntry, { foreignKey: 'user_collection_id', as: 'userCollectionEntries' });
UserCollectionEntry.belongsTo(Collection, { foreignKey: 'user_collection_id', as: 'userCollection' });
// * ENTRY & GENRE - MANY TO MANY
Entry.belongsToMany(Genre, {
@@ -86,8 +86,16 @@ EntryGenre.belongsTo(Genre, { foreignKey: 'genre_id', as: 'genre' });
// * ENTRY & MEDIA - ONE TO MANY
Entry.hasMany(Media, { foreignKey: 'entryId', as: 'medias', onDelete: 'CASCADE', });
Media.belongsTo(Entry, { foreignKey: 'entryId', as: 'entry' });
// Entry.hasMany(Media, { foreignKey: 'entryId', as: 'medias', onDelete: 'CASCADE', });
// Media.belongsTo(Entry, { foreignKey: 'entryId', as: 'entry' });
// * ENTRY & MEDIA - ONE TO MANY
Media.belongsTo(Entry, { foreignKey: 'mediaableId', as: 'entry', constraints: false, scope: { mediaable: 'entry' } });
Media.belongsTo(Collection, { foreignKey: 'mediaableId', as: 'collection', constraints: false, scope: { mediaable: 'collection' } });
// * ENTRY & COLLECTION - ONE TO MANY
Entry.hasMany(Media, { foreignKey: 'mediaableId', constraints: false, as: 'medias', scope: { mediaable: 'entry' } });
Collection.hasMany(Media, { foreignKey: 'mediaableId', constraints: false, as: 'medias', scope: { mediaable: 'collection' } });
// * ENTRY & SEASON - ONE TO MANY
Entry.hasMany(Season, { foreignKey: 'entry_id', as: 'seasons', onDelete: 'CASCADE', });