Awesome Reviewers

Reviewer · Database

Bound and minimize query results

Every data-access call must explicitly bound how much it reads and avoid pulling more than the caller actually needs. 1. Validate (don't silently clamp) any caller-supplied page/row size against a hard maximum before issuing the query, and reject out-of-range values with an error instead of truncating silently. Clamping a `perPage`/`size` value can make a...

Instruction

Every data-access call must explicitly bound how much it reads and avoid pulling more than the caller actually needs.

  1. Validate (don’t silently clamp) any caller-supplied page/row size against a hard maximum before issuing the query, and reject out-of-range values with an error instead of truncating silently. Clamping a perPage/size value can make a caller mistake a short, truncated page for the last page, and passing an unbounded value straight into an Elasticsearch size (or similar) risks exceeding index.max_result_window and failing the whole request.
const MAX_DOCS_PER_PAGE = 10_000;
if (perPage < 1 || perPage > MAX_DOCS_PER_PAGE) {
  throw Boom.badRequest(`perPage must be between 1 and ${MAX_DOCS_PER_PAGE}`);
}
  1. When streaming or batching large results (cursors, scroll/search_after, SQL cursors), cap both row count and response byte size, and read in small batches so a handful of oversized rows/documents can’t be buffered in full before the cap applies. Also guard against a deadline/timeout for the whole read, not just per-batch.

  2. Never fetch a whole document/row when only a few fields are required. Use field-level projections (_source.includes, sourceIncludes, selecting specific columns) instead of loading the full payload and discarding the rest — this reduces both query time and transferred/held data, especially important for large text/JSON blobs.

  3. Double check truncation/edge-case handling around these limits (e.g., don’t let a “drop last item to signal truncation” strategy wipe out an already-small result set), and keep paging/sizing limits in sync across the API schema and the service layer that executes the query.</description>

</invoke>