From 182527bfa88162a31b9908d1145ed81d7c3a542c Mon Sep 17 00:00:00 2001 From: CoffeeKnyte <67730400+CoffeeKnyte@users.noreply.github.com> Date: Tue, 30 Apr 2024 11:09:06 -0400 Subject: [PATCH 1/4] Update LibraryController.js used a lighter function to find total author count --- server/controllers/LibraryController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index 30c9c0d9..0a0fb4d4 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -610,7 +610,7 @@ class LibraryController { const bookStats = await libraryItemsBookFilters.getBookLibraryStats(req.library.id) const longestBooks = await libraryItemsBookFilters.getLongestBooks(req.library.id, 10) - stats.totalAuthors = authors.length + stats.totalAuthors = await authorFilters.getAuthorsTotalCount(req.library.id) stats.authorsWithCount = authors stats.totalGenres = genres.length stats.genresWithCount = genres From 95cdb23efbab63d3acccc28f0c891f8c3b3403f5 Mon Sep 17 00:00:00 2001 From: CoffeeKnyte <67730400+CoffeeKnyte@users.noreply.github.com> Date: Tue, 30 Apr 2024 11:14:55 -0400 Subject: [PATCH 2/4] split getAuthorsWithCount to 2 lighter functions getAuthorsWithCount - now only gets the top 10 authors (in that library) by number of books getAuthorsTotalCount - new function to only get total number of authors (in that library) --- server/utils/queries/authorFilters.js | 49 +++++++++++++++++---------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/server/utils/queries/authorFilters.js b/server/utils/queries/authorFilters.js index c6a08a61..e13158fe 100644 --- a/server/utils/queries/authorFilters.js +++ b/server/utils/queries/authorFilters.js @@ -2,35 +2,48 @@ const Sequelize = require('sequelize') const Database = require('../../Database') module.exports = { + /** + * Get authors total count + * @param {string} libraryId + * @returns {{id:string, name:string, count:number}} + */ + async getAuthorsTotalCount(libraryId) { + const authorsCount = await Database.authorModel.count({ + where: { + libraryId: libraryId + } + }); + return authorsCount; + }, + /** * Get authors with count of num books * @param {string} libraryId * @returns {{id:string, name:string, count:number}} */ async getAuthorsWithCount(libraryId) { - const authors = await Database.authorModel.findAll({ - where: [ - { - libraryId - }, - Sequelize.where(Sequelize.literal('count'), { - [Sequelize.Op.gt]: 0 - }) - ], + const authors = await Database.bookAuthorModel.findAll({ + include: [{ + model: Database.authorModel, + as: 'author', // Use the correct alias as defined in your associations + attributes: ['name'], + where: { + libraryId: libraryId + } + }], attributes: [ - 'id', - 'name', - [Sequelize.literal('(SELECT count(*) FROM bookAuthors ba WHERE ba.authorId = author.id)'), 'count'] + 'authorId', + [Sequelize.fn('COUNT', Sequelize.col('authorId')), 'count'] ], - order: [ - ['count', 'DESC'] - ] + group: ['authorId', 'author.id'], // Include 'author.id' to satisfy GROUP BY with JOIN + order: [[Sequelize.literal('count'), 'DESC']], + limit: 10 }) return authors.map(au => { return { - id: au.id, - name: au.name, - count: au.dataValues.count + id: au.authorId, + name: au.author.name, + count: au.get('count') // Use get method to access aliased attributes } }) }, From 7229cfce84ada9e9500523344cdd7a63cdf47d84 Mon Sep 17 00:00:00 2001 From: CoffeeKnyte <67730400+CoffeeKnyte@users.noreply.github.com> Date: Wed, 1 May 2024 07:20:48 -0400 Subject: [PATCH 3/4] Added limit 10 to getAuthorsWithCount() call As per nichwall's request --- server/controllers/LibraryController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index 0a0fb4d4..77b79ac0 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -605,7 +605,7 @@ class LibraryController { } if (req.library.isBook) { - const authors = await authorFilters.getAuthorsWithCount(req.library.id) + const authors = await authorFilters.getAuthorsWithCount(req.library.id, 10) const genres = await libraryItemsBookFilters.getGenresWithCount(req.library.id) const bookStats = await libraryItemsBookFilters.getBookLibraryStats(req.library.id) const longestBooks = await libraryItemsBookFilters.getLongestBooks(req.library.id, 10) From 5041f80cb031bc2006ef3f530eab859f42c90240 Mon Sep 17 00:00:00 2001 From: CoffeeKnyte <67730400+CoffeeKnyte@users.noreply.github.com> Date: Wed, 1 May 2024 07:24:42 -0400 Subject: [PATCH 4/4] Added limit variable to getAuthorsWithCount() - Clarified and updated the comments - added parameter "limit" to getAuthorsWithCount() - the limit is set to 10 when called from LibraryController.js - as per Nichwall's comments --- server/utils/queries/authorFilters.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/utils/queries/authorFilters.js b/server/utils/queries/authorFilters.js index e13158fe..e684232b 100644 --- a/server/utils/queries/authorFilters.js +++ b/server/utils/queries/authorFilters.js @@ -5,7 +5,7 @@ module.exports = { /** * Get authors total count * @param {string} libraryId - * @returns {{id:string, name:string, count:number}} + * @returns {number} count */ async getAuthorsTotalCount(libraryId) { const authorsCount = await Database.authorModel.count({ @@ -19,9 +19,10 @@ module.exports = { /** * Get authors with count of num books * @param {string} libraryId + * @param {number} limit * @returns {{id:string, name:string, count:number}} */ - async getAuthorsWithCount(libraryId) { + async getAuthorsWithCount(libraryId, limit) { const authors = await Database.bookAuthorModel.findAll({ include: [{ model: Database.authorModel, @@ -37,7 +38,7 @@ module.exports = { ], group: ['authorId', 'author.id'], // Include 'author.id' to satisfy GROUP BY with JOIN order: [[Sequelize.literal('count'), 'DESC']], - limit: 10 + limit: limit }) return authors.map(au => { return {