From 82b1f12f9905349dce69b78a97d2b161fe54d87e Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 1 Oct 2024 12:26:27 +0100 Subject: [PATCH] Avoid loading image-related fields when not necessary Image data and url are only necessary when loading the image, which is done exclusively via the /images endpoint. Most other read queries can safely exclude them, and rely only on the hash as a cache key. --- lib/music_library/records.ex | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index ef232b4b..511256d2 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -4,6 +4,8 @@ defmodule MusicLibrary.Records do alias MusicLibrary.Records.{MusicBrainz, Record} + @fields [:id, :type, :format, :title, :year, :genres, :musicbrainz_id, :image_data_hash] + def list_records(opts \\ []) do limit = Keyword.get(opts, :limit, 20) offset = Keyword.get(opts, :offset, 0) @@ -12,7 +14,8 @@ defmodule MusicLibrary.Records do from r in Record, order_by: [r.artists[0]["sort_name"], r.title], limit: ^limit, - offset: ^offset + offset: ^offset, + select: ^@fields Repo.all(q) end @@ -26,7 +29,8 @@ defmodule MusicLibrary.Records do where: like(r.title, ^"%#{query}%") or like(r.artists, ^"%#{query}%"), order_by: [r.artists[0]["sort_name"], r.title], limit: ^limit, - offset: ^offset + offset: ^offset, + select: ^@fields Repo.all(q) end @@ -49,7 +53,8 @@ defmodule MusicLibrary.Records do q = from r in Record, order_by: [desc: r.inserted_at], - limit: 1 + limit: 1, + select: ^@fields Repo.one!(q) end