2023-07-05 01:14:44 +02:00
|
|
|
const { DataTypes, Model } = require('sequelize')
|
|
|
|
|
|
|
|
const oldSeries = require('../objects/entities/Series')
|
|
|
|
|
|
|
|
module.exports = (sequelize) => {
|
|
|
|
class Series extends Model {
|
|
|
|
static async getAllOldSeries() {
|
|
|
|
const series = await this.findAll()
|
|
|
|
return series.map(se => se.getOldSeries())
|
|
|
|
}
|
|
|
|
|
|
|
|
getOldSeries() {
|
|
|
|
return new oldSeries({
|
|
|
|
id: this.id,
|
|
|
|
name: this.name,
|
|
|
|
description: this.description,
|
2023-07-08 17:07:57 +02:00
|
|
|
libraryId: this.libraryId,
|
2023-07-05 01:14:44 +02:00
|
|
|
addedAt: this.createdAt.valueOf(),
|
|
|
|
updatedAt: this.updatedAt.valueOf()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static updateFromOld(oldSeries) {
|
|
|
|
const series = this.getFromOld(oldSeries)
|
|
|
|
return this.update(series, {
|
|
|
|
where: {
|
|
|
|
id: series.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
static getFromOld(oldSeries) {
|
|
|
|
return {
|
|
|
|
id: oldSeries.id,
|
|
|
|
name: oldSeries.name,
|
2023-07-29 01:03:31 +02:00
|
|
|
nameIgnorePrefix: oldSeries.nameIgnorePrefix,
|
2023-07-08 17:07:57 +02:00
|
|
|
description: oldSeries.description,
|
|
|
|
libraryId: oldSeries.libraryId
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static removeById(seriesId) {
|
|
|
|
return this.destroy({
|
|
|
|
where: {
|
|
|
|
id: seriesId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Series.init({
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true
|
|
|
|
},
|
|
|
|
name: DataTypes.STRING,
|
2023-07-29 01:03:31 +02:00
|
|
|
nameIgnorePrefix: DataTypes.STRING,
|
2023-07-05 01:14:44 +02:00
|
|
|
description: DataTypes.TEXT
|
|
|
|
}, {
|
|
|
|
sequelize,
|
|
|
|
modelName: 'series'
|
|
|
|
})
|
|
|
|
|
2023-07-08 16:57:32 +02:00
|
|
|
const { library } = sequelize.models
|
2023-07-09 18:39:15 +02:00
|
|
|
library.hasMany(Series, {
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
2023-07-08 16:57:32 +02:00
|
|
|
Series.belongsTo(library)
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
return Series
|
|
|
|
}
|