mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	Update libraryItem model to include libraryId
This commit is contained in:
		
							parent
							
								
									d745e6b656
								
							
						
					
					
						commit
						633e83a4ab
					
				
							
								
								
									
										28
									
								
								server/controllers2/library.controller.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								server/controllers2/library.controller.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,28 @@
 | 
			
		||||
const libraryDb = require('../db/library.db')
 | 
			
		||||
const itemDb = require('../db/item.db')
 | 
			
		||||
 | 
			
		||||
const getAllLibraries = async (req, res) => {
 | 
			
		||||
  const libraries = await libraryDb.getAllLibraries()
 | 
			
		||||
  res.json({
 | 
			
		||||
    libraries
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getLibrary = async (req, res) => {
 | 
			
		||||
  const library = await libraryDb.getLibrary(req.params.id)
 | 
			
		||||
  if (!library) return res.sendStatus(404)
 | 
			
		||||
  res.json(library)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getLibraryItems = async (req, res) => {
 | 
			
		||||
  const libraryItems = await itemDb.getLibraryItemsForLibrary(req.params.id)
 | 
			
		||||
  res.json({
 | 
			
		||||
    libraryItems
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  getAllLibraries,
 | 
			
		||||
  getLibrary,
 | 
			
		||||
  getLibraryItems
 | 
			
		||||
}
 | 
			
		||||
@ -254,9 +254,92 @@ const getLibraryItemExpanded = (libraryItemId) => {
 | 
			
		||||
        model: Database.models.libraryFile,
 | 
			
		||||
        include: 'fileMetadata'
 | 
			
		||||
      },
 | 
			
		||||
      'libraryFolder',
 | 
			
		||||
      'library'
 | 
			
		||||
    ]
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getLibraryItemsForLibrary = async (libraryId) => {
 | 
			
		||||
  return Database.models.libraryItem.findAll({
 | 
			
		||||
    where: {
 | 
			
		||||
      libraryId
 | 
			
		||||
    },
 | 
			
		||||
    limit: 50,
 | 
			
		||||
    order: [
 | 
			
		||||
      [Database.models.book, 'title', 'DESC'],
 | 
			
		||||
      [Database.models.podcast, 'title', 'DESC']
 | 
			
		||||
    ],
 | 
			
		||||
    include: [
 | 
			
		||||
      {
 | 
			
		||||
        model: Database.models.libraryFolder,
 | 
			
		||||
        include: 'library'
 | 
			
		||||
        model: Database.models.book,
 | 
			
		||||
        attributes: [
 | 
			
		||||
          'id', 'title', 'subtitle', 'publishedYear', 'publishedDate', 'publisher', 'description', 'isbn', 'asin', 'language', 'explicit',
 | 
			
		||||
          [Sequelize.literal('(SELECT COUNT(*) FROM "audioTracks" WHERE "audioTracks"."mediaItemId" = book.id)'), 'numAudioTracks'],
 | 
			
		||||
          [Sequelize.literal('(SELECT COUNT(*) FROM "bookChapters" WHERE "bookChapters"."bookId" = book.id)'), 'numChapters']
 | 
			
		||||
        ],
 | 
			
		||||
        include: [
 | 
			
		||||
          {
 | 
			
		||||
            model: Database.models.genre,
 | 
			
		||||
            attributes: ['id', 'name'],
 | 
			
		||||
            through: {
 | 
			
		||||
              attributes: []
 | 
			
		||||
            }
 | 
			
		||||
          },
 | 
			
		||||
          {
 | 
			
		||||
            model: Database.models.tag,
 | 
			
		||||
            attributes: ['id', 'name'],
 | 
			
		||||
            through: {
 | 
			
		||||
              attributes: []
 | 
			
		||||
            }
 | 
			
		||||
          },
 | 
			
		||||
          {
 | 
			
		||||
            model: Database.models.person,
 | 
			
		||||
            as: 'authors',
 | 
			
		||||
            attributes: ['id', 'name'],
 | 
			
		||||
            through: {
 | 
			
		||||
              attributes: []
 | 
			
		||||
            }
 | 
			
		||||
          },
 | 
			
		||||
          {
 | 
			
		||||
            model: Database.models.person,
 | 
			
		||||
            as: 'narrators',
 | 
			
		||||
            attributes: ['id', 'name'],
 | 
			
		||||
            through: {
 | 
			
		||||
              attributes: []
 | 
			
		||||
            }
 | 
			
		||||
          },
 | 
			
		||||
          {
 | 
			
		||||
            model: Database.models.series,
 | 
			
		||||
            attributes: ['id', 'name'],
 | 
			
		||||
            through: {
 | 
			
		||||
              attributes: ['sequence']
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
        ]
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        model: Database.models.podcast,
 | 
			
		||||
        attributes: [
 | 
			
		||||
          'id', 'title', 'author', 'releaseDate', 'feedURL', 'imageURL', 'description', 'itunesPageURL', 'itunesId', 'itunesArtistId', 'language', 'podcastType', 'explicit', 'autoDownloadEpisodes',
 | 
			
		||||
          [Sequelize.literal('(SELECT COUNT(*) FROM "podcastEpisodes" WHERE "podcastEpisodes"."podcastId" = podcast.id)'), 'numPodcastEpisodes']
 | 
			
		||||
        ],
 | 
			
		||||
        include: [
 | 
			
		||||
          {
 | 
			
		||||
            model: Database.models.genre,
 | 
			
		||||
            attributes: ['id', 'name'],
 | 
			
		||||
            through: {
 | 
			
		||||
              attributes: []
 | 
			
		||||
            }
 | 
			
		||||
          },
 | 
			
		||||
          {
 | 
			
		||||
            model: Database.models.tag,
 | 
			
		||||
            attributes: ['id', 'name'],
 | 
			
		||||
            through: {
 | 
			
		||||
              attributes: []
 | 
			
		||||
            }
 | 
			
		||||
          },
 | 
			
		||||
        ]
 | 
			
		||||
      }
 | 
			
		||||
    ]
 | 
			
		||||
  })
 | 
			
		||||
@ -265,5 +348,6 @@ const getLibraryItemExpanded = (libraryItemId) => {
 | 
			
		||||
module.exports = {
 | 
			
		||||
  getLibraryItemMinified,
 | 
			
		||||
  getLibraryItemFull,
 | 
			
		||||
  getLibraryItemExpanded
 | 
			
		||||
  getLibraryItemExpanded,
 | 
			
		||||
  getLibraryItemsForLibrary
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								server/db/library.db.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								server/db/library.db.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
const Database = require('../Database')
 | 
			
		||||
 | 
			
		||||
const getAllLibraries = () => {
 | 
			
		||||
  return Database.models.library.findAll({
 | 
			
		||||
    include: {
 | 
			
		||||
      model: Database.models.librarySetting,
 | 
			
		||||
      attributes: ['key', 'value']
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getLibrary = (libraryId) => {
 | 
			
		||||
  return Database.models.library.findByPk(libraryId, {
 | 
			
		||||
    include: {
 | 
			
		||||
      model: Database.models.librarySetting,
 | 
			
		||||
      attributes: ['key', 'value']
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  getAllLibraries,
 | 
			
		||||
  getLibrary
 | 
			
		||||
}
 | 
			
		||||
@ -33,7 +33,10 @@ module.exports = (sequelize) => {
 | 
			
		||||
    modelName: 'libraryItem'
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  const { libraryFolder, book, podcast } = sequelize.models
 | 
			
		||||
  const { library, libraryFolder, book, podcast } = sequelize.models
 | 
			
		||||
  library.hasMany(LibraryItem)
 | 
			
		||||
  LibraryItem.belongsTo(library)
 | 
			
		||||
 | 
			
		||||
  libraryFolder.hasMany(LibraryItem)
 | 
			
		||||
  LibraryItem.belongsTo(libraryFolder)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,8 +1,10 @@
 | 
			
		||||
const express = require('express')
 | 
			
		||||
const items = require('./items')
 | 
			
		||||
const libraries = require('./libraries')
 | 
			
		||||
 | 
			
		||||
const router = express.Router()
 | 
			
		||||
 | 
			
		||||
router.use('/items', items)
 | 
			
		||||
router.use('/libraries', libraries)
 | 
			
		||||
 | 
			
		||||
module.exports = router
 | 
			
		||||
							
								
								
									
										10
									
								
								server/routes/libraries.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								server/routes/libraries.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
const express = require('express')
 | 
			
		||||
const LibraryController = require('../controllers2/library.controller')
 | 
			
		||||
 | 
			
		||||
const router = express.Router()
 | 
			
		||||
 | 
			
		||||
router.get('/', LibraryController.getAllLibraries)
 | 
			
		||||
router.get('/:id', LibraryController.getLibrary)
 | 
			
		||||
router.get('/:id/items', LibraryController.getLibraryItems)
 | 
			
		||||
 | 
			
		||||
module.exports = router
 | 
			
		||||
@ -601,6 +601,11 @@ function migrateLibraryItems(oldLibraryItems) {
 | 
			
		||||
      Logger.error(`[dbMigration] migrateLibraryItems: Old library folder id not found "${oldLibraryItem.folderId}"`)
 | 
			
		||||
      continue
 | 
			
		||||
    }
 | 
			
		||||
    const libraryId = oldDbIdMap.libraries[oldLibraryItem.libraryId]
 | 
			
		||||
    if (!libraryId) {
 | 
			
		||||
      Logger.error(`[dbMigration] migrateLibraryItems: Old library id not found "${oldLibraryItem.libraryId}"`)
 | 
			
		||||
      continue
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //
 | 
			
		||||
    // Migrate LibraryItem
 | 
			
		||||
@ -622,6 +627,7 @@ function migrateLibraryItems(oldLibraryItems) {
 | 
			
		||||
      lastScanVersion: oldLibraryItem.scanVersion,
 | 
			
		||||
      createdAt: oldLibraryItem.addedAt,
 | 
			
		||||
      updatedAt: oldLibraryItem.updatedAt,
 | 
			
		||||
      libraryId,
 | 
			
		||||
      libraryFolderId
 | 
			
		||||
    }
 | 
			
		||||
    oldDbIdMap.libraryItems[oldLibraryItem.id] = LibraryItem.id
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user