From 527498bb1e960fac33dcc5476e7f1521208ce5a9 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Sun, 31 Jul 2022 15:55:27 +0700 Subject: [PATCH] perf: list albums db query --- controllers/albumsController.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/controllers/albumsController.js b/controllers/albumsController.js index 2434830..c4dfba1 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -121,6 +121,16 @@ self.list = async (req, res) => { // Base result object const result = { success: true, albums: [], albumsPerPage, count: 0, homeDomain } + // If simple listing (for dashboard sidebar) + if (simple) { + result.albums = await utils.db.table('albums') + .where(filter) + .select('id', 'name') + result.count = result.albums.length + + return res.json(result) + } + // Query albums count for pagination result.count = await utils.db.table('albums') .where(filter) @@ -130,16 +140,6 @@ self.list = async (req, res) => { return res.json(result) } - const fields = ['id', 'name'] - - if (simple) { - result.albums = await utils.db.table('albums') - .where(filter) - .select(fields) - - return res.json(result) - } - let offset = req.path_parameters && Number(req.path_parameters.page) if (isNaN(offset)) { offset = 0 @@ -147,7 +147,7 @@ self.list = async (req, res) => { offset = Math.max(0, Math.ceil(result.count / albumsPerPage) + offset) } - fields.push('identifier', 'enabled', 'timestamp', 'editedAt', 'zipGeneratedAt', 'download', 'public', 'description') + const fields = ['id', 'name', 'identifier', 'enabled', 'timestamp', 'editedAt', 'zipGeneratedAt', 'download', 'public', 'description'] if (all) { fields.push('userid') }