audiobookshelf/server/models/PlaylistMediaItem.js

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-07-05 01:14:44 +02:00
const { DataTypes, Model } = require('sequelize')
2023-08-16 23:38:48 +02:00
class PlaylistMediaItem extends Model {
constructor(values, options) {
super(values, options)
/** @type {UUIDV4} */
this.id
/** @type {UUIDV4} */
this.mediaItemId
/** @type {string} */
this.mediaItemType
/** @type {number} */
this.order
/** @type {UUIDV4} */
this.playlistId
/** @type {Date} */
this.createdAt
}
static removeByIds(playlistId, mediaItemId) {
return this.destroy({
where: {
playlistId,
mediaItemId
}
})
}
2023-07-05 01:14:44 +02:00
2023-08-16 23:38:48 +02:00
getMediaItem(options) {
if (!this.mediaItemType) return Promise.resolve(null)
const mixinMethodName = `get${this.sequelize.uppercaseFirst(this.mediaItemType)}`
return this[mixinMethodName](options)
2023-07-05 01:14:44 +02:00
}
2023-08-16 23:38:48 +02:00
/**
* Initialize model
* @param {import('../Database').sequelize} sequelize
*/
static init(sequelize) {
super.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
mediaItemId: DataTypes.UUIDV4,
mediaItemType: DataTypes.STRING,
order: DataTypes.INTEGER
}, {
sequelize,
timestamps: true,
updatedAt: false,
modelName: 'playlistMediaItem'
})
2023-07-05 01:14:44 +02:00
2023-08-16 23:38:48 +02:00
const { book, podcastEpisode, playlist } = sequelize.models
2023-07-05 01:14:44 +02:00
2023-08-16 23:38:48 +02:00
book.hasMany(PlaylistMediaItem, {
foreignKey: 'mediaItemId',
constraints: false,
scope: {
mediaItemType: 'book'
}
})
PlaylistMediaItem.belongsTo(book, { foreignKey: 'mediaItemId', constraints: false })
2023-07-05 01:14:44 +02:00
2023-08-16 23:38:48 +02:00
podcastEpisode.hasOne(PlaylistMediaItem, {
foreignKey: 'mediaItemId',
constraints: false,
scope: {
mediaItemType: 'podcastEpisode'
}
})
PlaylistMediaItem.belongsTo(podcastEpisode, { foreignKey: 'mediaItemId', constraints: false })
2023-07-05 01:14:44 +02:00
2023-08-16 23:38:48 +02:00
PlaylistMediaItem.addHook('afterFind', findResult => {
if (!findResult) return
2023-07-05 01:14:44 +02:00
2023-08-16 23:38:48 +02:00
if (!Array.isArray(findResult)) findResult = [findResult]
2023-07-05 01:14:44 +02:00
2023-08-16 23:38:48 +02:00
for (const instance of findResult) {
if (instance.mediaItemType === 'book' && instance.book !== undefined) {
instance.mediaItem = instance.book
instance.dataValues.mediaItem = instance.dataValues.book
} else if (instance.mediaItemType === 'podcastEpisode' && instance.podcastEpisode !== undefined) {
instance.mediaItem = instance.podcastEpisode
instance.dataValues.mediaItem = instance.dataValues.podcastEpisode
}
// To prevent mistakes:
delete instance.book
delete instance.dataValues.book
delete instance.podcastEpisode
delete instance.dataValues.podcastEpisode
2023-07-05 01:14:44 +02:00
}
2023-08-16 23:38:48 +02:00
})
2023-07-05 01:14:44 +02:00
2023-08-16 23:38:48 +02:00
playlist.hasMany(PlaylistMediaItem, {
onDelete: 'CASCADE'
})
PlaylistMediaItem.belongsTo(playlist)
}
}
2023-07-05 01:14:44 +02:00
2023-08-16 23:38:48 +02:00
module.exports = PlaylistMediaItem