mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-01-13 09:28:20 +01:00
Update Author model to define types
This commit is contained in:
parent
6d6e8613cf
commit
7afda1295b
@ -102,7 +102,7 @@ class Database {
|
|||||||
require('./models/MediaProgress')(this.sequelize)
|
require('./models/MediaProgress')(this.sequelize)
|
||||||
require('./models/Series')(this.sequelize)
|
require('./models/Series')(this.sequelize)
|
||||||
require('./models/BookSeries')(this.sequelize)
|
require('./models/BookSeries')(this.sequelize)
|
||||||
require('./models/Author')(this.sequelize)
|
require('./models/Author').init(this.sequelize)
|
||||||
require('./models/BookAuthor')(this.sequelize)
|
require('./models/BookAuthor')(this.sequelize)
|
||||||
require('./models/Collection')(this.sequelize)
|
require('./models/Collection')(this.sequelize)
|
||||||
require('./models/CollectionBook')(this.sequelize)
|
require('./models/CollectionBook')(this.sequelize)
|
||||||
|
@ -2,104 +2,130 @@ const { DataTypes, Model } = require('sequelize')
|
|||||||
|
|
||||||
const oldAuthor = require('../objects/entities/Author')
|
const oldAuthor = require('../objects/entities/Author')
|
||||||
|
|
||||||
module.exports = (sequelize) => {
|
class Author extends Model {
|
||||||
class Author extends Model {
|
constructor(values, options) {
|
||||||
static async getOldAuthors() {
|
super(values, options)
|
||||||
const authors = await this.findAll()
|
|
||||||
return authors.map(au => au.getOldAuthor())
|
|
||||||
}
|
|
||||||
|
|
||||||
getOldAuthor() {
|
/** @type {UUIDV4} */
|
||||||
return new oldAuthor({
|
this.id
|
||||||
id: this.id,
|
/** @type {string} */
|
||||||
asin: this.asin,
|
this.name
|
||||||
name: this.name,
|
/** @type {string} */
|
||||||
description: this.description,
|
this.lastFirst
|
||||||
imagePath: this.imagePath,
|
/** @type {string} */
|
||||||
libraryId: this.libraryId,
|
this.asin
|
||||||
addedAt: this.createdAt.valueOf(),
|
/** @type {string} */
|
||||||
updatedAt: this.updatedAt.valueOf()
|
this.description
|
||||||
})
|
/** @type {string} */
|
||||||
}
|
this.imagePath
|
||||||
|
/** @type {UUIDV4} */
|
||||||
|
this.libraryId
|
||||||
|
/** @type {Date} */
|
||||||
|
this.updatedAt
|
||||||
|
/** @type {Date} */
|
||||||
|
this.createdAt
|
||||||
|
}
|
||||||
|
|
||||||
static updateFromOld(oldAuthor) {
|
static async getOldAuthors() {
|
||||||
const author = this.getFromOld(oldAuthor)
|
const authors = await this.findAll()
|
||||||
return this.update(author, {
|
return authors.map(au => au.getOldAuthor())
|
||||||
where: {
|
}
|
||||||
id: author.id
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
static createFromOld(oldAuthor) {
|
getOldAuthor() {
|
||||||
const author = this.getFromOld(oldAuthor)
|
return new oldAuthor({
|
||||||
return this.create(author)
|
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()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
static createBulkFromOld(oldAuthors) {
|
static updateFromOld(oldAuthor) {
|
||||||
const authors = oldAuthors.map(this.getFromOld)
|
const author = this.getFromOld(oldAuthor)
|
||||||
return this.bulkCreate(authors)
|
return this.update(author, {
|
||||||
}
|
where: {
|
||||||
|
id: author.id
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
}
|
||||||
|
|
||||||
static removeById(authorId) {
|
static createFromOld(oldAuthor) {
|
||||||
return this.destroy({
|
const author = this.getFromOld(oldAuthor)
|
||||||
where: {
|
return this.create(author)
|
||||||
id: authorId
|
}
|
||||||
}
|
|
||||||
})
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Author.init({
|
static removeById(authorId) {
|
||||||
id: {
|
return this.destroy({
|
||||||
type: DataTypes.UUID,
|
where: {
|
||||||
defaultValue: DataTypes.UUIDV4,
|
id: authorId
|
||||||
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']
|
|
||||||
}
|
}
|
||||||
]
|
})
|
||||||
})
|
}
|
||||||
|
|
||||||
const { library } = sequelize.models
|
/**
|
||||||
library.hasMany(Author, {
|
* Initialize model
|
||||||
onDelete: 'CASCADE'
|
* @param {import('../Database').sequelize} sequelize
|
||||||
})
|
*/
|
||||||
Author.belongsTo(library)
|
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']
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
return Author
|
const { library } = sequelize.models
|
||||||
}
|
library.hasMany(Author, {
|
||||||
|
onDelete: 'CASCADE'
|
||||||
|
})
|
||||||
|
Author.belongsTo(library)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
module.exports = Author
|
||||||
|
@ -491,4 +491,4 @@ module.exports = {
|
|||||||
Logger.debug(`Loaded filterdata in ${((Date.now() - start) / 1000).toFixed(2)}s`)
|
Logger.debug(`Loaded filterdata in ${((Date.now() - start) / 1000).toFixed(2)}s`)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user