Repository navigation
Page deck and card lists by id with limit and after - #96
Merged
Merged
Conversation
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.
GET /api/decks/andGET /api/decks/{id}/cards/returned every row. A benchmark had the deck list going from 4 ms with 20 decks to 120 ms with 8,500.Both endpoints now page by id (keyset):
limit(1 to 100, default 50) andafter(the last id the client has seen, optional).{"items": [...], "next_after": <id> | null}. Passnext_afterback asafterto get the next page. It isnullon the last page.WHERE id > :after ORDER BY id LIMIT :limit + 1. The extra row tellsPage.from_modelswhether there is a next page, so noCOUNT(*)is needed.Why keyset over OFFSET: both lists were already ordered by id, and OFFSET makes Postgres read and discard every skipped row, so deep pages get slower as the table grows. Keyset keeps every page at index-scan cost on the primary key.
No new index. The deck list uses the primary key. The card list filters on
deck_idand orders byid, which the(deck_id, front)unique index plus a sort covers well enough for per-deck card counts. A(deck_id, id)index can come later if decks grow large.Breaking change: clients that relied on getting every row in one response now get the first 50 and must follow
next_after. Write endpoints (POST/PUT /decks/{id}/cards/) still return the unpagedCardscollection.schemas.Decksis replaced byschemas.DecksPage.Bad params (
limit=0,limit=101,after=0, non-integer) are rejected by the framework's validation.AGENTS.md documents the pattern next to the
Collection[T]convention.just lintandjust testpass (100% coverage).