Complete migration file

This commit is contained in:
advplyr 2023-03-18 16:56:57 -05:00
parent b8de041497
commit 243bc7b0d0
22 changed files with 1203 additions and 162 deletions

View file

@ -1,7 +1,5 @@
const { DataTypes, Model } = require('sequelize')
const uppercaseFirst = str => `${str[0].toUpperCase()}${str.substr(1)}`
/*
* Polymorphic association: https://sequelize.org/docs/v6/advanced-association-concepts/polymorphic-associations/
* Feeds can be created from LibraryItem, Collection, Playlist or Series
@ -10,7 +8,7 @@ module.exports = (sequelize) => {
class Feed extends Model {
getEntity(options) {
if (!this.entityType) return Promise.resolve(null)
const mixinMethodName = `get${uppercaseFirst(this.entityType)}`
const mixinMethodName = `get${this.entityType}`
return this[mixinMethodName](options)
}
}
@ -23,7 +21,7 @@ module.exports = (sequelize) => {
},
slug: DataTypes.STRING,
entityType: DataTypes.STRING,
entityId: DataTypes.UUIDV4,
EntityId: DataTypes.UUIDV4,
entityUpdatedAt: DataTypes.DATE,
serverAddress: DataTypes.STRING,
feedURL: DataTypes.STRING,
@ -49,51 +47,51 @@ module.exports = (sequelize) => {
Feed.belongsTo(User)
LibraryItem.hasMany(Feed, {
foreignKey: 'entityId',
foreignKey: 'EntityId',
constraints: false,
scope: {
entityType: 'libraryItem'
entityType: 'LibraryItem'
}
})
Feed.belongsTo(LibraryItem, { foreignKey: 'entityId', constraints: false })
Feed.belongsTo(LibraryItem, { foreignKey: 'EntityId', constraints: false })
Collection.hasMany(Feed, {
foreignKey: 'entityId',
foreignKey: 'EntityId',
constraints: false,
scope: {
entityType: 'collection'
entityType: 'Collection'
}
})
Feed.belongsTo(Collection, { foreignKey: 'entityId', constraints: false })
Feed.belongsTo(Collection, { foreignKey: 'EntityId', constraints: false })
Series.hasMany(Feed, {
foreignKey: 'entityId',
foreignKey: 'EntityId',
constraints: false,
scope: {
entityType: 'series'
entityType: 'Series'
}
})
Feed.belongsTo(Series, { foreignKey: 'entityId', constraints: false })
Feed.belongsTo(Series, { foreignKey: 'EntityId', constraints: false })
Playlist.hasMany(Feed, {
foreignKey: 'entityId',
foreignKey: 'EntityId',
constraints: false,
scope: {
entityType: 'playlist'
entityType: 'Playlist'
}
})
Feed.belongsTo(Playlist, { foreignKey: 'entityId', constraints: false })
Feed.belongsTo(Playlist, { foreignKey: 'EntityId', constraints: false })
Feed.addHook('afterFind', findResult => {
if (!Array.isArray(findResult)) findResult = [findResult]
for (const instance of findResult) {
if (instance.entityType === 'libraryItem' && instance.LibraryItem !== undefined) {
if (instance.entityType === 'LibraryItem' && instance.LibraryItem !== undefined) {
instance.Entity = instance.LibraryItem
} else if (instance.mediaItemType === 'collection' && instance.Collection !== undefined) {
} else if (instance.mediaItemType === 'Collection' && instance.Collection !== undefined) {
instance.Entity = instance.Collection
} else if (instance.mediaItemType === 'series' && instance.Series !== undefined) {
} else if (instance.mediaItemType === 'Series' && instance.Series !== undefined) {
instance.Entity = instance.Series
} else if (instance.mediaItemType === 'playlist' && instance.Playlist !== undefined) {
} else if (instance.mediaItemType === 'Playlist' && instance.Playlist !== undefined) {
instance.Entity = instance.Playlist
}