diff options
| author | Pitu <[email protected]> | 2020-06-25 01:35:52 +0900 |
|---|---|---|
| committer | Pitu <[email protected]> | 2020-06-25 01:35:52 +0900 |
| commit | b526d8803696161961ffb9eb912cb4b83a3c9eff (patch) | |
| tree | bad1cef433bfbafb41ecb4849a8fd46c12f60dbd /src/api/routes/albums/albumFullGET.js | |
| parent | Fix frontend registration display (diff) | |
| download | host.fuwn.me-b526d8803696161961ffb9eb912cb4b83a3c9eff.tar.xz host.fuwn.me-b526d8803696161961ffb9eb912cb4b83a3c9eff.zip | |
Optimize the queries fetching albums/files
Diffstat (limited to 'src/api/routes/albums/albumFullGET.js')
| -rw-r--r-- | src/api/routes/albums/albumFullGET.js | 31 |
1 files changed, 5 insertions, 26 deletions
diff --git a/src/api/routes/albums/albumFullGET.js b/src/api/routes/albums/albumFullGET.js index f92f9ae..93b56ce 100644 --- a/src/api/routes/albums/albumFullGET.js +++ b/src/api/routes/albums/albumFullGET.js @@ -13,33 +13,12 @@ class albumGET extends Route { const album = await db.table('albums').where({ id, userId: user.id }).first(); if (!album) return res.status(404).json({ message: 'Album not found' }); - /* - Grab the files in a very unoptimized way. (This should be a join between both tables) - */ - const fileList = await db.table('albumsFiles').where('albumId', id).select('fileId'); - const fileIds = fileList.map(el => el.fileId); - const files = await db.table('files') - .whereIn('id', fileIds) - .orderBy('id', 'desc'); + const files = await db.table('albumsFiles') + .where({ albumId: id }) + .join('files', 'albumsFiles.fileId', 'files.id') + .select('files.id', 'files.name') + .orderBy('files.id', 'desc'); - for (const file of files) { - file.albums = []; - const albumFiles = await db.table('albumsFiles') - .where('fileId', file.id); - if (!albumFiles.length) continue; - - for (const albumFile of albumFiles) { - const album = await db.table('albums') - .where('id', albumFile.albumId) - .select('id', 'name') - .first(); - if (!album) continue; - file.albums.push(album); - } - } - /* - For each file, create the public link to be able to display the file - */ for (let file of files) { file = Util.constructFilePublicLink(file); } |