2023-07-05 01:14:44 +02:00
|
|
|
const { DataTypes, Model } = require('sequelize')
|
|
|
|
|
|
|
|
const oldAuthor = require('../objects/entities/Author')
|
|
|
|
|
2023-08-15 01:22:38 +02:00
|
|
|
class Author extends Model {
|
|
|
|
constructor(values, options) {
|
|
|
|
super(values, options)
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-15 01:22:38 +02:00
|
|
|
/** @type {UUIDV4} */
|
|
|
|
this.id
|
|
|
|
/** @type {string} */
|
|
|
|
this.name
|
|
|
|
/** @type {string} */
|
|
|
|
this.lastFirst
|
|
|
|
/** @type {string} */
|
|
|
|
this.asin
|
|
|
|
/** @type {string} */
|
|
|
|
this.description
|
|
|
|
/** @type {string} */
|
|
|
|
this.imagePath
|
|
|
|
/** @type {UUIDV4} */
|
|
|
|
this.libraryId
|
|
|
|
/** @type {Date} */
|
|
|
|
this.updatedAt
|
|
|
|
/** @type {Date} */
|
|
|
|
this.createdAt
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-15 01:22:38 +02:00
|
|
|
static async getOldAuthors() {
|
|
|
|
const authors = await this.findAll()
|
|
|
|
return authors.map(au => au.getOldAuthor())
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-15 01:22:38 +02:00
|
|
|
getOldAuthor() {
|
|
|
|
return new oldAuthor({
|
|
|
|
id: this.id,
|
|
|
|
asin: this.asin,
|
|
|
|
name: this.name,
|
|
|
|
description: this.description,
|
|
|
|
imagePath: this.imagePath,
|
|
|
|
libraryId: this.libraryId,
|
|
|
|
addedAt: this.createdAt.valueOf(),
|
|
|
|
updatedAt: this.updatedAt.valueOf()
|
|
|
|
})
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-15 01:22:38 +02:00
|
|
|
static updateFromOld(oldAuthor) {
|
|
|
|
const author = this.getFromOld(oldAuthor)
|
|
|
|
return this.update(author, {
|
|
|
|
where: {
|
|
|
|
id: author.id
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
2023-08-15 01:22:38 +02:00
|
|
|
})
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-15 01:22:38 +02:00
|
|
|
static createFromOld(oldAuthor) {
|
|
|
|
const author = this.getFromOld(oldAuthor)
|
|
|
|
return this.create(author)
|
|
|
|
}
|
|
|
|
|
|
|
|
static createBulkFromOld(oldAuthors) {
|
|
|
|
const authors = oldAuthors.map(this.getFromOld)
|
|
|
|
return this.bulkCreate(authors)
|
|
|
|
}
|
|
|
|
|
|
|
|
static getFromOld(oldAuthor) {
|
|
|
|
return {
|
|
|
|
id: oldAuthor.id,
|
|
|
|
name: oldAuthor.name,
|
|
|
|
lastFirst: oldAuthor.lastFirst,
|
|
|
|
asin: oldAuthor.asin,
|
|
|
|
description: oldAuthor.description,
|
|
|
|
imagePath: oldAuthor.imagePath,
|
|
|
|
libraryId: oldAuthor.libraryId
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 01:22:38 +02:00
|
|
|
static removeById(authorId) {
|
|
|
|
return this.destroy({
|
|
|
|
where: {
|
|
|
|
id: authorId
|
2023-08-14 00:45:53 +02:00
|
|
|
}
|
2023-08-15 01:22:38 +02:00
|
|
|
})
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-18 21:40:36 +02:00
|
|
|
/**
|
|
|
|
* Check if author exists
|
|
|
|
* @param {string} authorId
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
static async checkExistsById(authorId) {
|
|
|
|
return (await this.count({ where: { id: authorId } })) > 0
|
|
|
|
}
|
|
|
|
|
2023-08-15 01:22:38 +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,
|
|
|
|
lastFirst: DataTypes.STRING,
|
|
|
|
asin: DataTypes.STRING,
|
|
|
|
description: DataTypes.TEXT,
|
|
|
|
imagePath: DataTypes.STRING
|
|
|
|
}, {
|
|
|
|
sequelize,
|
|
|
|
modelName: 'author',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [{
|
|
|
|
name: 'name',
|
|
|
|
collate: 'NOCASE'
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [{
|
|
|
|
name: 'lastFirst',
|
|
|
|
collate: 'NOCASE'
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: ['libraryId']
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2023-07-08 16:57:32 +02:00
|
|
|
|
2023-08-15 01:22:38 +02:00
|
|
|
const { library } = sequelize.models
|
|
|
|
library.hasMany(Author, {
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
Author.belongsTo(library)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = Author
|