73 lines
2.4 KiB
JavaScript
73 lines
2.4 KiB
JavaScript
// Import models
|
|
const Collection = require('../models/Collection');
|
|
const Entry = require('../models/Entry')
|
|
const EntryId = require('../models/EntryId')
|
|
const EntryRating = require('../models/EntryRating')
|
|
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
|
|
// A hasOne B = A > B
|
|
// A belongsTo B = A < B
|
|
// ? one to many
|
|
// A hasMany B = A > B
|
|
// A belongsTo B = A < B
|
|
|
|
|
|
// * ENTRY & EntryId - ONE TO ONE
|
|
Entry.hasOne(EntryId, {
|
|
foreignKey: 'entry_id', 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', 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' });
|
|
|
|
// * ENTRY & LIST - MANY TO MANY
|
|
Entry.belongsToMany(List, {
|
|
through: ListEntry,
|
|
foreignKey: 'entry_id',
|
|
otherKey: 'list_id',
|
|
onDelete: 'CASCADE',
|
|
as: 'entries',
|
|
});
|
|
|
|
List.belongsToMany(Entry, {
|
|
through: ListEntry,
|
|
foreignKey: 'list_id',
|
|
otherKey: 'entry_id',
|
|
onDelete: 'CASCADE',
|
|
as: 'entries',
|
|
});
|
|
|
|
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.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' }); |