audiobookshelf/server/models/AudioTrack.js

82 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-03-12 20:51:45 +01:00
const { DataTypes, Model } = require('sequelize')
/*
* Polymorphic association: https://sequelize.org/docs/v6/advanced-association-concepts/polymorphic-associations/
* Book has many AudioTrack. PodcastEpisode has one AudioTrack.
*/
module.exports = (sequelize) => {
class AudioTrack extends Model {
getMediaItem(options) {
if (!this.mediaItemType) return Promise.resolve(null)
2023-03-19 21:19:22 +01:00
const mixinMethodName = `get${sequelize.uppercaseFirst(this.mediaItemType)}`
2023-03-12 20:51:45 +01:00
return this[mixinMethodName](options)
}
}
AudioTrack.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
2023-03-19 21:19:22 +01:00
mediaItemId: DataTypes.UUIDV4,
2023-03-12 20:51:45 +01:00
mediaItemType: DataTypes.STRING,
2023-03-15 23:42:35 +01:00
index: DataTypes.INTEGER,
2023-03-18 22:56:57 +01:00
startOffset: DataTypes.FLOAT,
duration: DataTypes.FLOAT,
2023-03-15 23:42:35 +01:00
title: DataTypes.STRING,
mimeType: DataTypes.STRING,
2023-03-18 22:56:57 +01:00
codec: DataTypes.STRING,
trackNumber: DataTypes.INTEGER,
discNumber: DataTypes.INTEGER
2023-03-12 20:51:45 +01:00
}, {
sequelize,
2023-03-19 21:19:22 +01:00
modelName: 'audioTrack'
2023-03-12 20:51:45 +01:00
})
2023-03-19 21:19:22 +01:00
const { book, podcastEpisode, mediaFile } = sequelize.models
2023-03-13 23:13:31 +01:00
2023-03-19 21:19:22 +01:00
mediaFile.hasOne(AudioTrack)
AudioTrack.belongsTo(mediaFile)
2023-03-13 23:13:31 +01:00
2023-03-19 21:19:22 +01:00
book.hasMany(AudioTrack, {
foreignKey: 'mediaItemId',
2023-03-12 20:51:45 +01:00
constraints: false,
scope: {
2023-03-19 21:19:22 +01:00
mediaItemType: 'book'
2023-03-12 20:51:45 +01:00
}
})
2023-03-19 21:19:22 +01:00
AudioTrack.belongsTo(book, { foreignKey: 'mediaItemId', constraints: false })
2023-03-12 20:51:45 +01:00
2023-03-19 21:19:22 +01:00
podcastEpisode.hasOne(AudioTrack, {
foreignKey: 'mediaItemId',
2023-03-12 20:51:45 +01:00
constraints: false,
scope: {
2023-03-19 21:19:22 +01:00
mediaItemType: 'podcastEpisode'
2023-03-12 20:51:45 +01:00
}
})
2023-03-19 21:19:22 +01:00
AudioTrack.belongsTo(podcastEpisode, { foreignKey: 'mediaItemId', constraints: false })
2023-03-12 20:51:45 +01:00
AudioTrack.addHook('afterFind', findResult => {
2023-03-19 21:19:22 +01:00
if (!findResult) return
2023-03-12 20:51:45 +01:00
if (!Array.isArray(findResult)) findResult = [findResult]
2023-03-19 21:19:22 +01:00
2023-03-12 20:51:45 +01:00
for (const instance of findResult) {
2023-03-19 21:19:22 +01:00
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
2023-03-12 20:51:45 +01:00
}
// To prevent mistakes:
2023-03-19 21:19:22 +01:00
delete instance.book
delete instance.dataValues.book
delete instance.podcastEpisode
delete instance.dataValues.podcastEpisode
2023-03-12 20:51:45 +01:00
}
})
return AudioTrack
}