Slight findFiles optimization

This commit is contained in:
Bryan Ashby 2018-03-17 13:48:11 -06:00
parent 18284d8038
commit 66f444d4fb
1 changed files with 8 additions and 7 deletions

View File

@ -483,7 +483,7 @@ module.exports = class FileEntry {
sqlOrderBy = `ORDER BY avg_rating ${sqlOrderDir}`;
} else {
sql =
`SELECT DISTINCT f.file_id, f.${filter.sort}
`SELECT DISTINCT f.file_id
FROM file f`;
sqlOrderBy = getOrderByWithCast(`f.${filter.sort}`) + ' ' + sqlOrderDir;
@ -579,13 +579,14 @@ module.exports = class FileEntry {
sql += ';';
const matchingFileIds = [];
fileDb.each(sql, (err, fileId) => {
if(fileId) {
matchingFileIds.push(fileId.file_id);
fileDb.all(sql, (err, rows) => {
if(err) {
return cb(err);
}
}, err => {
return cb(err, matchingFileIds);
if(!rows || 0 === rows.length) {
return cb(null, []); // no matches
}
return cb(null, rows.map(r => r.file_id));
});
}