perf: list albums db query

This commit is contained in:
Bobby Wibowo 2022-07-31 15:55:27 +07:00
parent 285e79c5a7
commit 527498bb1e
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -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')
}