2024-12-15 18:44:07 +01:00
|
|
|
const { DataTypes, Model, where, fn, col, literal } = require('sequelize')
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2024-09-01 22:08:56 +02:00
|
|
|
const { getTitlePrefixAtEnd } = require('../utils/index')
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
class Series extends Model {
|
|
|
|
constructor(values, options) {
|
|
|
|
super(values, options)
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
/** @type {UUIDV4} */
|
|
|
|
this.id
|
|
|
|
/** @type {string} */
|
|
|
|
this.name
|
|
|
|
/** @type {string} */
|
|
|
|
this.nameIgnorePrefix
|
|
|
|
/** @type {string} */
|
|
|
|
this.description
|
|
|
|
/** @type {UUIDV4} */
|
|
|
|
this.libraryId
|
|
|
|
/** @type {Date} */
|
|
|
|
this.createdAt
|
|
|
|
/** @type {Date} */
|
|
|
|
this.updatedAt
|
2024-12-15 18:44:07 +01:00
|
|
|
|
|
|
|
// Expanded properties
|
|
|
|
|
|
|
|
/** @type {import('./Book').BookExpandedWithLibraryItem[]} - only set when expanded */
|
|
|
|
this.books
|
2023-08-16 23:38:48 +02:00
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-18 21:40:36 +02:00
|
|
|
/**
|
|
|
|
* Check if series exists
|
2024-05-29 00:24:02 +02:00
|
|
|
* @param {string} seriesId
|
2023-08-18 21:40:36 +02:00
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
static async checkExistsById(seriesId) {
|
|
|
|
return (await this.count({ where: { id: seriesId } })) > 0
|
|
|
|
}
|
|
|
|
|
2023-09-03 17:49:02 +02:00
|
|
|
/**
|
2024-09-01 22:08:56 +02:00
|
|
|
* Get series by name and libraryId. name case insensitive
|
2024-05-29 00:24:02 +02:00
|
|
|
*
|
|
|
|
* @param {string} seriesName
|
|
|
|
* @param {string} libraryId
|
2024-09-01 22:08:56 +02:00
|
|
|
* @returns {Promise<Series>}
|
2023-09-03 17:49:02 +02:00
|
|
|
*/
|
2024-09-01 22:08:56 +02:00
|
|
|
static async getByNameAndLibrary(seriesName, libraryId) {
|
2024-09-13 19:09:32 +02:00
|
|
|
return this.findOne({
|
|
|
|
where: [
|
2024-10-08 21:52:26 +02:00
|
|
|
where(fn('lower', col('name')), seriesName.toLowerCase()),
|
2024-09-13 19:09:32 +02:00
|
|
|
{
|
|
|
|
libraryId
|
2024-09-01 22:08:56 +02:00
|
|
|
}
|
2024-09-13 19:09:32 +02:00
|
|
|
]
|
|
|
|
})
|
2023-09-03 17:49:02 +02:00
|
|
|
}
|
|
|
|
|
2024-12-15 23:56:59 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} seriesId
|
|
|
|
* @returns {Promise<Series>}
|
|
|
|
*/
|
|
|
|
static async getExpandedById(seriesId) {
|
|
|
|
const series = await this.findByPk(seriesId)
|
|
|
|
if (!series) return null
|
|
|
|
series.books = await series.getBooksExpandedWithLibraryItem()
|
|
|
|
return series
|
|
|
|
}
|
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
/**
|
|
|
|
* Initialize model
|
2024-05-29 00:24:02 +02:00
|
|
|
* @param {import('../Database').sequelize} sequelize
|
2023-08-16 23:38:48 +02:00
|
|
|
*/
|
|
|
|
static init(sequelize) {
|
2024-05-29 00:24:02 +02:00
|
|
|
super.init(
|
|
|
|
{
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true
|
2023-08-19 00:08:34 +02:00
|
|
|
},
|
2024-05-29 00:24:02 +02:00
|
|
|
name: DataTypes.STRING,
|
|
|
|
nameIgnorePrefix: DataTypes.STRING,
|
|
|
|
description: DataTypes.TEXT
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sequelize,
|
|
|
|
modelName: 'series',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: 'name',
|
|
|
|
collate: 'NOCASE'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
// {
|
|
|
|
// fields: [{
|
|
|
|
// name: 'nameIgnorePrefix',
|
|
|
|
// collate: 'NOCASE'
|
|
|
|
// }]
|
|
|
|
// },
|
2024-09-14 01:55:48 +02:00
|
|
|
{
|
|
|
|
// unique constraint on name and libraryId
|
|
|
|
fields: ['name', 'libraryId'],
|
|
|
|
unique: true,
|
|
|
|
name: 'unique_series_name_per_library'
|
|
|
|
},
|
2024-05-29 00:24:02 +02:00
|
|
|
{
|
|
|
|
fields: ['libraryId']
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
2023-08-16 23:38:48 +02:00
|
|
|
|
|
|
|
const { library } = sequelize.models
|
|
|
|
library.hasMany(Series, {
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
Series.belongsTo(library)
|
|
|
|
}
|
2024-09-01 22:08:56 +02:00
|
|
|
|
2024-12-15 18:44:07 +01:00
|
|
|
/**
|
|
|
|
* Get all books in collection expanded with library item
|
|
|
|
*
|
|
|
|
* @returns {Promise<import('./Book').BookExpandedWithLibraryItem[]>}
|
|
|
|
*/
|
|
|
|
getBooksExpandedWithLibraryItem() {
|
|
|
|
return this.getBooks({
|
|
|
|
joinTableAttributes: ['sequence'],
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.libraryItem
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.author,
|
|
|
|
through: {
|
|
|
|
attributes: []
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.series,
|
|
|
|
through: {
|
|
|
|
attributes: ['sequence']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
order: [[literal('CAST(`bookSeries.sequence` AS FLOAT) ASC NULLS LAST')]]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-01 22:08:56 +02:00
|
|
|
toOldJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
name: this.name,
|
|
|
|
nameIgnorePrefix: getTitlePrefixAtEnd(this.name),
|
|
|
|
description: this.description,
|
|
|
|
addedAt: this.createdAt.valueOf(),
|
|
|
|
updatedAt: this.updatedAt.valueOf(),
|
|
|
|
libraryId: this.libraryId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSONMinimal(sequence) {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
name: this.name,
|
|
|
|
sequence
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 23:38:48 +02:00
|
|
|
}
|
2023-07-08 16:57:32 +02:00
|
|
|
|
2024-05-29 00:24:02 +02:00
|
|
|
module.exports = Series
|