From 456bb87a008e1f19dc9ed16c2990babff851ab84 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 30 Dec 2023 12:12:48 -0600 Subject: [PATCH] Update:Find one library item endpoint sequelize query split into two queries to improve performance #2073 #2075 --- server/models/LibraryItem.js | 63 ++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/server/models/LibraryItem.js b/server/models/LibraryItem.js index b6f2f285..ffd2f9c0 100644 --- a/server/models/LibraryItem.js +++ b/server/models/LibraryItem.js @@ -419,39 +419,40 @@ class LibraryItem extends Model { */ static async getOldById(libraryItemId) { if (!libraryItemId) return null - const libraryItem = await this.findByPk(libraryItemId, { - include: [ - { - model: this.sequelize.models.book, - include: [ - { - model: this.sequelize.models.author, - through: { - attributes: [] - } - }, - { - model: this.sequelize.models.series, - through: { - attributes: ['sequence'] - } + + const libraryItem = await this.findByPk(libraryItemId) + + if (libraryItem.mediaType === 'podcast') { + libraryItem.media = await libraryItem.getMedia({ + include: [ + { + model: this.sequelize.models.podcastEpisode + } + ] + }) + } else { + libraryItem.media = await libraryItem.getMedia({ + include: [ + { + model: this.sequelize.models.author, + through: { + attributes: [] } - ] - }, - { - model: this.sequelize.models.podcast, - include: [ - { - model: this.sequelize.models.podcastEpisode + }, + { + model: this.sequelize.models.series, + through: { + attributes: ['sequence'] } - ] - } - ], - order: [ - [this.sequelize.models.book, this.sequelize.models.author, this.sequelize.models.bookAuthor, 'createdAt', 'ASC'], - [this.sequelize.models.book, this.sequelize.models.series, 'bookSeries', 'createdAt', 'ASC'] - ] - }) + } + ], + order: [ + [this.sequelize.models.author, this.sequelize.models.bookAuthor, 'createdAt', 'ASC'], + [this.sequelize.models.series, 'bookSeries', 'createdAt', 'ASC'] + ] + }) + } + if (!libraryItem) return null return this.getOldLibraryItem(libraryItem) }