Skip to content

Conversation

evenkkl
Copy link
Member

@evenkkl evenkkl commented Oct 26, 2026

Closes #12

What was done

Server

  • Added the five missing sorts to AlbumSort: TITLE_DESC, RELEASE_YEAR_ASC, AVERAGE_RATING_ASC and AVERAGE_RATING_DESC are new, and AVERAGE_RATING_DESC no longer throws. The existing enum is extended, not renamed.
  • New column album.average_rating (migration 0002). A trigger on review recalculates it for the one album a review insert, update or delete touches, so the cost does not grow with the catalog. It locks the album row first, so two reviews saved at the same time cannot leave a stale average. The migration fills the column from existing reviews.
  • The rating sorts read that stored column instead of a live AVG(), as agreed in the issue, so the cursor has a stable value to seek against.
  • Every sort keeps the same keyset cursor shape: sort value plus an album.id tiebreak, with NULLs last in both directions. Albums with no rating, or no release year, come last.
  • Album.averageRating now reads the same column, so the number on a card matches the order.

Client

  • A "Sort by" dropdown in the results header (a native <select> with a visible label). It writes through setSort from useSearchState, so the sort is in the URL and survives reload, back/forward and shared links.
  • Changing the sort sends a new query to the server. The client never re-sorts loaded results.

Docs

  • README.md, docs/architecture.md and docs/data-model.md are updated, and there is a new row in docs/AI-USAGE.md.

What was tested

  • pnpm format:check, lint, typecheck, test and the client build all pass.
  • Unit tests (search.test.ts) check the sort SQL, direction and NULL handling for every sort, the cursor conditions for the new sorts, and the id tiebreak on rating ties.
  • New sort.postgres.test.ts (runs when TEST_DATABASE_URL is set):
    • Pages every sort two rows at a time over fixture data full of tied titles, years and ratings plus NULLs, and checks the result equals one unpaged ORDER BY, with no gaps or duplicates.
    • Checks the trigger on insert, rating update, moving a review to another album, and delete. It runs in a transaction that is rolled back.
  • I ran these against a temporary PostgreSQL with all migrations applied, and they passed.
  • App.test.tsx: switching sort re-queries with the new sort and updates the URL, and a link with ?sort= restores the dropdown.

Notes for the reviewer

  • No new indexes. The existing title and year sorts don't have any either, so performance on the large seed can be its own follow-up if needed.
  • This overlaps a little with Server-side filtering with facet counts #13 (App.tsx, README.md, docs/architecture.md, the AI-USAGE table). Whichever merges second gets small, easy conflicts.

@evenkkl evenkkl requested a review from a team as a code owner October 26, 2026 19:15
@evenkkl evenkkl linked an issue Oct 26, 2026 that may be closed by this pull request
5 tasks
@robertky robertky mentioned this pull request Oct 26, 2026
Copy link
Member

@robertky robertky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks solid against issue #12. The stored average rating, review trigger, stable sort keys and pagination tests are good additions.

Please update the README before approval. It still says the app shows only the first 20 results ordered by relevance and lists sorting and pagination controls as planned, although #62 added pagination and this PR adds sorting. Please make the implemented and planned sections consistent with the current app.

After rebasing on the latest main, resolving the overlaps with #65 and confirming CI passes, this should be ready for approval.

@rachelks rachelks changed the title Feat/12 server side sorting feat: #12 server side sorting Oct 27, 2026
…orting

# Conflicts:
#	client/src/components/ResultsLayout.css
#	docs/AI-USAGE.md
#	server/drizzle/meta/0002_snapshot.json
#	server/drizzle/meta/_journal.json
@evenkkl
Copy link
Member Author

evenkkl commented Oct 28, 2026

Changes since the review:

  • Merged latest main. The average-rating migration is renumbered to 0003 because Import real album data into Crate #63 added 0002.
  • README implemented/planned sections updated to match the current app.
  • Load more keyboard test updated for the new sort dropdown in the tab order.
  • Format, lint, typecheck, tests and build pass.

The overlap with #65 will be resolved by whichever PR merges second.

@evenkkl evenkkl requested a review from robertky October 28, 2026 17:39
Copy link
Member

@rachelks rachelks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested PR #66 locally, including all sorting options, pagination, URL persistence and rating updates. Everything worked as expected, and the full test suite passed, including the PostgreSQL integration tests.

The updated documentation and migration numbering also look good. No further issues found from my testing. Looks good to me!

Copy link
Member

@robertky robertky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the README and rebasing on the latest main. The implemented and planned sections now match the current project, and the CI checks pass. Rachel’s review also confirms the sorting, pagination and rating updates work as expected. This addresses my previous feedback.

@robertky robertky merged commit 0d5a38f into main Oct 29, 2026
2 checks passed
@rachelks rachelks deleted the feat/12-server-side-sorting branch October 29, 2026 15:36
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Server-side sorting
3 participants