add relations
This commit is contained in:
1 parent
ffb9ce8eef
commit
e0d9fa44b6
1 file changed
+19
-50
+19
-50
@@ -1,5 +1,4 @@
|
|||||||
// Import models
|
// Import models
|
||||||
// const Persons = require('../models/Person')
|
|
||||||
const Collection = require('../models/Collection');
|
const Collection = require('../models/Collection');
|
||||||
const Entry = require('../models/Entry')
|
const Entry = require('../models/Entry')
|
||||||
const EntryId = require('../models/EntryId')
|
const EntryId = require('../models/EntryId')
|
||||||
@@ -19,38 +18,22 @@ const EpisodeSeen = require('../models/EpisodeSeen');
|
|||||||
// A hasMany B = A > B
|
// A hasMany B = A > B
|
||||||
// A belongsTo B = A < B
|
// A belongsTo B = A < B
|
||||||
|
|
||||||
// Define associations here
|
|
||||||
|
|
||||||
// * ENTRY & EntryId - ONE TO ONE
|
// * ENTRY & EntryId - ONE TO ONE
|
||||||
Entry.hasOne(EntryId, {
|
Entry.hasOne(EntryId, {
|
||||||
foreignKey: 'entry_id',
|
foreignKey: 'entry_id', as: 'entryId', onDelete: 'CASCADE',
|
||||||
as: 'entryId',
|
|
||||||
onDelete: 'CASCADE',
|
|
||||||
});
|
|
||||||
EntryId.belongsTo(Entry, {
|
|
||||||
foreignKey: 'entry_id',
|
|
||||||
as: 'entry'
|
|
||||||
});
|
});
|
||||||
|
EntryId.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
|
||||||
|
|
||||||
// * ENTRY & EntryRating - ONE TO ONE
|
// * ENTRY & EntryRating - ONE TO ONE
|
||||||
Entry.hasOne(EntryRating, {
|
Entry.hasOne(EntryRating, {
|
||||||
foreignKey: 'entry_id',
|
foreignKey: 'entry_id', as: 'entryRating', onDelete: 'CASCADE',
|
||||||
as: 'entryRating',
|
|
||||||
onDelete: 'CASCADE',
|
|
||||||
});
|
|
||||||
EntryRating.belongsTo(Entry, {
|
|
||||||
foreignKey: 'entry_id',
|
|
||||||
as: 'entry'
|
|
||||||
});
|
});
|
||||||
|
EntryRating.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
|
||||||
|
|
||||||
// * ENTRY & ENTRY_SEEN - ONE TO MANY
|
// * ENTRY & ENTRY_SEEN - ONE TO MANY
|
||||||
Entry.hasMany(EntrySeen, {
|
Entry.hasMany(EntrySeen, { foreignKey: 'entry_id', onDelete: 'CASCADE', });
|
||||||
foreignKey: 'entry_id',
|
EntrySeen.belongsTo(Entry, { foreignKey: 'entry_id' });
|
||||||
onDelete: 'CASCADE',
|
|
||||||
});
|
|
||||||
EntrySeen.belongsTo(Entry, {
|
|
||||||
foreignKey: 'entry_id'
|
|
||||||
});
|
|
||||||
|
|
||||||
// * ENTRY & LIST - MANY TO MANY
|
// * ENTRY & LIST - MANY TO MANY
|
||||||
Entry.belongsToMany(List, {
|
Entry.belongsToMany(List, {
|
||||||
@@ -67,36 +50,22 @@ List.belongsToMany(Entry, {
|
|||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Entry.hasMany(ListEntry, { foreignKey: 'entry_id', as: 'listEntries' });
|
||||||
|
ListEntry.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
|
||||||
|
List.hasMany(ListEntry, { foreignKey: 'list_id', as: 'listEntries' });
|
||||||
|
ListEntry.belongsTo(List, { foreignKey: 'list_id', as: 'list' });
|
||||||
|
|
||||||
// * ENTRY & SEASON - ONE TO MANY
|
// * ENTRY & SEASON - ONE TO MANY
|
||||||
Entry.hasMany(Season, {
|
Entry.hasMany(Season, { foreignKey: 'entry_id', onDelete: 'CASCADE', });
|
||||||
foreignKey: 'entry_id',
|
Season.belongsTo(Entry, { foreignKey: 'entry_id' });
|
||||||
onDelete: 'CASCADE',
|
|
||||||
});
|
|
||||||
Season.belongsTo(Entry, {
|
|
||||||
foreignKey: 'entry_id'
|
|
||||||
});
|
|
||||||
|
|
||||||
// * SEASON & EPISODE - ONE TO MANY
|
// * SEASON & EPISODE - ONE TO MANY
|
||||||
Season.hasMany(Episode, {
|
Season.hasMany(Episode, { foreignKey: 'season_id', onDelete: 'CASCADE', });
|
||||||
foreignKey: 'season_id',
|
Episode.belongsTo(Season, { foreignKey: 'season_id' });
|
||||||
onDelete: 'CASCADE',
|
|
||||||
});
|
|
||||||
Episode.belongsTo(Season, {
|
|
||||||
foreignKey: 'season_id'
|
|
||||||
});
|
|
||||||
|
|
||||||
// * EPISODE & EPISODE_SEEN - ONE TO MANY
|
// * EPISODE & EPISODE_SEEN - ONE TO MANY
|
||||||
Episode.hasMany(EpisodeSeen, {
|
Episode.hasMany(EpisodeSeen, { foreignKey: 'episode_id', onDelete: 'CASCADE', });
|
||||||
foreignKey: 'episode_id',
|
EpisodeSeen.belongsTo(Episode, { foreignKey: 'episode_id' });
|
||||||
onDelete: 'CASCADE',
|
|
||||||
});
|
|
||||||
EpisodeSeen.belongsTo(Episode, {
|
|
||||||
foreignKey: 'episode_id'
|
|
||||||
});
|
|
||||||
|
|
||||||
Collection.hasMany(Entry, {
|
Collection.hasMany(Entry, { foreignKey: 'collection_id' });
|
||||||
foreignKey: 'collection_id'
|
Entry.belongsTo(Collection, { foreignKey: 'collection_id' });
|
||||||
});
|
|
||||||
Entry.belongsTo(Collection, {
|
|
||||||
foreignKey: 'collection_id'
|
|
||||||
});
|
|
||||||
Reference in new issue
Block a user