add/modified some backend models relations

This commit is contained in:
X4Lo committed 2024-04-16 10:14:55 +01:00
1 parent 8a1d5a8416
commit ce9dcc79b7
3 files changed
+77 -7

No files matched your search

+46 -5
View File
@@ -9,6 +9,8 @@ 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
@@ -32,8 +34,10 @@ Entry.hasOne(EntryRating, {
EntryRating.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
// * ENTRY & ENTRY_SEEN - ONE TO MANY
Entry.hasMany(EntrySeen, { foreignKey: 'entry_id', onDelete: 'CASCADE', });
EntrySeen.belongsTo(Entry, { foreignKey: 'entry_id' });
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, {
@@ -57,9 +61,32 @@ 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', onDelete: 'CASCADE', });
Season.belongsTo(Entry, { foreignKey: 'entry_id' });
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', });
@@ -70,4 +97,18 @@ Episode.hasMany(EpisodeSeen, { foreignKey: 'episode_id', onDelete: 'CASCADE', })
EpisodeSeen.belongsTo(Episode, { foreignKey: 'episode_id' });
Franchise.hasMany(Entry, { foreignKey: 'franchise_id' });
Entry.belongsTo(Franchise, { 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()
})
});