diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index 4df859e1a..9539feb87 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -1083,7 +1083,7 @@ def get_rom_simple( fetched on user-driven detail interactions (game details page, quick- note dialog open, achievements panel).""" - rom = db_rom_handler.get_rom(id) + rom = db_rom_handler.get_rom_simple(id) if not rom: raise RomNotFoundInDatabaseException(id) diff --git a/backend/handler/database/roms_handler.py b/backend/handler/database/roms_handler.py index 04c9c62a0..51ae73cd4 100644 --- a/backend/handler/database/roms_handler.py +++ b/backend/handler/database/roms_handler.py @@ -243,6 +243,50 @@ def with_details(func): return wrapper +def with_simple_details(func): + """Lightweight eager-load for the `SimpleRomSchema` (v2 gallery card). + + Loads only the relationships `SimpleRomSchema` serializes (rom_users, + files, sibling_roms + their rom_users, notes) and the deferred file-count + columns, skipping the `saves` / `states` / `screenshots` / `collections` + arrays that `with_details` pulls for the detail view. Keeps per-card + hydration cheap on low-power hosts. + """ + + @functools.wraps(func) + def wrapper(*args, **kwargs): + kwargs["query"] = select(Rom).options( + selectinload(Rom.platform), + selectinload(Rom.rom_users).options(noload(RomUser.rom)), + selectinload(Rom.metadatum).options(noload(RomMetadata.rom)), + selectinload(Rom.files).options( + joinedload(RomFile.rom).load_only(Rom.fs_path, Rom.fs_name), + selectinload(RomFile.track_meta), + ), + selectinload(Rom.sibling_roms).options( + noload(Rom.platform), + noload(Rom.metadatum), + # Per-sibling is_main_sibling resolution needs each sibling's + # RomUser (relationship is `lazy="raise"`). + selectinload(Rom.rom_users).options(noload(RomUser.rom)), + load_only( + Rom.id, + Rom.name, + Rom.platform_id, + Rom.fs_name_no_tags, + Rom.fs_name_no_ext, + ), + ), + selectinload(Rom.notes), + undefer(Rom.multi_file), + undefer(Rom.top_level_file_count), + undefer(Rom.has_soundtrack), + ) + return func(*args, **kwargs) + + return wrapper + + class DBRomsHandler(DBBaseHandler): @begin_session @with_details @@ -268,6 +312,18 @@ class DBRomsHandler(DBBaseHandler): ) -> Rom | None: return session.scalar(query.filter_by(id=id).limit(1)) + @begin_session + @with_simple_details + def get_rom_simple( + self, + id: int, + *, + query: Query = None, # type: ignore + session: Session = None, # type: ignore + ) -> Rom | None: + """Get a rom by ID with only the loads `SimpleRomSchema` needs.""" + return session.scalar(query.filter_by(id=id).limit(1)) + @begin_session @with_details def get_roms_by_ids( diff --git a/backend/tests/endpoints/roms/test_rom.py b/backend/tests/endpoints/roms/test_rom.py index eb3f5f95b..525895a25 100644 --- a/backend/tests/endpoints/roms/test_rom.py +++ b/backend/tests/endpoints/roms/test_rom.py @@ -44,6 +44,32 @@ def test_get_rom(client: TestClient, access_token: str, rom: Rom): assert body["id"] == rom.id +def test_get_rom_simple(client: TestClient, access_token: str, rom: Rom): + response = client.get( + f"/api/roms/{rom.id}/simple", + headers={"Authorization": f"Bearer {access_token}"}, + ) + assert response.status_code == status.HTTP_200_OK + + body = response.json() + assert body["id"] == rom.id + # SimpleRomSchema stays lightweight: none of the detail-only arrays are + # present, so the endpoint must not eager-load them. + assert "user_saves" not in body + assert "user_states" not in body + assert "user_screenshots" not in body + assert "user_collections" not in body + assert "all_user_notes" not in body + + +def test_get_rom_simple_missing_returns_404(client: TestClient, access_token: str): + response = client.get( + "/api/roms/999999/simple", + headers={"Authorization": f"Bearer {access_token}"}, + ) + assert response.status_code == status.HTTP_404_NOT_FOUND + + def test_download_multi_file_rom_content( client: TestClient, access_token: str, multi_file_rom: Rom ):