2023-07-05 01:14:44 +02:00
|
|
|
const { DataTypes, Model } = require('sequelize')
|
|
|
|
|
|
|
|
const oldAuthor = require('../objects/entities/Author')
|
|
|
|
|
|
|
|
module.exports = (sequelize) => {
|
|
|
|
class Author extends Model {
|
|
|
|
static async getOldAuthors() {
|
|
|
|
const authors = await this.findAll()
|
|
|
|
return authors.map(au => au.getOldAuthor())
|
|
|
|
}
|
|
|
|
|
|
|
|
getOldAuthor() {
|
|
|
|
return new oldAuthor({
|
|
|
|
id: this.id,
|
|
|
|
asin: this.asin,
|
|
|
|
name: this.name,
|
|
|
|
description: this.description,
|
|
|
|
imagePath: this.imagePath,
|
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(oldAuthor) {
|
|
|
|
const author = this.getFromOld(oldAuthor)
|
|
|
|
return this.update(author, {
|
|
|
|
where: {
|
|
|
|
id: author.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-07-29 01:03:31 +02:00
|
|
|
lastFirst: oldAuthor.lastFirst,
|
2023-07-05 01:14:44 +02:00
|
|
|
asin: oldAuthor.asin,
|
|
|
|
description: oldAuthor.description,
|
2023-07-08 17:07:57 +02:00
|
|
|
imagePath: oldAuthor.imagePath,
|
|
|
|
libraryId: oldAuthor.libraryId
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static removeById(authorId) {
|
|
|
|
return this.destroy({
|
|
|
|
where: {
|
|
|
|
id: authorId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Author.init({
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true
|
|
|
|
},
|
|
|
|
name: DataTypes.STRING,
|
2023-07-29 01:03:31 +02:00
|
|
|
lastFirst: DataTypes.STRING,
|
2023-07-05 01:14:44 +02:00
|
|
|
asin: DataTypes.STRING,
|
|
|
|
description: DataTypes.TEXT,
|
|
|
|
imagePath: DataTypes.STRING
|
|
|
|
}, {
|
|
|
|
sequelize,
|
|
|
|
modelName: 'author'
|
|
|
|
})
|
|
|
|
|
2023-07-08 16:57:32 +02:00
|
|
|
const { library } = sequelize.models
|
2023-07-09 18:39:15 +02:00
|
|
|
library.hasMany(Author, {
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
2023-07-08 16:57:32 +02:00
|
|
|
Author.belongsTo(library)
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
return Author
|
|
|
|
}
|