Only select necessary columns in artists/albums/songs views

Some guy on GitHub said this is better lol
This commit is contained in:
mitteneer 2025-03-28 09:47:07 -04:00
parent 43e07901d6
commit 05e9c05742
4 changed files with 17 additions and 6 deletions

View file

@ -5,7 +5,8 @@ const jetquery = @import("jetzig").jetquery;
pub fn index(request: *jetzig.Request) !jetzig.View { pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); var root = try request.data(.object);
var albums_view = try root.put("albums", .array); var albums_view = try root.put("albums", .array);
const query = jetzig.database.Query(.Album).select(.{}) const query = jetzig.database.Query(.Album)
.select(.{ .id, .name })
.include(.albumartists, .{ .select = .{.artist_id} }) .include(.albumartists, .{ .select = .{.artist_id} })
.orderBy(.{ .name = .asc }); .orderBy(.{ .name = .asc });
const albums = try request.repo.all(query); const albums = try request.repo.all(query);
@ -16,7 +17,7 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
var artist_infos = try album_view.put("artist_info", .array); var artist_infos = try album_view.put("artist_info", .array);
for (album.albumartists) |artist| { for (album.albumartists) |artist| {
var artist_info = try artist_infos.append(.object); var artist_info = try artist_infos.append(.object);
const artist_data = try jetzig.database.Query(.Artist).where(.{ .id = artist.artist_id }).all(request.repo); const artist_data = try jetzig.database.Query(.Artist).select(.{ .id, .name }).where(.{ .id = artist.artist_id }).all(request.repo);
for (artist_data) |ad| { for (artist_data) |ad| {
try artist_info.put("name", ad.name); try artist_info.put("name", ad.name);
try artist_info.put("id", ad.id); try artist_info.put("id", ad.id);
@ -35,7 +36,11 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); var root = try request.data(.object);
try root.put("album", album.?.name); try root.put("album", album.?.name);
var songs_view = try root.put("songs", .array); var songs_view = try root.put("songs", .array);
const query = jetzig.database.Query(.Albumsong).include(.song, .{ .select = .{ .name, .id } }).join(.inner, .album).where(.{ .album = .{ .id = id } }); const query = jetzig.database.Query(.Albumsong)
.select(.{.id})
.include(.song, .{ .select = .{ .name, .id } })
.join(.inner, .album)
.where(.{ .album = .{ .id = id } });
const songs = try request.repo.all(query); const songs = try request.repo.all(query);
for (songs) |song| { for (songs) |song| {
const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .song_id = song.song.id }).count().execute(request.repo); const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .song_id = song.song.id }).count().execute(request.repo);

View file

@ -5,7 +5,9 @@ const jetquery = @import("jetzig").jetquery;
pub fn index(request: *jetzig.Request) !jetzig.View { pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); var root = try request.data(.object);
var artists_view = try root.put("artists", .array); var artists_view = try root.put("artists", .array);
const query = jetzig.database.Query(.Artist).select(.{}).orderBy(.{ .name = .asc }); const query = jetzig.database.Query(.Artist)
.select(.{ .id, .name })
.orderBy(.{ .name = .asc });
const artists = try request.repo.all(query); const artists = try request.repo.all(query);
for (artists) |artist| { for (artists) |artist| {
const scrobbles = try jetzig.database.Query(.Scrobbleartist).where(.{ .artist_id = artist.id }).count().execute(request.repo); const scrobbles = try jetzig.database.Query(.Scrobbleartist).where(.{ .artist_id = artist.id }).count().execute(request.repo);
@ -26,6 +28,7 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
try root.put("artist", artist.?.name); try root.put("artist", artist.?.name);
var albums_view = try root.put("albums", .array); var albums_view = try root.put("albums", .array);
const query = jetzig.database.Query(.Albumartist) const query = jetzig.database.Query(.Albumartist)
.select(.{.id})
.include(.album, .{ .select = .{ .name, .id } }) .include(.album, .{ .select = .{ .name, .id } })
.join(.inner, .artist) .join(.inner, .artist)
.where(.{ .artist = .{ .id = id } }); .where(.{ .artist = .{ .id = id } });

View file

@ -4,7 +4,7 @@ const jetzig = @import("jetzig");
pub fn index(request: *jetzig.Request) !jetzig.View { pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); var root = try request.data(.object);
var scrobbles_view = try root.put("scrobbles", .array); var scrobbles_view = try root.put("scrobbles", .array);
const query = jetzig.database.Query(.Scrobble).select(.{}) const query = jetzig.database.Query(.Scrobble).select(.{.date})
.include(.song, .{ .select = .{ .id, .name } }) .include(.song, .{ .select = .{ .id, .name } })
.include(.album, .{ .select = .{ .id, .name } }) .include(.album, .{ .select = .{ .id, .name } })
.include(.scrobbleartists, .{ .select = .{.artist_id} }) .include(.scrobbleartists, .{ .select = .{.artist_id} })

View file

@ -4,7 +4,10 @@ const jetzig = @import("jetzig");
pub fn index(request: *jetzig.Request) !jetzig.View { pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); var root = try request.data(.object);
var songs_view = try root.put("songs", .array); var songs_view = try root.put("songs", .array);
const query = jetzig.database.Query(.Song).select(.{}).orderBy(.{ .name = .asc }); const query = jetzig.database.Query(.Song)
.select(.{ .id, .name })
.include(.songartists, .{ .select = .{.artist_id} })
.orderBy(.{ .name = .asc });
const songs = try request.repo.all(query); const songs = try request.repo.all(query);
for (songs) |song| { for (songs) |song| {
const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .song_id = song.id }).count().execute(request.repo); const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .song_id = song.id }).count().execute(request.repo);