diff --git a/server/models/LibraryItem.js b/server/models/LibraryItem.js index 4876d52e6..7dcbb7ef9 100644 --- a/server/models/LibraryItem.js +++ b/server/models/LibraryItem.js @@ -148,7 +148,7 @@ class LibraryItem extends Model { * @returns {Promise} */ static async findAllExpandedWhere(where = null) { - return this.findAll({ + const libraryItems = await this.findAll({ where, include: [ { @@ -169,10 +169,7 @@ class LibraryItem extends Model { ] }, { - model: this.sequelize.models.podcast, - include: { - model: this.sequelize.models.podcastEpisode - } + model: this.sequelize.models.podcast } ], order: [ @@ -181,6 +178,31 @@ class LibraryItem extends Model { [this.sequelize.models.book, this.sequelize.models.series, 'bookSeries', 'createdAt', 'ASC'] ] }) + + // Fetch podcast episodes separately to avoid repeating libraryItem.libraryFiles on every episode row in SQLite + const podcasts = libraryItems.filter((li) => li.mediaType === 'podcast' && li.media).map((li) => li.media) + if (podcasts.length) { + const podcastIds = podcasts.map((p) => p.id) + const episodes = await this.sequelize.models.podcastEpisode.findAll({ + where: { podcastId: podcastIds } + }) + const episodesByPodcastId = new Map() + for (const episode of episodes) { + let list = episodesByPodcastId.get(episode.podcastId) + if (!list) { + list = [] + episodesByPodcastId.set(episode.podcastId, list) + } + list.push(episode) + } + for (const podcast of podcasts) { + const podcastEpisodes = episodesByPodcastId.get(podcast.id) || [] + podcast.podcastEpisodes = podcastEpisodes + podcast.dataValues.podcastEpisodes = podcastEpisodes + } + } + + return libraryItems } /**