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

+28
View File
@@ -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');
+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()
})
});
+3 -2
View File
@@ -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")