Keep search results ordered when batch-loading persons#132
Merged
atomantic merged 1 commit intoJun 28, 2026
Merged
Conversation
`searchWithSqlite` computes its result order with `ORDER BY display_name` and passes the ordered ids to `getPersonsBatch`. That loader fetched rows with `WHERE person_id IN (...)`, which SQLite returns in table (rowid) order — silently discarding the requested order, so the default search view appeared randomly sorted. Assemble persons by iterating the input `personIds` (looking each row up in a by-id map) so the caller's order is preserved and ids with no row are skipped. Also removes the dead `searchService.quickSearch` and `searchGlobal` methods (no callers anywhere — the live quick-search route has its own inline SQL) and the now-unused `idMappingService` import. These were the last remaining N+1 person-load loops in the search service, closing out PLAN "Next Up" #4. New unit tests guard the batch loader's ordering and missing-id behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The SQLite search path (
searchWithSqlite→getPersonsBatch) was silently losing its sort order.searchWithSqlitecomputes the result order withORDER BY display_nameand passes the ordered ids togetPersonsBatch, but that loader fetched rows withWHERE person_id IN (...)— which SQLite returns in table (rowid) order, not in the order of the IN list. The requested ordering was discarded, so the default (unsorted) search view inSearchPageappeared randomly ordered.The fix indexes the fetched rows by id and assembles the result by iterating the input
personIds, so the caller's order is preserved and ids with no matching row are skipped. This is a single-pass change — the loop now emits in order directly instead of assembling in DB order and re-sorting.While here, this removes the dead
searchService.quickSearchandsearchService.searchGlobalmethods (no callers anywhere — the live quick-search route inperson.routes.tshas its own inline SQL) and the now-unusedidMappingServiceimport. These were the last remaining N+1 person-load loops in the search service, closing out PLAN "Next Up" #4.Test plan
tests/unit/services/databaseBatch.spec.tspins the batch loader's contract: returns persons in the caller-requested order (not SQLite table order), drops requested ids with no row, returns[]for an empty list. The order test fails without the fix (mock returns table order, test asserts requested order).npx tsc -p server/tsconfig.json --noEmit— clean.npx vitest run tests/unit tests/integration— 165 passed.