PlaybackSession, Playlist, PlaylistMediaItem, Device data models

This commit is contained in:
advplyr 2023-03-12 17:55:12 -05:00
parent a1a923df94
commit bed3758268
9 changed files with 231 additions and 25 deletions

View File

@ -62,6 +62,10 @@ class Database {
require('./models/PodcastTag')(this.sequelize)
require('./models/Collection')(this.sequelize)
require('./models/CollectionBook')(this.sequelize)
require('./models/Playlist')(this.sequelize)
require('./models/PlaylistMediaItem')(this.sequelize)
require('./models/Device')(this.sequelize)
require('./models/PlaybackSession')(this.sequelize)
return this.sequelize.sync()
}

View File

@ -52,20 +52,19 @@ module.exports = (sequelize) => {
AudioBookmark.addHook('afterFind', findResult => {
if (!Array.isArray(findResult)) findResult = [findResult]
for (const instance of findResult) {
if (instance.mediaItemType === 'book' && instance.book !== undefined) {
instance.mediaItem = instance.book
} else if (instance.mediaItemType === 'podcastEpisode' && instance.podcastEpisode !== undefined) {
instance.mediaItem = instance.podcastEpisode
if (instance.mediaItemType === 'book' && instance.Book !== undefined) {
instance.MediaItem = instance.Book
} else if (instance.mediaItemType === 'podcastEpisode' && instance.PodcastEpisode !== undefined) {
instance.MediaItem = instance.PodcastEpisode
}
// To prevent mistakes:
delete instance.book
delete instance.dataValues.book
delete instance.podcastEpisode
delete instance.dataValues.podcastEpisode
delete instance.Book
delete instance.dataValues.Book
delete instance.PodcastEpisode
delete instance.dataValues.PodcastEpisode
}
})
User.hasMany(AudioBookmark)
AudioBookmark.belongsTo(User)

View File

@ -51,16 +51,16 @@ module.exports = (sequelize) => {
AudioTrack.addHook('afterFind', findResult => {
if (!Array.isArray(findResult)) findResult = [findResult]
for (const instance of findResult) {
if (instance.mediaItemType === 'book' && instance.book !== undefined) {
instance.mediaItem = instance.book
} else if (instance.mediaItemType === 'podcastEpisode' && instance.podcastEpisode !== undefined) {
instance.mediaItem = instance.podcastEpisode
if (instance.mediaItemType === 'book' && instance.Book !== undefined) {
instance.MediaItem = instance.Book
} else if (instance.mediaItemType === 'podcastEpisode' && instance.PodcastEpisode !== undefined) {
instance.MediaItem = instance.PodcastEpisode
}
// To prevent mistakes:
delete instance.book
delete instance.dataValues.book
delete instance.podcastEpisode
delete instance.dataValues.podcastEpisode
delete instance.Book
delete instance.dataValues.Book
delete instance.PodcastEpisode
delete instance.dataValues.PodcastEpisode
}
})

29
server/models/Device.js Normal file
View File

@ -0,0 +1,29 @@
const { DataTypes, Model } = require('sequelize')
module.exports = (sequelize) => {
class Device extends Model { }
Device.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
identifier: DataTypes.STRING,
clientName: DataTypes.STRING, // e.g. Abs Web, Abs Android
clientVersion: DataTypes.STRING,
ipAddress: DataTypes.STRING,
deviceName: DataTypes.STRING, // e.g. Windows 10 Chrome, Google Pixel 6, Apple iPhone 10,3
deviceVersion: DataTypes.STRING // e.g. Browser version or Android SDK
}, {
sequelize,
modelName: 'Device'
})
const { User } = sequelize.models
User.hasMany(Device)
Device.belongsTo(User)
return Device
}

View File

@ -55,16 +55,16 @@ module.exports = (sequelize) => {
MediaProgress.addHook('afterFind', findResult => {
if (!Array.isArray(findResult)) findResult = [findResult]
for (const instance of findResult) {
if (instance.mediaItemType === 'book' && instance.book !== undefined) {
instance.mediaItem = instance.book
} else if (instance.mediaItemType === 'podcastEpisode' && instance.podcastEpisode !== undefined) {
instance.mediaItem = instance.podcastEpisode
if (instance.mediaItemType === 'book' && instance.Book !== undefined) {
instance.MediaItem = instance.Book
} else if (instance.mediaItemType === 'podcastEpisode' && instance.PodcastEpisode !== undefined) {
instance.MediaItem = instance.PodcastEpisode
}
// To prevent mistakes:
delete instance.book
delete instance.dataValues.book
delete instance.podcastEpisode
delete instance.dataValues.podcastEpisode
delete instance.Book
delete instance.dataValues.Book
delete instance.PodcastEpisode
delete instance.dataValues.PodcastEpisode
}
})

View File

@ -0,0 +1,79 @@
const { DataTypes, Model } = require('sequelize')
const uppercaseFirst = str => `${str[0].toUpperCase()}${str.substr(1)}`
module.exports = (sequelize) => {
class PlaybackSession extends Model {
getMediaItem(options) {
if (!this.mediaItemType) return Promise.resolve(null)
const mixinMethodName = `get${uppercaseFirst(this.mediaItemType)}`
return this[mixinMethodName](options)
}
}
PlaybackSession.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
mediaItemId: DataTypes.UUIDV4,
mediaItemType: DataTypes.STRING,
displayTitle: DataTypes.STRING,
displayAuthor: DataTypes.STRING,
duration: DataTypes.INTEGER,
playMethod: DataTypes.STRING,
mediaPlayer: DataTypes.STRING,
startTime: DataTypes.INTEGER,
currentTime: DataTypes.INTEGER,
timeListening: DataTypes.INTEGER,
serverVersion: DataTypes.STRING
}, {
sequelize,
modelName: 'PlaybackSession'
})
const { Book, PodcastEpisode, User, Device } = sequelize.models
Book.hasMany(PlaybackSession, {
foreignKey: 'mediaItemId',
constraints: false,
scope: {
mediaItemType: 'book'
}
})
PlaybackSession.belongsTo(Book, { foreignKey: 'mediaItemId', constraints: false })
PodcastEpisode.hasOne(PlaybackSession, {
foreignKey: 'mediaItemId',
constraints: false,
scope: {
mediaItemType: 'podcastEpisode'
}
})
PlaybackSession.belongsTo(PodcastEpisode, { foreignKey: 'mediaItemId', constraints: false })
PlaybackSession.addHook('afterFind', findResult => {
if (!Array.isArray(findResult)) findResult = [findResult]
for (const instance of findResult) {
if (instance.mediaItemType === 'book' && instance.Book !== undefined) {
instance.MediaItem = instance.Book
} else if (instance.mediaItemType === 'podcastEpisode' && instance.PodcastEpisode !== undefined) {
instance.MediaItem = instance.PodcastEpisode
}
// To prevent mistakes:
delete instance.Book
delete instance.dataValues.Book
delete instance.PodcastEpisode
delete instance.dataValues.PodcastEpisode
}
})
User.hasMany(PlaybackSession)
PlaybackSession.belongsTo(User)
Device.hasMany(PlaybackSession)
PlaybackSession.belongsTo(Device)
return PlaybackSession
}

27
server/models/Playlist.js Normal file
View File

@ -0,0 +1,27 @@
const { DataTypes, Model } = require('sequelize')
module.exports = (sequelize) => {
class Playlist extends Model { }
Playlist.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
name: DataTypes.STRING,
description: DataTypes.TEXT
}, {
sequelize,
modelName: 'Playlist'
})
const { Library, User } = sequelize.models
Library.hasMany(Playlist)
Playlist.belongsTo(Library)
User.hasMany(Playlist)
Playlist.belongsTo(User)
return Playlist
}

View File

@ -0,0 +1,67 @@
const { DataTypes, Model } = require('sequelize')
const uppercaseFirst = str => `${str[0].toUpperCase()}${str.substr(1)}`
module.exports = (sequelize) => {
class PlaylistMediaItem extends Model {
getMediaItem(options) {
if (!this.mediaItemType) return Promise.resolve(null)
const mixinMethodName = `get${uppercaseFirst(this.mediaItemType)}`
return this[mixinMethodName](options)
}
}
PlaylistMediaItem.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
mediaItemId: DataTypes.UUIDV4,
mediaItemType: DataTypes.STRING
}, {
sequelize,
modelName: 'PlaylistMediaItem'
})
const { Book, PodcastEpisode, Playlist } = sequelize.models
Book.hasMany(PlaylistMediaItem, {
foreignKey: 'mediaItemId',
constraints: false,
scope: {
mediaItemType: 'book'
}
})
PlaylistMediaItem.belongsTo(Book, { foreignKey: 'mediaItemId', constraints: false })
PodcastEpisode.hasOne(PlaylistMediaItem, {
foreignKey: 'mediaItemId',
constraints: false,
scope: {
mediaItemType: 'podcastEpisode'
}
})
PlaylistMediaItem.belongsTo(PodcastEpisode, { foreignKey: 'mediaItemId', constraints: false })
PlaylistMediaItem.addHook('afterFind', findResult => {
if (!Array.isArray(findResult)) findResult = [findResult]
for (const instance of findResult) {
if (instance.mediaItemType === 'book' && instance.Book !== undefined) {
instance.MediaItem = instance.Book
} else if (instance.mediaItemType === 'podcastEpisode' && instance.PodcastEpisode !== undefined) {
instance.MediaItem = instance.PodcastEpisode
}
// To prevent mistakes:
delete instance.Book
delete instance.dataValues.Book
delete instance.PodcastEpisode
delete instance.dataValues.PodcastEpisode
}
})
Playlist.hasMany(PlaylistMediaItem)
PlaylistMediaItem.belongsTo(Playlist)
return PlaylistMediaItem
}

View File

@ -10,6 +10,7 @@ module.exports = (sequelize) => {
primaryKey: true
},
username: DataTypes.STRING,
email: DataTypes.STRING,
pash: DataTypes.STRING,
type: DataTypes.STRING,
token: DataTypes.STRING,