mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-05-13 06:51:29 +00:00
Add favorite property for items and associated filters.
This commit is contained in:
parent
6d3773a0b8
commit
a5999fb9df
14 changed files with 308 additions and 11 deletions
66
server/models/UserFavorite.js
Normal file
66
server/models/UserFavorite.js
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
const { DataTypes, Model } = require('sequelize')
|
||||
|
||||
class UserFavorite extends Model {
|
||||
constructor(values, options) {
|
||||
super(values, options)
|
||||
|
||||
/** @type {UUIDV4} */
|
||||
this.id
|
||||
/** @type {UUIDV4} */
|
||||
this.libraryItemId
|
||||
/** @type {UUIDV4} */
|
||||
this.userId
|
||||
}
|
||||
|
||||
static init(sequelize) {
|
||||
super.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true
|
||||
},
|
||||
libraryItemId: DataTypes.UUID,
|
||||
userId: DataTypes.UUID
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
modelName: 'userFavorite',
|
||||
indexes: [
|
||||
{
|
||||
fields: ['userId']
|
||||
},
|
||||
{
|
||||
fields: ['libraryItemId']
|
||||
},
|
||||
{
|
||||
unique: true,
|
||||
fields: ['libraryItemId', 'userId'],
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
const { libraryItem, user } = sequelize.models
|
||||
|
||||
libraryItem.hasMany(UserFavorite, {
|
||||
foreignKey: 'libraryItemId',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
UserFavorite.belongsTo(libraryItem, { foreignKey: 'libraryItemId' })
|
||||
|
||||
user.hasMany(UserFavorite, {
|
||||
foreignKey: 'userId',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
user.belongsToMany(libraryItem, {
|
||||
through: UserFavorite,
|
||||
foreignKey: 'userId',
|
||||
otherKey: 'libraryItemId',
|
||||
as: 'favorites'
|
||||
})
|
||||
UserFavorite.belongsTo(user, { foreignKey: 'userId' })
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UserFavorite
|
||||
Loading…
Add table
Add a link
Reference in a new issue