audiobookshelf/server/models/UserBookRating.js
2025-06-28 08:34:04 +02:00

53 lines
1 KiB
JavaScript

const { DataTypes, Model } = require('sequelize')
class UserBookRating extends Model {
static init(sequelize) {
super.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
userId: {
type: DataTypes.STRING,
allowNull: false
},
bookId: {
type: DataTypes.STRING,
allowNull: false
},
rating: {
type: DataTypes.FLOAT,
allowNull: false
}
},
{
sequelize,
modelName: 'userBookRating',
indexes: [
{
unique: true,
fields: ['userId', 'bookId']
}
]
}
)
const { user, book } = sequelize.models
user.hasMany(UserBookRating, {
foreignKey: 'userId'
})
this.belongsTo(user, { foreignKey: 'userId' })
book.hasMany(this, {
foreignKey: 'bookId'
})
this.belongsTo(book, { foreignKey: 'bookId' })
}
}
module.exports = UserBookRating