From bf0378e895839ef9b8b21002a04194cc970cc16e Mon Sep 17 00:00:00 2001 From: X4Lo Date: Mon, 1 Apr 2024 07:31:05 +0000 Subject: [PATCH] add relation between models --- backend/db/relations.js | 76 +++++++++++++++++++++++++++++++++++++---- backend/server.js | 8 ++--- 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/backend/db/relations.js b/backend/db/relations.js index 87dbec1..3850a75 100644 --- a/backend/db/relations.js +++ b/backend/db/relations.js @@ -1,9 +1,15 @@ // Import models // const Persons = require('../models/Person') +const Collection = require('../models/Collection'); const Entry = require('../models/Entry') const EntryId = require('../models/EntryId') const EntryRating = require('../models/EntryRating') -const Collection = require('../models/Collection') +const EntrySeen = require('../models/EntrySeen'); +const List = require('../models/List'); +const ListEntry = require('../models/ListEntry'); +const Season = require('../models/Season'); +const Episode = require('../models/Episode'); +const EpisodeSeen = require('../models/EpisodeSeen'); // ? one to one @@ -14,25 +20,83 @@ const Collection = require('../models/Collection') // A belongsTo B = A < B // Define associations here + +// * ENTRY & EntryId - ONE TO ONE Entry.hasOne(EntryId, { foreignKey: 'entry_id', - as: 'entryId' + 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' + as: 'entryRating', + onDelete: 'CASCADE', }); 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' +}); -Collection.hasMany(Entry, { foreignKey: 'collection_id' }); -Entry.belongsTo(Collection, { foreignKey: 'collection_id' }); \ No newline at end of file +// * ENTRY & LIST - MANY TO MANY +Entry.belongsToMany(List, { + through: ListEntry, + foreignKey: 'entry_id', + otherKey: 'list_id', + onDelete: 'CASCADE', +}); + +List.belongsToMany(Entry, { + through: ListEntry, + foreignKey: 'list_id', + otherKey: 'entry_id', + onDelete: 'CASCADE', +}); + +// * ENTRY & SEASON - ONE TO MANY +Entry.hasMany(Season, { + foreignKey: 'entry_id', + onDelete: 'CASCADE', +}); +Season.belongsTo(Entry, { + foreignKey: 'entry_id' +}); + +// * 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' +}); + +Collection.hasMany(Entry, { + foreignKey: 'collection_id' +}); +Entry.belongsTo(Collection, { + foreignKey: 'collection_id' +}); \ No newline at end of file diff --git a/backend/server.js b/backend/server.js index a4b0591..25af490 100644 --- a/backend/server.js +++ b/backend/server.js @@ -32,13 +32,13 @@ app.use('/api/v1', routes); const relations = require('./db/relations.js') sequelize - .sync({force: true}) + // .sync({force: true}) + .sync({ alter: true }) .then((result) => { // console.log(result); app.listen(PORT, () => { - const Entry = require("./models/Entry.js") - Entry.create({title: "Avengers Endgame"}) - console.log(`Server running on http://localhost:${PORT}`); + //const Entry = require("./models/Entry.js") + console.log(`Backend server running on http://localhost:${PORT}`); }); }) .catch(err => {