Files
Showy/backend/db/relations.js
T

119 lines
3.9 KiB
JavaScript

// 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 UserCollection = require('../models/UserCollection');
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');
// ? 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(UserCollection, {
through: UserCollectionEntry,
foreignKey: 'entry_id',
otherKey: 'user_collection_id',
onDelete: 'CASCADE',
as: 'userCollections',
});
UserCollection.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' });
UserCollection.hasMany(UserCollectionEntry, { foreignKey: 'user_collection_id', as: 'userCollectionEntries' });
UserCollectionEntry.belongsTo(UserCollection, { 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 & 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' });
// ! TRIGGERS
UserCollectionEntry.afterUpdate(async (instance, options) => {
await UserCollection.update(instance.user_collection_id, {
updatedAt: new Date()
})
});
UserCollectionEntry.afterDestroy(async (instance, options) => {
await UserCollection.update(instance.user_collection_id, {
updatedAt: new Date()
})
});