2023-07-05 01:14:44 +02:00
|
|
|
const { DataTypes, Model } = require('sequelize')
|
|
|
|
|
|
|
|
const oldSeries = require('../objects/entities/Series')
|
|
|
|
|
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
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
static async getAllOldSeries() {
|
|
|
|
const series = await this.findAll()
|
|
|
|
return series.map(se => se.getOldSeries())
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
getOldSeries() {
|
|
|
|
return new oldSeries({
|
|
|
|
id: this.id,
|
|
|
|
name: this.name,
|
|
|
|
description: this.description,
|
|
|
|
libraryId: this.libraryId,
|
|
|
|
addedAt: this.createdAt.valueOf(),
|
|
|
|
updatedAt: this.updatedAt.valueOf()
|
|
|
|
})
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
static updateFromOld(oldSeries) {
|
|
|
|
const series = this.getFromOld(oldSeries)
|
|
|
|
return this.update(series, {
|
|
|
|
where: {
|
|
|
|
id: series.id
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
2023-08-16 23:38:48 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static createFromOld(oldSeries) {
|
|
|
|
const series = this.getFromOld(oldSeries)
|
|
|
|
return this.create(series)
|
|
|
|
}
|
|
|
|
|
|
|
|
static createBulkFromOld(oldSeriesObjs) {
|
|
|
|
const series = oldSeriesObjs.map(this.getFromOld)
|
|
|
|
return this.bulkCreate(series)
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
static getFromOld(oldSeries) {
|
|
|
|
return {
|
|
|
|
id: oldSeries.id,
|
|
|
|
name: oldSeries.name,
|
|
|
|
nameIgnorePrefix: oldSeries.nameIgnorePrefix,
|
|
|
|
description: oldSeries.description,
|
|
|
|
libraryId: oldSeries.libraryId
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
static removeById(seriesId) {
|
|
|
|
return this.destroy({
|
|
|
|
where: {
|
|
|
|
id: seriesId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-18 21:40:36 +02:00
|
|
|
/**
|
|
|
|
* Check if series exists
|
|
|
|
* @param {string} seriesId
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
static async checkExistsById(seriesId) {
|
|
|
|
return (await this.count({ where: { id: seriesId } })) > 0
|
|
|
|
}
|
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
/**
|
|
|
|
* Initialize model
|
|
|
|
* @param {import('../Database').sequelize} sequelize
|
|
|
|
*/
|
|
|
|
static init(sequelize) {
|
|
|
|
super.init({
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true
|
|
|
|
},
|
|
|
|
name: DataTypes.STRING,
|
|
|
|
nameIgnorePrefix: DataTypes.STRING,
|
|
|
|
description: DataTypes.TEXT
|
|
|
|
}, {
|
|
|
|
sequelize,
|
2023-08-19 00:08:34 +02:00
|
|
|
modelName: 'series',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [{
|
|
|
|
name: 'name',
|
|
|
|
collate: 'NOCASE'
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [{
|
|
|
|
name: 'nameIgnorePrefix',
|
|
|
|
collate: 'NOCASE'
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: ['libraryId']
|
|
|
|
}
|
|
|
|
]
|
2023-08-16 23:38:48 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const { library } = sequelize.models
|
|
|
|
library.hasMany(Series, {
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
Series.belongsTo(library)
|
|
|
|
}
|
|
|
|
}
|
2023-07-08 16:57:32 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
module.exports = Series
|