// Import models const Franchise = require('../models/Franchise'); const Entry = require('../models/Entry'); const EntryId = require('../models/EntryId'); const EntryRating = require('../models/EntryRating'); const EntrySeen = require('../models/EntrySeen'); const Collection = require('../models/Collection'); const UserCollectionEntry = require('../models/UserCollectionEntry'); const Season = require('../models/Season'); const Episode = require('../models/Episode'); const EpisodeSeen = require('../models/EpisodeSeen'); const Genre = require('../models/Genre'); const EntryGenre = require('../models/EntryGenre'); const Media = require('../models/Media'); const Network = require('../models/Network'); const EntryNetwork = require('../models/EntryNetwork'); const People = require('../models/People'); const Credit = require('../models/Credit'); const WatchBuddy = require('../models/WatchBuddy'); const EntrySeenBuddy = require('../models/EntrySeenBuddy'); const EpisodeSeenBuddy = require('../models/EpisodeSeenBuddy'); const Video = require('../models/Video'); const VideoEntry = require('../models/VideoEntry'); // ? one to one // A hasOne B = A > B // A belongsTo B = A < B // ? one to many // A hasMany B = A > B // A belongsTo B = A < B // * ENTRY & EntryId - ONE TO ONE Entry.hasOne(EntryId, { foreignKey: 'entry_id', as: 'entryId', onDelete: 'CASCADE', }); EntryId.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' }); // * ENTRY & EntryRating - ONE TO ONE Entry.hasOne(EntryRating, { foreignKey: 'entry_id', as: 'entryRating', onDelete: 'CASCADE', }); EntryRating.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' }); // * ENTRY & ENTRY_SEEN - ONE TO MANY Entry.hasMany(EntrySeen, { foreignKey: 'entry_id', as: 'entrySeen', onDelete: 'CASCADE', }); EntrySeen.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' }); // * ENTRY & LIST - MANY TO MANY Entry.belongsToMany(Collection, { through: UserCollectionEntry, foreignKey: 'entry_id', otherKey: 'user_collection_id', onDelete: 'CASCADE', as: 'collections', }); Collection.belongsToMany(Entry, { through: UserCollectionEntry, foreignKey: 'user_collection_id', otherKey: 'entry_id', onDelete: 'CASCADE', as: 'entries', }); Entry.hasMany(UserCollectionEntry, { foreignKey: 'entry_id', as: 'userCollectionEntries', }); UserCollectionEntry.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' }); 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, { through: EntryGenre, foreignKey: 'entry_id', otherKey: 'genre_id', onDelete: 'CASCADE', as: 'genres', }); Genre.belongsToMany(Entry, { through: EntryGenre, foreignKey: 'genre_id', otherKey: 'entry_id', onDelete: 'CASCADE', as: 'entries', }); Entry.hasMany(EntryGenre, { foreignKey: 'entry_id', as: 'entryGenres' }); EntryGenre.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' }); Genre.hasMany(EntryGenre, { foreignKey: 'genre_id', as: 'entryGenres' }); EntryGenre.belongsTo(Genre, { foreignKey: 'genre_id', as: 'genre' }); // * ENTRY & NETWORK - MANY TO MANY Entry.belongsToMany(Network, { through: EntryNetwork, foreignKey: 'entry_id', otherKey: 'network_id', onDelete: 'CASCADE', as: 'networks', }); Network.belongsToMany(Entry, { through: EntryNetwork, foreignKey: 'network_id', otherKey: 'entry_id', onDelete: 'CASCADE', as: 'entries', }); // ! MEDIA // * MEDIA & ENTRY - MANY TO ONE Media.belongsTo(Entry, { foreignKey: 'mediaableId', as: 'entry', constraints: false, where: { mediaable: 'entry', }, }); Entry.hasMany(Media, { foreignKey: 'mediaableId', constraints: false, as: 'medias', scope: { mediaable: 'entry' }, }); // * MEDIA & People - MANY TO ONE Media.belongsTo(People, { foreignKey: 'mediaableId', as: 'people', constraints: false, where: { mediaable: 'people', }, }); People.hasMany(Media, { foreignKey: 'mediaableId', constraints: false, as: 'medias', scope: { mediaable: 'people' }, }); // * MEDIA & Collection - MANY TO ONE Media.belongsTo(Collection, { foreignKey: 'mediaableId', as: 'collection', constraints: false, where: { mediaable: 'collection', }, }); Collection.hasMany(Media, { foreignKey: 'mediaableId', constraints: false, as: 'medias', 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' }); // * SEASON & EPISODE - ONE TO MANY Season.hasMany(Episode, { foreignKey: 'season_id', onDelete: 'CASCADE' }); Episode.belongsTo(Season, { foreignKey: 'season_id' }); // * EPISODE & EPISODE_SEEN - ONE TO MANY Episode.hasMany(EpisodeSeen, { foreignKey: 'episode_id', onDelete: 'CASCADE' }); EpisodeSeen.belongsTo(Episode, { foreignKey: 'episode_id' }); // * ENTRY & EPISODE_SEEN - ONE TO MANY Entry.hasMany(EpisodeSeen, { foreignKey: 'entry_id', onDelete: 'CASCADE' }); EpisodeSeen.belongsTo(Entry, { foreignKey: 'entry_id' }); // * ---- Franchise.hasMany(Entry, { foreignKey: 'franchise_id' }); Entry.belongsTo(Franchise, { foreignKey: 'franchise_id' }); // * ENTRY & Credit - ONE TO MANY Entry.hasMany(Credit, { foreignKey: 'entry_id', as: 'credits', onDelete: 'CASCADE', }); Credit.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' }); // * PEOPLE & CAST - ONE TO MANY People.hasMany(Credit, { foreignKey: 'people_id', as: 'roles', onDelete: 'CASCADE', }); Credit.belongsTo(People, { foreignKey: 'people_id', as: 'person' }); // * WATCH BUDDY — MEDIA (profile picture) WatchBuddy.hasMany(Media, { foreignKey: 'mediaableId', constraints: false, as: 'medias', scope: { mediaable: 'watch_buddy' }, }); Media.belongsTo(WatchBuddy, { foreignKey: 'mediaableId', as: 'watchBuddy', constraints: false, where: { mediaable: 'watch_buddy' }, }); // * ENTRY_SEEN & WATCH_BUDDY - MANY TO MANY EntrySeen.belongsToMany(WatchBuddy, { through: EntrySeenBuddy, foreignKey: 'entry_seen_id', otherKey: 'watch_buddy_id', as: 'buddies', }); WatchBuddy.belongsToMany(EntrySeen, { through: EntrySeenBuddy, foreignKey: 'watch_buddy_id', otherKey: 'entry_seen_id', as: 'entrySeens', }); // * EPISODE_SEEN & WATCH_BUDDY - MANY TO MANY EpisodeSeen.belongsToMany(WatchBuddy, { through: EpisodeSeenBuddy, foreignKey: 'episode_seen_id', otherKey: 'watch_buddy_id', as: 'buddies', }); WatchBuddy.belongsToMany(EpisodeSeen, { through: EpisodeSeenBuddy, foreignKey: 'watch_buddy_id', otherKey: 'episode_seen_id', as: 'episodeSeens', }); // * VIDEO & ENTRY - MANY TO MANY (discovery videos) Video.belongsToMany(Entry, { through: VideoEntry, foreignKey: 'video_id', otherKey: 'entry_id', onDelete: 'CASCADE', as: 'entries', }); Entry.belongsToMany(Video, { through: VideoEntry, foreignKey: 'entry_id', otherKey: 'video_id', onDelete: 'CASCADE', as: 'videos', });