mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-04-16 01:16:24 +02:00
Update:Only load collections when needed
This commit is contained in:
parent
354e16e462
commit
5a9eed0a5a
@ -19,7 +19,6 @@ class Database {
|
|||||||
// TODO: below data should be loaded from the DB as needed
|
// TODO: below data should be loaded from the DB as needed
|
||||||
this.libraryItems = []
|
this.libraryItems = []
|
||||||
this.settings = []
|
this.settings = []
|
||||||
this.collections = []
|
|
||||||
this.playlists = []
|
this.playlists = []
|
||||||
this.authors = []
|
this.authors = []
|
||||||
this.series = []
|
this.series = []
|
||||||
@ -161,9 +160,6 @@ class Database {
|
|||||||
this.libraryItems = await this.models.libraryItem.loadAllLibraryItems()
|
this.libraryItems = await this.models.libraryItem.loadAllLibraryItems()
|
||||||
Logger.info(`[Database] Loaded ${this.libraryItems.length} library items`)
|
Logger.info(`[Database] Loaded ${this.libraryItems.length} library items`)
|
||||||
|
|
||||||
this.collections = await this.models.collection.getOldCollections()
|
|
||||||
Logger.info(`[Database] Loaded ${this.collections.length} collections`)
|
|
||||||
|
|
||||||
this.playlists = await this.models.playlist.getOldPlaylists()
|
this.playlists = await this.models.playlist.getOldPlaylists()
|
||||||
Logger.info(`[Database] Loaded ${this.playlists.length} playlists`)
|
Logger.info(`[Database] Loaded ${this.playlists.length} playlists`)
|
||||||
|
|
||||||
@ -280,7 +276,6 @@ class Database {
|
|||||||
await this.createBulkCollectionBooks(collectionBooks)
|
await this.createBulkCollectionBooks(collectionBooks)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.collections.push(oldCollection)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCollection(oldCollection) {
|
updateCollection(oldCollection) {
|
||||||
@ -302,7 +297,6 @@ class Database {
|
|||||||
async removeCollection(collectionId) {
|
async removeCollection(collectionId) {
|
||||||
if (!this.sequelize) return false
|
if (!this.sequelize) return false
|
||||||
await this.models.collection.removeById(collectionId)
|
await this.models.collection.removeById(collectionId)
|
||||||
this.collections = this.collections.filter(c => c.id !== collectionId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
createCollectionBook(collectionBook) {
|
createCollectionBook(collectionBook) {
|
||||||
|
@ -20,9 +20,10 @@ class CollectionController {
|
|||||||
res.json(jsonExpanded)
|
res.json(jsonExpanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
findAll(req, res) {
|
async findAll(req, res) {
|
||||||
|
const collections = await Database.models.collection.getOldCollections()
|
||||||
res.json({
|
res.json({
|
||||||
collections: Database.collections.map(c => c.toJSONExpanded(Database.libraryItems))
|
collections: collections.map(c => c.toJSONExpanded(Database.libraryItems))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,9 +161,9 @@ class CollectionController {
|
|||||||
res.json(collection.toJSONExpanded(Database.libraryItems))
|
res.json(collection.toJSONExpanded(Database.libraryItems))
|
||||||
}
|
}
|
||||||
|
|
||||||
middleware(req, res, next) {
|
async middleware(req, res, next) {
|
||||||
if (req.params.id) {
|
if (req.params.id) {
|
||||||
const collection = Database.collections.find(c => c.id === req.params.id)
|
const collection = await Database.models.collection.getById(req.params.id)
|
||||||
if (!collection) {
|
if (!collection) {
|
||||||
return res.status(404).send('Collection not found')
|
return res.status(404).send('Collection not found')
|
||||||
}
|
}
|
||||||
|
@ -167,10 +167,9 @@ class LibraryController {
|
|||||||
this.watcher.removeLibrary(library)
|
this.watcher.removeLibrary(library)
|
||||||
|
|
||||||
// Remove collections for library
|
// Remove collections for library
|
||||||
const collections = Database.collections.filter(c => c.libraryId === library.id)
|
const numCollectionsRemoved = await Database.models.collection.removeAllForLibrary(library.id)
|
||||||
for (const collection of collections) {
|
if (numCollectionsRemoved) {
|
||||||
Logger.info(`[Server] deleting collection "${collection.name}" for library "${library.name}"`)
|
Logger.info(`[Server] Removed ${numCollectionsRemoved} collections for library "${library.name}"`)
|
||||||
await Database.removeCollection(collection.id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove items in this library
|
// Remove items in this library
|
||||||
@ -527,7 +526,9 @@ class LibraryController {
|
|||||||
include: include.join(',')
|
include: include.join(',')
|
||||||
}
|
}
|
||||||
|
|
||||||
let collections = await Promise.all(Database.collections.filter(c => c.libraryId === req.library.id).map(async c => {
|
const collectionsForLibrary = await Database.models.collection.getAllForLibrary(req.library.id)
|
||||||
|
|
||||||
|
let collections = await Promise.all(collectionsForLibrary.map(async c => {
|
||||||
const expanded = c.toJSONExpanded(libraryItems, payload.minified)
|
const expanded = c.toJSONExpanded(libraryItems, payload.minified)
|
||||||
|
|
||||||
// If all books restricted to user in this collection then hide this collection
|
// If all books restricted to user in this collection then hide this collection
|
||||||
|
@ -200,7 +200,7 @@ class PlaylistController {
|
|||||||
|
|
||||||
// POST: api/playlists/collection/:collectionId
|
// POST: api/playlists/collection/:collectionId
|
||||||
async createFromCollection(req, res) {
|
async createFromCollection(req, res) {
|
||||||
let collection = Database.collections.find(c => c.id === req.params.collectionId)
|
let collection = await Database.models.collection.getById(req.params.collectionId)
|
||||||
if (!collection) {
|
if (!collection) {
|
||||||
return res.status(404).send('Collection not found')
|
return res.status(404).send('Collection not found')
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ class RSSFeedController {
|
|||||||
async openRSSFeedForCollection(req, res) {
|
async openRSSFeedForCollection(req, res) {
|
||||||
const options = req.body || {}
|
const options = req.body || {}
|
||||||
|
|
||||||
const collection = Database.collections.find(li => li.id === req.params.collectionId)
|
const collection = await Database.models.collection.getById(req.params.collectionId)
|
||||||
if (!collection) return res.sendStatus(404)
|
if (!collection) return res.sendStatus(404)
|
||||||
|
|
||||||
// Check request body options exist
|
// Check request body options exist
|
||||||
|
@ -10,9 +10,10 @@ const Feed = require('../objects/Feed')
|
|||||||
class RssFeedManager {
|
class RssFeedManager {
|
||||||
constructor() { }
|
constructor() { }
|
||||||
|
|
||||||
validateFeedEntity(feedObj) {
|
async validateFeedEntity(feedObj) {
|
||||||
if (feedObj.entityType === 'collection') {
|
if (feedObj.entityType === 'collection') {
|
||||||
if (!Database.collections.some(li => li.id === feedObj.entityId)) {
|
const collection = await Database.models.collection.getById(feedObj.entityId)
|
||||||
|
if (!collection) {
|
||||||
Logger.error(`[RssFeedManager] Removing feed "${feedObj.id}". Collection "${feedObj.entityId}" not found`)
|
Logger.error(`[RssFeedManager] Removing feed "${feedObj.id}". Collection "${feedObj.entityId}" not found`)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -42,7 +43,7 @@ class RssFeedManager {
|
|||||||
const feeds = await Database.models.feed.getOldFeeds()
|
const feeds = await Database.models.feed.getOldFeeds()
|
||||||
for (const feed of feeds) {
|
for (const feed of feeds) {
|
||||||
// Remove invalid feeds
|
// Remove invalid feeds
|
||||||
if (!this.validateFeedEntity(feed)) {
|
if (!await this.validateFeedEntity(feed)) {
|
||||||
await Database.removeFeed(feed.id)
|
await Database.removeFeed(feed.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,7 +102,7 @@ class RssFeedManager {
|
|||||||
await Database.updateFeed(feed)
|
await Database.updateFeed(feed)
|
||||||
}
|
}
|
||||||
} else if (feed.entityType === 'collection') {
|
} else if (feed.entityType === 'collection') {
|
||||||
const collection = Database.collections.find(c => c.id === feed.entityId)
|
const collection = await Database.models.collection.getById(feed.entityId)
|
||||||
if (collection) {
|
if (collection) {
|
||||||
const collectionExpanded = collection.toJSONExpanded(Database.libraryItems)
|
const collectionExpanded = collection.toJSONExpanded(Database.libraryItems)
|
||||||
|
|
||||||
|
@ -92,6 +92,73 @@ module.exports = (sequelize) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get collection by id
|
||||||
|
* @param {string} collectionId
|
||||||
|
* @returns {Promise<oldCollection|null>} returns null if not found
|
||||||
|
*/
|
||||||
|
static async getById(collectionId) {
|
||||||
|
if (!collectionId) return null
|
||||||
|
const collection = await this.findByPk(collectionId, {
|
||||||
|
include: {
|
||||||
|
model: sequelize.models.book,
|
||||||
|
include: sequelize.models.libraryItem
|
||||||
|
},
|
||||||
|
order: [[sequelize.models.book, sequelize.models.collectionBook, 'order', 'ASC']]
|
||||||
|
})
|
||||||
|
if (!collection) return null
|
||||||
|
return this.getOldCollection(collection)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all collections belonging to library
|
||||||
|
* @param {string} libraryId
|
||||||
|
* @returns {Promise<number>} number of collections destroyed
|
||||||
|
*/
|
||||||
|
static async removeAllForLibrary(libraryId) {
|
||||||
|
if (!libraryId) return 0
|
||||||
|
return this.destroy({
|
||||||
|
where: {
|
||||||
|
libraryId
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all collections for a library
|
||||||
|
* @param {string} libraryId
|
||||||
|
* @returns {Promise<oldCollection[]>}
|
||||||
|
*/
|
||||||
|
static async getAllForLibrary(libraryId) {
|
||||||
|
if (!libraryId) return []
|
||||||
|
const collections = await this.findAll({
|
||||||
|
where: {
|
||||||
|
libraryId
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
model: sequelize.models.book,
|
||||||
|
include: sequelize.models.libraryItem
|
||||||
|
},
|
||||||
|
order: [[sequelize.models.book, sequelize.models.collectionBook, 'order', 'ASC']]
|
||||||
|
})
|
||||||
|
return collections.map(c => this.getOldCollection(c))
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getAllForBook(bookId) {
|
||||||
|
const collections = await this.findAll({
|
||||||
|
include: {
|
||||||
|
model: sequelize.models.book,
|
||||||
|
where: {
|
||||||
|
id: bookId
|
||||||
|
},
|
||||||
|
required: true,
|
||||||
|
include: sequelize.models.libraryItem
|
||||||
|
},
|
||||||
|
order: [[sequelize.models.book, sequelize.models.collectionBook, 'order', 'ASC']]
|
||||||
|
})
|
||||||
|
return collections.map(c => this.getOldCollection(c))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Collection.init({
|
Collection.init({
|
||||||
|
@ -392,9 +392,8 @@ class ApiRouter {
|
|||||||
|
|
||||||
if (libraryItem.isBook) {
|
if (libraryItem.isBook) {
|
||||||
// remove book from collections
|
// remove book from collections
|
||||||
const collectionsWithBook = Database.collections.filter(c => c.books.includes(libraryItem.id))
|
const collectionsWithBook = await Database.models.collection.getAllForBook(libraryItem.media.id)
|
||||||
for (let i = 0; i < collectionsWithBook.length; i++) {
|
for (const collection of collectionsWithBook) {
|
||||||
const collection = collectionsWithBook[i]
|
|
||||||
collection.removeBook(libraryItem.id)
|
collection.removeBook(libraryItem.id)
|
||||||
await Database.removeCollectionBook(collection.id, libraryItem.media.id)
|
await Database.removeCollectionBook(collection.id, libraryItem.media.id)
|
||||||
SocketAuthority.emitter('collection_updated', collection.toJSONExpanded(Database.libraryItems))
|
SocketAuthority.emitter('collection_updated', collection.toJSONExpanded(Database.libraryItems))
|
||||||
|
Loading…
Reference in New Issue
Block a user