From ce9dcc79b78e0f7c0d5cdb80dbacac84082a311a Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Tue, 16 Apr 2024 10:14:55 +0100 Subject: [PATCH] add/modified some backend models relations --- backend/db/genres.sql | 28 ++++++++++++++++++++++ backend/db/relations.js | 51 +++++++++++++++++++++++++++++++++++++---- backend/server.js | 5 ++-- 3 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 backend/db/genres.sql diff --git a/backend/db/genres.sql b/backend/db/genres.sql new file mode 100644 index 0000000..a5a3058 --- /dev/null +++ b/backend/db/genres.sql @@ -0,0 +1,28 @@ +-- 27 genres +INSERT INTO genres(id,name) VALUES (10759,'Action & Adventure'); +INSERT INTO genres(id,name) VALUES (16,'Animation'); +INSERT INTO genres(id,name) VALUES (35,'Comedy'); +INSERT INTO genres(id,name) VALUES (80,'Crime'); +INSERT INTO genres(id,name) VALUES (99,'Documentary'); +INSERT INTO genres(id,name) VALUES (18,'Drama'); +INSERT INTO genres(id,name) VALUES (10751,'Family'); +INSERT INTO genres(id,name) VALUES (10762,'Kids'); +INSERT INTO genres(id,name) VALUES (9648,'Mystery'); +INSERT INTO genres(id,name) VALUES (10763,'News'); +INSERT INTO genres(id,name) VALUES (10764,'Reality'); +INSERT INTO genres(id,name) VALUES (10765,'Sci-Fi & Fantasy'); +INSERT INTO genres(id,name) VALUES (10766,'Soap'); +INSERT INTO genres(id,name) VALUES (10767,'Talk'); +INSERT INTO genres(id,name) VALUES (10768,'War & Politics'); +INSERT INTO genres(id,name) VALUES (37,'Western'); +INSERT INTO genres(id,name) VALUES (28,'Action'); +INSERT INTO genres(id,name) VALUES (12,'Adventure'); +INSERT INTO genres(id,name) VALUES (14,'Fantasy'); +INSERT INTO genres(id,name) VALUES (36,'History'); +INSERT INTO genres(id,name) VALUES (27,'Horror'); +INSERT INTO genres(id,name) VALUES (10402,'Music'); +INSERT INTO genres(id,name) VALUES (10749,'Romance'); +INSERT INTO genres(id,name) VALUES (878,'Science Fiction'); +INSERT INTO genres(id,name) VALUES (10770,'TV Movie'); +INSERT INTO genres(id,name) VALUES (53,'Thriller'); +INSERT INTO genres(id,name) VALUES (10752,'War'); diff --git a/backend/db/relations.js b/backend/db/relations.js index 7d11848..a81ad34 100644 --- a/backend/db/relations.js +++ b/backend/db/relations.js @@ -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' }); \ No newline at end of file +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() + }) +}); \ No newline at end of file diff --git a/backend/server.js b/backend/server.js index 64d484e..a5dd45a 100644 --- a/backend/server.js +++ b/backend/server.js @@ -32,8 +32,9 @@ app.use('/api/v1', routes); const relations = require('./db/relations.js') sequelize - // .sync({force: true}) - .sync({ alter: true }) + // .sync({force: true}) // will drop all tables and recreate them. + // .sync({ alter: true }) // will attempt to automatically alter tables to fit the model definitions. + .sync() .then((result) => { app.listen(PORT, () => { //const Entry = require("./models/Entry.js")