From 7afda1295b8d990d5b1dc2ca9c500c73a5999d7e Mon Sep 17 00:00:00 2001 From: advplyr Date: Mon, 14 Aug 2023 18:22:38 -0500 Subject: [PATCH] Update Author model to define types --- server/Database.js | 2 +- server/models/Author.js | 204 ++++++++++++++----------- server/utils/queries/libraryFilters.js | 2 +- 3 files changed, 117 insertions(+), 91 deletions(-) diff --git a/server/Database.js b/server/Database.js index 3c5f0461..7dc4f5aa 100644 --- a/server/Database.js +++ b/server/Database.js @@ -102,7 +102,7 @@ class Database { require('./models/MediaProgress')(this.sequelize) require('./models/Series')(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/Collection')(this.sequelize) require('./models/CollectionBook')(this.sequelize) diff --git a/server/models/Author.js b/server/models/Author.js index 58a4a870..88dc3eea 100644 --- a/server/models/Author.js +++ b/server/models/Author.js @@ -2,104 +2,130 @@ 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()) - } +class Author extends Model { + constructor(values, options) { + super(values, options) - 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() - }) - } + /** @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 + } - static updateFromOld(oldAuthor) { - const author = this.getFromOld(oldAuthor) - return this.update(author, { - where: { - id: author.id - } - }) - } + static async getOldAuthors() { + const authors = await this.findAll() + return authors.map(au => au.getOldAuthor()) + } - static createFromOld(oldAuthor) { - const author = this.getFromOld(oldAuthor) - return this.create(author) - } + 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() + }) + } - 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 + static updateFromOld(oldAuthor) { + const author = this.getFromOld(oldAuthor) + return this.update(author, { + where: { + id: author.id } - } + }) + } - static removeById(authorId) { - return this.destroy({ - where: { - id: authorId - } - }) + 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 } } - Author.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'] + static removeById(authorId) { + return this.destroy({ + where: { + id: authorId } - ] - }) + }) + } - const { library } = sequelize.models - library.hasMany(Author, { - onDelete: 'CASCADE' - }) - Author.belongsTo(library) + /** + * 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'] + } + ] + }) - return Author -} \ No newline at end of file + const { library } = sequelize.models + library.hasMany(Author, { + onDelete: 'CASCADE' + }) + Author.belongsTo(library) + } +} +module.exports = Author diff --git a/server/utils/queries/libraryFilters.js b/server/utils/queries/libraryFilters.js index 492cbe76..27e1b933 100644 --- a/server/utils/queries/libraryFilters.js +++ b/server/utils/queries/libraryFilters.js @@ -491,4 +491,4 @@ module.exports = { Logger.debug(`Loaded filterdata in ${((Date.now() - start) / 1000).toFixed(2)}s`) return data } -} \ No newline at end of file +}