mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-12-05 06:26:26 +01:00
Fetch podcast episodes in a separate query in LibraryItem.findAllExpandedWhere
This commit is contained in:
1 parent
f5a40b1ece
commit
b7ead7a316
1 file changed
+27
-5
@@ -148,7 +148,7 @@ class LibraryItem extends Model {
|
||||
* @returns {Promise<LibraryItemExpanded[]>}
|
||||
*/
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user